[image] misiekd: Could you help me to explain The difference between two unsigned numbers is… unsigned. As long as you use unsigned long integers, and subtract to calculate durations, millis() rollover is not a problem unless you are trying to time really long intervals like 49 days long. W… Answer from alto777 on forum.arduino.cc
🌐
Arduino Forum
forum.arduino.cc › forum 2005-2010 (read only) › software › syntax & programs
What is maximum value of millis() - Syntax & Programs - Arduino Forum
May 15, 2010 - In Arduino version 18, what is ... without knowing when that might occur · Millis() uses unsigned longs which is a 32 bit number....
🌐
Tech Explorations
techexplorations.com › home › tech explorations guides › /arduino/ › programming › 12. ​how to deal with the millis rollover
12. ​How to deal with the millis rollover - Tech Explorations
March 5, 2024 - Remember that these values are unsigned, so the subtraction does not yield a negative number, but a positive that is the result of the difference between 4,294,947,295 and 4,294,967,295 (the max value for a 4-byte unsigned number) PLUS 3,000.
Discussions

Display upTime using `millis()` - Arduino Uno - Arduino Stack Exchange
So, use unsigned long data types for millis and other stuff that's large. 4 billion and some change is the max value that unsigned long data types can store, hence no overflowing till about 49 days. More on arduino.stackexchange.com
🌐 arduino.stackexchange.com
Question about ESP32 millis() counter?
Hello, I have this kind of question. Is there any limitation about max millis() counter? I mean does it matter if currenttime in millis counts up certain value and the whole loop stops? I'm using millis() in order to set one counter to 0. At the moment ESP32 plugged to serial monitor about ... More on forum.arduino.cc
🌐 forum.arduino.cc
5
0
March 8, 2021
programming - How can I handle the millis() rollover? - Arduino Stack Exchange
I need to read a sensor every five minutes, but since my sketch also has other tasks to do, I cannot just delay() between the readings. There is the Blink without delay tutorial suggesting I code a... More on arduino.stackexchange.com
🌐 arduino.stackexchange.com
June 12, 2015
What is exact value for millis() rollover?
Since I don't want to wait 50 days - Does the Arduino clock rollover to 0 after millis() reaches 2,147,483,647 (maximum value of a signed long, 24.8 days), or 4,294,967,294 (maximum value of an unsigned long, 50 days)? While the documentation implies that the latter is the case, it seems strange, ... More on forum.arduino.cc
🌐 forum.arduino.cc
0
0
October 4, 2006
🌐
Best Microcontroller Projects
best-microcontroller-projects.com › home › arduino hardware › arduino millis
Secrets of Arduino millis: How it works and how to use it.
Minutes and Seconds and Days can only have a maximum value below 255 you can use type uint8_t for each of the above quantities. uint8_t is the same as "unsigned char". Lets just say at the start of this discussion - "Don't Use delay()". This ...
🌐
The Robotics Back-End
roboticsbackend.com › home › arduino millis() vs micros()
Arduino millis() vs micros() - The Robotics Back-End
October 11, 2021 - On 16 bits Arduino (Uno, Mega, etc), an unsigned long is stored on 4 bytes, or 32 bits. Values for the variable ranges from 0 to 4,294,967,295 (2^32 – 1). It’s important to know that. Why? Because when you keep incrementing a variable, it will eventually reach the maximum possible value.
🌐
Bald Engineer
baldengineer.com › home › arduino: how do you reset millis() ?
Arduino: How do you reset millis() ? - Bald Engineer
March 31, 2022 - When millis() is called, the value of that variable is returned. The data type used is an unsigned long which is 4-bytes or 32-bits. This means the maximum value it can hold is 4,294,967,295.
🌐
Programming Electronics Academy
programmingelectronics.com › home › millis() arduino function: 5+ things to consider
millis() Arduino function: 5+ things to consider - Programming Electronics Academy
April 6, 2023 - ... That’s 343 in dog days! You may hear the jargon for when it reaches this maximum number and starts over as “rolling over”, but a more proper way to say it is the timer “overflows”. So how do we access this information?
Find elsewhere
🌐
Arduino
arduino.cc › reference › en › language › functions › time › millis
millis() | Arduino Documentation
October 31, 2023 - The return value for · millis() is of type · unsigned long; logic errors may occur if a programmer tries to do arithmetic with smaller data types such as · int. Even signed · long may encounter errors as its maximum value is half that of its unsigned counterpart.
🌐
AranaCorp
aranacorp.com › blog › tutorials › using the millis() function of the arduino ide
Using the millis() function of the Arduino IDE • AranaCorp
April 23, 2023 - The millis() function takes no ... The value is unsigned long (4-bytes or 32-bits). The maximum value it can take is 4,294,967,295 or 49 days....
🌐
Norwegian Creations
norwegiancreations.com › 2018 › 10 › arduino-tutorial-avoiding-the-overflow-issue-when-using-millis-and-micros
Arduino Tutorial: Avoiding the Overflow Issue When Using millis() and micros() – Norwegian Creations
October 11, 2018 - Since both inputs to the calculation are of the unsigned long data type, the answer will also be an unsigned long, and thus the result will overflow in line with the return value of millis(). Remember that both millis() and micros() return unsigned ...
🌐
Circuit Geeks
circuitgeeks.com › home › arduino millis() and micros()
Arduino millis() and micros()
March 2, 2022 - Arduino millis() count the time in milliseconds and we can store that data in an unsigned long variable. The maximum value of an unsigned long variable can be 232 – 1 or 4,294,967,295. And there are 1,000 milliseconds in a second.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Question about ESP32 millis() counter? - Programming - Arduino Forum
March 8, 2021 - Hello, I have this kind of question. Is there any limitation about max millis() counter? I mean does it matter if currenttime in millis counts up certain value and the whole loop stops? I'm using millis() in order to set one counter to 0. At the moment ESP32 plugged to serial monitor about 23hours ticking, the millis() was working fine.
🌐
Arduino Getting Started
arduinogetstarted.com › reference › arduino-millis
millis() | Arduino Getting Started
3 days ago - const unsigned long TIME_INTERVAL = 1000; unsigned long previousMillis; void setup() { Serial.begin(9600); previousMillis = millis(); } void loop() { if (millis() - previousMillis >= TIME_INTERVAL) { previousMillis = millis(); Serial.println("Arduino References"); } } ... Please note that the return value for millis() is of type unsigned long, logic errors may occur if a programmer tries to do arithmetic with smaller data types such as int. Even signed long may encounter errors as its maximum value is half that of its unsigned counterpart.
Top answer
1 of 4
187

