system
February 11, 2015, 4:18pm
1
Hi all,
today I came here to post a solution to a problem, but hopefully it will help somebody else one day.
I wanted to schedule various things to happen every minute, hour, day etc. using a DS3232 real time clock and the time.h library.
All was going well until I got to the daily schedule as I used the following code:
if(currentTime>lastCleanup+60*60*24) {
//do stuff
}
it turns out that 6060 24 evaluates to 20864, which is wrong.
The code works fine when I substitute 86400.
That has nothing to do with the IDE.
6060 24 = 86400. BUT 86400 does not fit into a 16-bit integer so you get weird result.
Change 6060 24 to 86400L
Pete
KeithRB
February 11, 2015, 4:21pm
3
Please, don't keep in suspense! YoU'Ll be keeping us from knowing how to fix the problem!
Perhaps you should have used
if(currentTime>lastCleanup+60UL*60*24) {
//do stuff
}
system
February 11, 2015, 4:23pm
5
Thanks Pete, hopefully it will help someone one day.
Presumably you are comparing long integer times, in which case another thing you need to fix is that the comparison will fail when the time wraps around.
Use this:
if(currentTime - lastCleanup > 86400L) {
//do stuff
}
Pete
system
February 11, 2015, 4:30pm
7
As the time is in seconds since 1970, a quick calculation says time will wrap around in approx 136 years. Or is there something I'm missing?
KeithRB
February 11, 2015, 4:40pm
8
He assumed that the current time is from millis(), not your RTC converted to a unix time.
Ooops. Brain not in gear again.
Pete