Hi.
I have a sketch that deals with time intervals and I and would like to test if I have coded everything correctly. So I'd like to test the behavior when the number of milliseconds rolls over (when it exceeds `4294967295 and restarts from 0, which happens after about 49 days since startup).
Is there a way to test without waiting 49 days ?
I have found that for Arduino boards the number of millis can be forced as follows:
You could use the ESP32's 64-bit timer capability. That will give you microsecond-resolution and you won't have to worry about rollover for more than 290,000 years.
If you use this calculation the roll-over of millis() is handled the right way automatically
define all variables that deal with millis() as unsigned long. Because the return-value of function millis() is of type unsigned long
unsigned long currentMillis;
unsigned long myInterval;
unsigned long startMillis;
if (currentMillis - startMillis >= myInterval) {
// time for timed action
}
The integer-math of unsigned integers handles rollover automatically right.
you are doing the math in the if-conditions with variables of type unsigned long.
This means you can do the test with variables that you assign a value short before rollover
currentMillis = 200; // currentMillis short after rollover
startMillis = 4294967295 - 100; // snapshot of time short before rollover
serial.println(currentMillis - startMillis);
I did some research trying to find the library that defines the function millis() for ESP32 but had no success so far
Thank you for all your answers. At the end I decided to use the esp_timer_get_time() function and use micros. I also changed any variable used to store micros to the "long long" type.
Dario