Short answer: do not try to “handle” the millis rollover, write rollover-safe code instead. Your example code from the tutorial is fine. If you try to detect the rollover in order to implement corrective measures, chances are you are doing something wrong. Most Arduino programs only have to manage events that span relatively short durations, like debouncing a button for 50 ms, or turning a heater on for 12 hours... Then, and even if the program is meant to run for years at a time, the millis rollover should not be a concern.

The correct way to manage (or rather, avoid having to manage) the rollover problem is to think of the unsigned long number returned by millis() in terms of modular arithmetics. For the mathematically inclined, some familiarity with this concept is very useful when programming. You can see the math in action in Nick Gammon's article millis() overflow ... a bad thing?. For the problem at hand, what's important to know is that in modular arithmetics the numbers “wrap around” when reaching a certain value – the modulus – so that 1 − modulus is not a negative number but 1 (think of a 12 hour clock where the modulus is 12: here 1 − 12 = 1).

For those who do not want to go through the computational details, I offer here an alternative (hopefully simpler) way of thinking about it. It is based on the simple distinction between instants and durations. As long as your tests only involve comparing durations, you should be fine.

Note on micros(): Everything said here about millis() applies equally to micros(), except for the fact that micros() rolls over every 71.6 minutes, and the setMillis() function provided below does not affect micros().

Instants, timestamps and durations

When dealing with time, we have to make the distinction between at least two different concepts: instants and durations. An instant is a point on the time axis. A duration is the length of a time interval, i.e. the distance in time between the instants that define the start and the end of the interval. The distinction between these concepts is not always very sharp in everyday language. For example, if I say “I will be back in five minutes”, then “five minutes” is the estimated duration of my absence, whereas “in five minutes” is the instant of my predicted coming back. Keeping the distinction in mind is important, because it is the simplest way to entirely avoid the rollover problem.

