Skip to main content
Arduino

Timeline for How can I handle the millis() rollover?

Current License: CC BY-SA 3.0

12 events
when toggle format what by license comment
Jul 7, 2023 at 17:00 comment added NateS The accepted answer is very good but didn't explain this part.
Aug 4, 2021 at 2:44 review Suggested edits
Aug 4, 2021 at 7:49
Jul 23, 2016 at 17:29 comment added Mausy5043 does one need to cast the calculation result to unsigned long? Or will this be automatic if the calculation is executed with two variables already declared as unsigned long?
S Apr 6, 2016 at 18:19 history suggested Community Bot CC BY-SA 3.0
Fixed calculation
Apr 6, 2016 at 13:55 review Suggested edits
S Apr 6, 2016 at 18:19
Jun 13, 2015 at 9:46 comment added Gerben Intervals larger that 49 days will be a bit more difficult, but in most applications not very useful, as the arduino crystal is not that accurate. Over 49 this inaccuracy could result in hours of error. Multiple rollovers can't occur since previousMillis is change when the interval has passed. PS the formula would actually be (t2-t1+4,294,967,295) % 4,294,967,295 since modulo will also give negative values (e.g. -3%2==-1)
Jun 12, 2015 at 23:11 comment added xyz Ah, ok. if you do t2-t1, and if you can guarantee t1 is measured before t2 then it's equivalent to signed (t2-t1)% 4,294,967,295, hence the auto wraparound. Nice!. But what if there are two rollovers, or interval is > 4,294,967,295?
Jun 12, 2015 at 22:35 comment added BrettFolkins @prakharsingh95 The one stored in previousMillis has to have been measured before currentMillis, so if currentMillis is smaller than previousMillis a rollover occurred. The math happens to work out that unless two rollovers have occurred, you don't even need to think about it.
Jun 12, 2015 at 22:28 comment added xyz Let me rephrase. Suppose you have two timestamps 200ms and 10ms. How do you tell which is(are) rolled over?
Jun 12, 2015 at 22:16 comment added BrettFolkins @prakharsingh95 10ms-15ms will become ~49.7 days - 5ms, which is the correct difference. The math works until millis() rolls over twice, but that is very unlikely to occur to the code in question.
Jun 12, 2015 at 21:15 comment added xyz How about a 15ms before rollover and a 10ms after rollover (ie 49.7 days after). 15 > 10, but the 15ms stamp is nearly a month and a half old. 15-10 > 0 and 10-15 > 0 unsigned logic, so that is no use here!
Jun 12, 2015 at 13:14 history answered Gerben CC BY-SA 3.0

AltStyle によって変換されたページ (->オリジナル) /