possible bug in TimeAlarms.h

Hi all.

I think i have found a bug in the TimeAlarms.h library. :-/

When using the Alarm.alarmRepeat function:

when the system starts up (arduino is powered on), the internal system time will be 00:00:00 by default.

If I then schedule an alarm for 08:00:00 daily, and then update the system time to something after 08:00 (lets say… 10:24:30), the scheduled alarm will execute, even if the time is not 08:00.

It seems like the alarmRepeat function will fire when the system time passes the set alarm time, even if the system time never actually hits the exact time for the alarm.

heres some code snippets from my project, where I have observed this behaviour:

includes:

// Libraries for time and timed calls
#include <Time.h>
#include <TimeAlarms.h>

setup:

  Alarm.timerRepeat(1,  Repeat1s);    

  Alarm.alarmRepeat(8,00,00, Daily);             // 08:00 every day

main:

void Daily()
{
  sentAlarmToday = false;
  sentWarnToday = false;

  Serial.println("daily");
}

hope my description makes sense! :slight_smile:

Doesn't it make sense to set the correct time BEFORE setting any alarms?

It seems like the alarmRepeat function will fire when the system time passes the set alarm time, even if the system time never actually hits the exact time for the alarm.

Time on the Arduino does not tick away in seconds. It ticks away in milliseconds. There are 1000 milliseconds in a "second", but the value in seconds is only updated every 1024 milliseconds. Periodically, the time, as reported to the nearest second, will jump by two seconds. Therefore, triggering the alarm at the exact second specified may not be possible. Therefore, the alarm needs to be triggered when the time is greater than or equal to, not strictly equal to, the specified alarm time.

Doesn't it make sense to set the correct time BEFORE setting any alarms?

You are right, and I have modified the code accordingly since I posted the above possible bug.

your explanation of the seconds vs ms makes perfect sense. I thought it might be something like that, that caused the behaviour. ::)