The return value of millis() could be interpreted as a duration: the time elapsed from the start of the program until now. This interpretation, however, breaks down as soon as millis overflows. It is generally far more useful to think of millis() as returning a timestamp, i.e. a “label” identifying a particular instant. It could be argued that this interpretation suffers from these labels being ambiguous, as they are reused every 49.7 days. This is, however, seldom a problem: in most embedded applications, anything that happened 49.7 days ago is ancient history we do not care about. Thus, recycling the old labels should not be an issue.

Do not compare timestamps

Trying to find out which among two timestamps is greater than the other does not make sense. Example:

unsigned long t1 = millis();
delay(3000);
unsigned long t2 = millis();
if (t2 > t1) { ... }

Naively, one would expect the condition of the if () to be always true. But it will actually be false if millis overflows during delay(3000). Thinking of t1 and t2 as recyclable labels is the simplest way to avoid the error: the label t1 has clearly been assigned to an instant prior to t2, but in 49.7 days it will be reassigned to a future instant. Thus, t1 happens both before and after t2. This should make clear that the expression t2 > t1 makes no sense.

But, if these are mere labels, the obvious question is: how can we do any useful time calculations with them? The answer is: by restricting ourselves to the only two calculations that make sense for timestamps:

  1. later_timestamp - earlier_timestamp yields a duration, namely the amount of time elapsed between the earlier instant and the later instant. This is the most useful arithmetic operation involving timestamps.
  2. timestamp ± duration yields a timestamp which is some time after (if using +) or before (if −) the initial timestamp. Not as useful as it sounds, since the resulting timestamp can be used in only two kinds of calculations...

Thanks to modular arithmetics, both of these are guaranteed to work fine across the millis rollover, at least as long as the delays involved are shorter than 49.7 days.

Comparing durations is fine

A duration is just the amount of milliseconds elapsed during some time interval. As long as we do not need to handle durations longer than 49.7 days, any operation that physically makes sense should also make sense computationally. We can, for example, multiply a duration by a frequency to get a number of periods. Or we can compare two durations to know which one is longer. For example, here are two alternative implementations of delay(). First, the buggy one:

void myDelay(unsigned long ms) {          // ms: duration
    unsigned long start = millis();       // start: timestamp
    unsigned long finished = start + ms;  // finished: timestamp
    for (;;) {
        unsigned long now = millis();     // now: timestamp
        if (now >= finished)              // comparing timestamps: BUG!
            return;
    }
}

And here is the correct one:

void myDelay(unsigned long ms) {              // ms: duration
    unsigned long start = millis();           // start: timestamp
    for (;;) {
        unsigned long now = millis();         // now: timestamp
        unsigned long elapsed = now - start;  // elapsed: duration
        if (elapsed >= ms)                    // comparing durations: OK
            return;
    }
}

Most C programmers would write the above loops in a terser form, like

while (millis() < start + ms) ;  // BUGGY version

and

while (millis() - start < ms) ;  // CORRECT version

Although they look deceptively similar, the timestamp/duration distinction should make clear which one is buggy and which one is correct.

What if I really need to compare timestamps?

Better try to avoid the situation. If it is unavoidable, there is still hope if it is known that the respective instants are close enough: closer than 24.85 days. Yes, our maximum manageable delay of 49.7 days just got cut in half.

The obvious solution is to convert our timestamp comparison problem into a duration comparison problem. Say we need to know whether instant t1 is before or after t2. We choose some reference instant in their common past, and compare the durations from this reference until both t1 and t2. The reference instant is obtained by subtracting a long enough duration from either t1 or t2:

unsigned long reference_instant = t2 - LONG_ENOUGH_DURATION;
unsigned long from_reference_until_t1 = t1 - reference_instant;
unsigned long from_reference_until_t2 = t2 - reference_instant;
if (from_reference_until_t1 < from_reference_until_t2)
    // t1 is before t2

This can be simplified as:

if (t1 - t2 + LONG_ENOUGH_DURATION < LONG_ENOUGH_DURATION)
    // t1 is before t2

It is tempting to simplify further into if (t1 - t2 < 0). Obviously, this does not work, because t1 - t2, being computed as an unsigned number, cannot be negative. This, however, although not portable, does work:

if ((signed long)(t1 - t2) < 0)  // works with gcc
    // t1 is before t2

The keyword signed above is redundant (a plain long is always signed), but it helps make the intent clear. Converting to a signed long is equivalent to setting LONG_ENOUGH_DURATION equal to 24.85 days. The trick is not portable because, according to the C standard, the result is implementation defined. But since the gcc compiler promises to do the right thing, it works reliably on Arduino. If we wish to avoid implementation defined behavior, the above signed comparison is mathematically equivalent to this:

#include <limits.h>

if (t1 - t2 > LONG_MAX)  // too big to be believed
    // t1 is before t2

with the only problem that the comparison looks backwards. It is also equivalent, as long as longs are 32-bits, to this single-bit test:

if ((t1 - t2) & 0x80000000)  // test the "sign" bit
    // t1 is before t2

The last three tests are actually compiled by gcc into the exact same machine code.

How do I test my sketch against the millis rollover

If you follow the precepts above, you should be all good. If you nevertheless want to test, add this function to your sketch:

#include <util/atomic.h>

void setMillis(unsigned long ms)
{
    extern unsigned long timer0_millis;
    ATOMIC_BLOCK (ATOMIC_RESTORESTATE) {
        timer0_millis = ms;
    }
}

and you can now time-travel your program by calling setMillis(destination). If you want it to go through the millis overflow over and over again, like Phil Connors reliving Groundhog Day, you can put this inside loop():

// 6-second time loop starting at rollover - 3 seconds
if (millis() - (-3000) >= 6000)
    setMillis(-3000);

The negative timestamp above (-3000) is implicitly converted by the compiler to an unsigned long corresponding to 3000 milliseconds before the rollover (it is converted to 4294964296).

What if I really need to track very long durations?

If you need to turn a relay on and turn it off three months later, then you really need to track the millis overflows. There are many ways to do so. The most straightforward solution may be to simply extend millis() to 64 bits:

uint64_t millis64() {
    static uint32_t low32, high32;
    uint32_t new_low32 = millis();
    if (new_low32 < low32) high32++;
    low32 = new_low32;
    return (uint64_t) high32 << 32 | low32;
}

This is essentially counting the rollover events, and using this count as the 32 most significant bits of a 64 bit millisecond count. For this counting to work properly, the function needs to be called at least once every 49.7 days. However, if it is only called once per 49.7 days, for some cases it is possible that the check (new_low32 < low32) fails and the code misses a count of high32. Using millis() to decide when to make the only call to this code in a single "wrap" of millis (a specific 49.7 day window) could be very hazardous, depending on how the time frames line up. For safety, if using millis() to determine when to make the only calls to millis64(), there should be at least two calls in every 49.7 day window.

Keep in mind, though, that 64 bit arithmetic is expensive on the Arduino. It may be worth to reduce the time resolution in order to stay at 32 bits.

2 of 4
39

TL;DR Short version:

An unsigned long is 0 to 4,294,967,295 (2^32 - 1).

So lets say previousMillis is 4,294,967,290 (5 ms before rollover), and currentMillis is 10 (10ms after rollover). Then currentMillis - previousMillis is actual 16 (not -4,294,967,280) since the result will be calculated as an unsigned long (which can't be negative, so itself will roll around). You can check this simply by:

Serial.println( ( unsigned long ) ( 10 - 4294967290 ) ); // 16

So the above code will work perfectly fine. The trick is to always calculate the time difference, and not compare the two time values.

🌐
DeepBlue
deepbluembedded.com › home › blog › arduino millis() function (timer vs delay) tutorial
Arduino millis() Function (Timer vs delay) Tutorial
August 17, 2023 - Those are some important notes that you need to know about the Arduino millis() function, so you can use it more efficiently in your projects. The maximum value for the Arduino millis() function is 232-1 which is 4,294,967,295.
🌐
Arduino Forum
forum.arduino.cc › forum 2005-2010 (read only) › software › syntax & programs
What is exact value for millis() rollover? - Syntax & Programs - Arduino Forum
October 4, 2006 - Since I don't want to wait 50 days - Does the Arduino clock rollover to 0 after millis() reaches 2,147,483,647 (maximum value of a signed long, 24.8 days), or 4,294,967,294 (maximum value of an unsigned long, 50 days)?
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Best Practice with millis()? - Programming - Arduino Forum
December 23, 2011 - I have seen code examples using millis 2 different ways and was wondering if one of these is better then the other. Method 1: unsigned long previousMillis = 0; unsigned long interval = 1000; void setup() { …