Arduino Millis() Rollover Handling

Millis Police

On the Arduino microcontroller, the millis() function counts the number of milliseconds since the program started running. Unfortunately, this count resets to zero after approximately 9 hours and 32 minutes. I have written a millisRollover() function that detects these rollovers, so that programs can respond properly to the overflow event. This can solve problems with servo routines, steppers, timed pauses and a variety of other calculations. In addition, because my millisRollover() function counts the number of times rollover has happened, it is now possible to record total Arduino runtime with a counter that’s good for over 35 years.

You want to blink an LED only on Christmas during leap years? Totally possible now.

(yes, there really is a millis police department)
Posted in General
11 comments on “Arduino Millis() Rollover Handling
  1. John says:

    I’m having a little difficulty understanding how I would use your rollover function in a scenario where I tick a clock if 1000 ms has elapsed, like:

    if( millis() – previousMillis > 1000 ) {
    previousMillis = millis();
    // One second has elapsed, tick the clock.

    It seems like this will “hiccup” during a rollover.

  2. faludi says:

    Glad you asked because it’s worth noting that the millis() function was greatly improved in Arduino 0012:

    * Improved millis(): it now overflows after 49 days instead of 9 hours, but
    now uses slightly more processing power.

    I’m assuming this means that it now fills the full unsigned long variable. So in your case you should just be able to do the math. The rollover will cause your subtraction to overflow an the result would be correct.

  3. David says:

    Just a thought – why not use the older, computationally cheap code for 49 minute roll overs and use a second 16 bit variable as a count for the roll overs and use seconds to return the sum of the two variables as a 64 bit value. Gives a long, long time before rollover.
    short int seconds = 0; //
    unsigned long millisLong() {
    return unsigned long int (seconds) * 1000 + unsigned long int (millis());

    I know this requires the ‘second’ variable to have millisecond precise accuracy but that is a relatively small price to pay for a very large, high precision counter.

    Probably best set by a build flag because of the extra interrupts required.

  4. David says:

    typo: short unsigned int seconds = 0;

  5. Hi folks,

    There’s a much better generic way of handling rollovers on timers. Instead of using unsigned timer values you use signed timer values. Then to decide whether time1 > time2 you simply calculate (time1-time2>0) which returns correct results for all cases where the period itself is b isn’t equivalent to a-b>0, due to the nature of 2’s compliment arithmetic. Thus, if instead we have:

    long eventTimeout=(long)millis()+1000;
    if((long)millis()-eventTimeout>=0) {

    We will *never* get a roll-over condition! Consider, in the old code, the if statement would fail when millis=0, and oldMillis was 4294967000. Here, millis-oldMillis (very much) >= 1000 even though the timer should not have expired.

    However, with the new code, we instead calculate the eventTimeout and compare with 0, so in the case above (long)millis – eventTimeout = -704, so there’s no timeout, until millis>=704.

    You might think this technique would fail when the signed values roll-over to unsigned values. However, it doesn’t. Consider when eventTimeout was 2147483000. When millis() reached that value, so millis()-eventTimeout was >=0, eventTimeout was then set to 2147484000 which is actually -2147483296. The timeout looks like it’s in the past, but because we’re doing signed arithmetic (long)millis()-eventTimeout = 2147483000-(-2147483296) = -1000 and so it sees it as being in the past (which it is). Similarly when (long)millis() crosses from 2147483647 to -2147483648 the subtraction will yield -353, then -352 and will only result in >=0 when (long)millis() = -2147483296 as intended.

    This can be combined into a function:

    #define smillis() ((long)millis())

    boolean after(long timeout)
    return smillis()-timeout>0;

    Then your code will always work if you do:

    long timeout=smillis()+1000;

    if(after(timeout)) {
    … do something.

    -cheers from Julz @P

  6. bombastic bob says:

    you might check out a tutorial I wrote for the playground wiki,

    There is a much simpler solution illustrated here. It’s the one I always use, and is actually similar to what the Linux kernel does for its kernel timers based on ‘jiffies’. But I didn’t steal my method from Linux. I just happened to observe that it does the same kind of thing.

  7. dadap says:

    may i know how to reset millis function for a 24-hour period.. coz, im doing a project called rainfall monitoring with the use of a tipping bucket rain gauge that sends pulses. and i want to record raainfall data for a 24-hour period only after then, the tipcount rsets to 0. thank u

  8. Alex says:

    Where did the value 17179868 come from? Twice this value is 34359736, which is not a power of two, suggesting that the max value is arbitrarily set and not a result of binary arithmetic.

    • faludi says:

      As I recall this number was empirically determined. It’s not a simple register overflow but dependent upon several other factors as well.

      • Alex says:

        I believe this article is based on the old millis function, as is the article by Mahto (link at the bottom of page). The millis function was greatly improved in Arduino 1.0 to rollover in approximately 50 days, which is the result of a 32 bit unsigned integer overflowing. The number 34359736 milliseconds is approximately 9.5 hours, which fits the release notes that the rollover time was increased from 9.5 hours to 50 days. Reference wiring.c in the current repository.

  9. AlexK says:

    Yeah, the simplest way is to calculate a time span like this:

    unsigned timeSpan(unsigned long startTime, unsigned long endTime)
    return (unsigned long)((long)endTime – (long)startTime);

    So timeSpan(0, 1) = 1 and timeSpan(0xffffffff, 0) = 1

3 Pings/Trackbacks for "Arduino Millis() Rollover Handling"
  1. […] accuracy of the millisecond count, and second, how overflows were handled. Rob Faduli proposed a method for detecting and handling rollovers in the code, but I was confused about the problem- if it was a […]

  2. […] nach etwa  9 Stunden (bzw. etwas mehr) wieder bei Null an zu zählen. Rob Faludi hat eine elegante Lösung für dieses Problem gefunden. Dieser Eintrag wurde veröffentlicht unter Arduino. Permalink in die Lesezeichen […]

  3. […] is the 9.54 hour rollover event, which Rob Faludi has provided an excellent solution for here. I made up a nice little over-analysis of the issue, available […]

Subscribe by email:

Enter your email address:

Delivered by FeedBurner