I have a kind of alarm clock built using Teensy LC and a DS1307 for time keeping. I can set alarms using a text file that's read from SD. The times are converted into minutes-into-the-day, and stored in RAM. If the current time (minutes-into-the-day) matches one of the alarms, the user is signaled. I check for a match between alarms and current time once at the top of every minute, whenever "seconds" equals zero.
Sounds good, right? The problem is -- and this has taken awhile to find -- if an alarm is precisely on the top of the hour, every now and then it won't trigger. This is because occasionally the Time library hour() and minute() functions ARE NOT IN SYNC.
I tracked the current time by printing it to the console at the top of every minute for 12 hours. For nine hours, everything was fine, but then hour() jumped back in time one hour for a fraction of a second like this:
8:57:00
8:58:00
8:59:00
8:00:00
9:01:00
9:02:00
I say "fraction of a second" because I assume they're only out of sync for the cycle that I checked minute() and hour() and stored them as variables. I would bet that adding a delay(1) somewhere in the code would be a workaround, but I would like to see this fixed at a lower level on the off chance it is a Time library or Teensy hardware bug. Unfortunately it's a PITA to reproduce. For what it's worth, it also happened an hour later at 10:00, but then not again for 4 hours after which I stopped the execution because....
I'm currently trying the same experiment using a DS3231 instead of a DS1307 and will followup with the results. In the meantime I would appreciate any other perspectives or input on the situation.
On a related note, my "get time" function is run on update and looks like this:
The Time library is set to sync with the RTC every second, so is it possible that it's occasionally re-syncing in the middle of my function, between currentHour and currentMinute? If so, how would I completely avoid this?
Sounds good, right? The problem is -- and this has taken awhile to find -- if an alarm is precisely on the top of the hour, every now and then it won't trigger. This is because occasionally the Time library hour() and minute() functions ARE NOT IN SYNC.
I tracked the current time by printing it to the console at the top of every minute for 12 hours. For nine hours, everything was fine, but then hour() jumped back in time one hour for a fraction of a second like this:
8:57:00
8:58:00
8:59:00
8:00:00
9:01:00
9:02:00
I say "fraction of a second" because I assume they're only out of sync for the cycle that I checked minute() and hour() and stored them as variables. I would bet that adding a delay(1) somewhere in the code would be a workaround, but I would like to see this fixed at a lower level on the off chance it is a Time library or Teensy hardware bug. Unfortunately it's a PITA to reproduce. For what it's worth, it also happened an hour later at 10:00, but then not again for 4 hours after which I stopped the execution because....
I'm currently trying the same experiment using a DS3231 instead of a DS1307 and will followup with the results. In the meantime I would appreciate any other perspectives or input on the situation.
On a related note, my "get time" function is run on update and looks like this:
Code:
void getCurrentTime()
{
currentHour = hour();
currentMinute = minute();
currentSecond = second();
}
The Time library is set to sync with the RTC every second, so is it possible that it's occasionally re-syncing in the middle of my function, between currentHour and currentMinute? If so, how would I completely avoid this?
Last edited: