Go Down

Topic: Time and TimeAlarms Libraries – Ask here for help or suggestions (Read 78 times) previous topic - next topic


Hey all,

I have a question regarding the function time_t now() inside the time library:
Inside this function the Arduino built-in timer ( millis() ) is compared to a last update time of the library.
I wonder what will happen after approx. 50 days of operation when the number returned by millis() has an overflow.

As far as I understood the code, the time will stay at its last update time and will not run anymore.
Did anyone observe such a behaviour or is there a certain function which takes care about such an overflow which I did not recognize?


Code: [Select]
time_t now(){
  while( millis() - prevMillis >= 1000){     
    prevMillis += 1000;
    sysUnsyncedTime++; // this can be compared to the synced time to measure long term drift     


  return sysTime;

Nick Gammon

millis will wrap around after 50-odd days. It doesn't stop, or not run.

If you do subtraction that wrap-around won't matter.


Code: [Select]

if (millis () - startTime >= wantedInterval)
 // do something


What will happen if you have the following:
  Alarm.alarmRepeat(8,30,0, AlarmA);
  Alarm.alarmRepeat(8,30,0, AlarmB);
  Alarm.alarmRepeat(8,30,0, AlarmC);

Will AlarmA get executed 1st, and then AlarmB and then AlarmC? 

Does the accuracy of the time, depends on other program running, or it has to be idle?  In another words, Do I need to add delay(1) or something like that in my loop? 

Does the time accuracy changes when used with other libraries like PulsePattern?

Thank you.


Alarms for the same time will be called in the order the were scheduled.

The scheduler calls the alarm handler in the Alarm.delay function so this should be called regularly in the sketch. There is a readme file in the download with some more details.
When there are more than one alarm ready to be triggered, the first scheduled alarm is serviced on the next call to Alarm.delay, the next scheduled alarm is serviced on the next call to Alarm.delay and so on.
The accuracy of the servicing depends on how often the Alarm.delay function is called.   

I have not used PulsePattern, but if time the code is in the interrupt handler is short, the timing accuracy should not be significantly affected. Why not give it a try. If you print the millis value in the alarm handler you should be able to see if that library is affecting the servicing of the alarms.

I hope that helps.

Have fun!


Thank you Mem.  It was very helpful. 

I have been successfully use Alarm.timerRepeat(1, DisplayTime) and it seems to work correctly. Showing "millies" is a great idea, thank you.  I will add that to my code. 

Within my routine, it fetches something from the network, Reading some huge file, and parse and filter what it receives.  I have this done every one minute with Alarm.timerRepeat(60, ReadData).  The problem is when the data is being read, the DisplayClock does not seem to be executed (Clock stops), for 10~20 seconds.  When its done with the ReadData code, it goes back and show the clock every second and the time seems to be correct. 

With in ReadData code, I am using Alarm.delay, client.connect and some other functions from Ethernet library. 

The PulsePattern works on Timer1.  I will give that a try. 

Thanks again. 

Go Up