Go Down

Topic: Best Practice with millis()? (Read 2303 times) previous topic - next topic



On the other hand, the rollover time is 49+ days. If the events of interest are more than 24 days apart, millis() is not really the tool of choice. An RTC is, for durations like that.
The art of getting good answers lies in asking good questions.


An RTC is, for durations like that.

you're perfectly right!


The only time (forgive the pun) you'll have a problem is if the interval is more than half the rollover time.

Exact! For example having as max number 255 (8bit)

d(12-240) --> b(0000 1100-1111 0000) = b0001 1100 d28 OK

d(12-112) --> b(0000 1100-0111 0000) = b1001 1100 d-100 WRONG (result would be 255-112+12=155 )

No, no, no.  There is no problem at all if the interval is more than half the rollover time.

Consider the above, with an 8-bit timer.  At time 112 was the 'last' event, and 12 is the 'current' time?  How much time has elapsed?  Well, with uint8_t, 12-112 is 156, and that's exactly right.  156 ticks have passed.  144 ticks passed between the last event and the rollover, and 12 more passed between the rollover and now.  No problem.

Consider an extreme case, back to uint32_t's from millis(), where the interval is 4294966296 (aka, -1000).  The 'last' event was at 4294967196 (-100), and the 'current' time is 4294966196 (-2100).  What is the elapsed time?  now-current is 4294965296 (-2000) which is still less than the interval, and so your event won't fire.  All is well.

Go Up