Go Down

Topic: Using Millis for timing - strange behaviour during simulation (Read 201 times) previous topic - next topic


I wondered what will happen if I let an unsigned long integer overflow and subtract something from it.

This is something what people usually do to create a 'timer'.

For simplicity I didn't use millis(), instead of that I just used a long unsigned integer.

When I run the code, something strange happens after a few seconds (usually when the time difference was 61). Please run the code and have a look yourself. And even more strange, sometimes it happens after a different time span after reset.

Can anyone explain this? The sketch is running on an Arduino Uno.

Code: [Select]

unsigned long timehelp1 = 4294967293;  //unsigned long just before it overflows
unsigned long timehelp2 = 4294967290;
unsigned long prev_time;

void setup() {
prev_time = millis();

void loop() {
//do something every second
if (millis() - prev_time >= 1000)
  {prev_time = millis(); doit();}


void doit()
timehelp1 = timehelp1 + 1;
Serial.print("Millis: "); Serial.print(timehelp1); Serial.print("        "); Serial.print("Difference: "); Serial.println(timehelp1 - timehelp2);

Thank you!


This code will give the correct result even if millis() overflows
Code: [Select]
if (millis() - prevTime >= interval) {
assuming prevTime and interval are unsigned long.

If you just want to experiment with the effect of rollover using unsigned integer maths it will be much simpler with byte variables as they roll over after 255 steps

Two or three hours spent thinking and reading documentation solves most programming problems.


You will have to give a description of the problem; I do not immediately observe anything strange when running it.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.


@Robin2: I was just experimenting and I don't understand why I get the strange behaviour with my code

@sterretje: well, the timehelp1 variable is counting up to 4294967295, then it overflows and goes to 0, it counts up tp 55 and then suddenly jumps to 4294967294, then to 4294967295, and then of course to 0 again.

And sometimes it doesn't even cout up to 55 but just to 27 or another value before jumping to 4294967294....


Code works fine for me. Uno (equivalent) processor and Arduino v1.8.5.

It's counted past 'Difference: 200', still running, nothing odd.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.


I can't confirm your issue.

What do you mean by "simulation"? Are you running the code on an actual hardware Uno, or some sort of software simulator of an Arduino?

If you are on an actual Uno, perhaps there are resets going on due to power supply or USB issues.



I was running it on an Arduino Nano.

And this Nano Hardware was faulty.

Sorry for the trouble.

Thank you very much for all of your help,


Go Up