2 years, 10 months ago.

Problem with alarm in clock

#include "mbed.h"
#include "TextLCD.h"
 
DigitalOut led1(PC_10);
DigitalOut buzzer(PC_5);
TextLCD lcd(PC_0, PC_1, PA_1, PC_2, PC_3, PA_4, TextLCD::LCD16x2); // rs, e, d4-d7
 
void displayFunction( void )
{
    time_t seconds = time(NULL);
    lcd.printf("Time: %s", ctime(&seconds));
    wait(1);
}

void alarmFunction( void )
{
    lcd.printf("ALARM !");
    //todo pin alarm
    led1 = !led1;
    wait(0.1);
    buzzer = !buzzer;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
    wait(0.1);
}

int main() {
    struct tm t;
    
    t.tm_hour = 15;
    t.tm_min = 31;
    t.tm_sec = 00;
    
    t.tm_year = 2018;
    t.tm_mon = 06;
    t.tm_mday = 14;

    t.tm_year = t.tm_year - 1900;
    t.tm_mon = t.tm_mon - 1;
    
    struct tm a;
    
    a.tm_hour = 15;
    a.tm_min = 31;
    a.tm_sec = 00;
    
    a.tm_year = 2018;
    a.tm_mon = 06;
    a.tm_mday = 14;

    a.tm_year = a.tm_year - 1900;
    a.tm_mon = a.tm_mon - 1;
        
        
    while(1) {  
    displayFunction();
    }
}


Hello, i dont know how to make alarm in this clock. There is any function to get this value, how to make it. I need only "if statement" to equal current time to my alarm time.

1 Answer

2 years, 10 months ago.

Hi Sebastian,

Basically you need to convert your timestamp ("seconds") into a time structure. Fortunately there's a C library function that does just that called "local time()".

Then, every second you can compare the time structures for your alarms ("t" and "a") to the current time. You'll have to check the tm_hour and tm_min fields for each alarm. When those fields are equal you can trigger the alarm.