Help with RTClib - using now.second() to create an offset

Hi Guys/Gals,
Apologies for the long post.
I am using the code below :

schedMillis=curMillis; //(used for time checking interval)
Serial.println(now.second()); //Debugging line -Prints current seconds
seconds=(int)now.second();//saves current seconds to int seconds
Serial.println(seconds);//Debugging line -Prints seconds
SCHEDSTEP = 60000 - (seconds * 1000);//Converts seconds to milliseconds and deducts from 60000ms to set current delay offset
Serial.println(SCHEDSTEP,DEC);//Debugging line - Print current Delay

The code is run when I sync the time of my Android phone to Arduino.
What I am doing here is:

  • I have a scheduler setup on Arduino which is set with an android App.
  • The App sends the start time to arduino.
  • I am setting arduino to check every 1 minute(60000millis), since schedule is set in HH:mm and ss is omitted.
  • The scheduler flag is enabled and the loop will start checking the clock to see if the "The time is Right" to switch on a device.

Say i upload the schedule, ON at 10:35, at 10:30:10 (local arduino and android time since they are synced)

The check will first happen at 10:31:10. I do not want that I want it to check at 10:31:00,10:32:00 etc.
If I upload the schedule when the seconds of the local clock are 0s and roughly 31-33s the offset is o.k.
As soon as I skip that the offset grows instead of get smaller.
I cannot understand what is happening here.
See Serial Monitor Below maybe it can help you help me :slight_smile:
FYI After this first offset is calculated and used in first scheduler check the delay between checks is defaulted to 60000millis (:00s)

DATE: 28/8/2011 [i]here i sync the time between android and arduino[/i]
Time: 10:56:53
5        [i]here I upload schedule for 10:58. This is the time I always upload below.[/i]
55000 [i] Offset is 55 seconds for next check. Good[/i]
17 [i]I upload schedule again. Same Schedule repeated to show the problem below[/i]
43000 [i]Offset is 43 seconds. Good[/i]
21
39000 [i]Offset is 39 seconds. Good[/i]
24
36000 [i]Offset is 36 seconds. Good[/i]
26
34000 [i]Offset is 34 seconds. Good[/i]
29
31000 [i]Offset is 31 seconds. Good[/i]
31
29000 [i]Offset is 29 seconds. Good[/i]
34
91536 [i][b][u]Offset is 91 seconds. Very BAD[/u][/b][/i]
40
85536 [i][b]Again BAD.....and so on[/b][/i]
45
80536
49
76536
55
70536
57
68536
12
48000 [i]Offset is 48 seconds. Good Again[/i]
16
44000

I cannot understand what is happening.
i have used a different way to calculate the offset but the otherway round happend i.e 0-31 bad offset and the rest ok.
if i recall well it was SCHEDSTEP = (60 - seconds) * 1000;
I do not have it documented anymore so I might be wrong but it did happen.

Hope the above is clear. if not let me know.
Thanks in advance

Looks like an overflow, use - 60000UL - iso 60000, the UL stands for unsigned long.

for math with time always use the type unsigned long, give it a try...

Spot on Dude!
I changed the 60000 and 1000 values in the code used to 60000UL and 1000UL as suggested and it worked.
I didn't think it would overflow.

Thanks!

an int on the Arduino platform goes from -32768 - 32767 which is less than 60.000 so an overflow is made fast ...