Go Down

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

#### aklein9999

##### Sep 19, 2018, 07:21 pm
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 overflowsunsigned long timehelp2 = 4294967290;unsigned long prev_time;void setup() {Serial.begin(115200);prev_time = millis();}void loop() {//do something every secondif (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!

#### Robin2

#1
##### Sep 19, 2018, 07:34 pm
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

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

#### sterretje

#2
##### Sep 19, 2018, 07:36 pmLast Edit: Sep 19, 2018, 07:37 pm by sterretje
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.

#### aklein9999

#3
##### Sep 19, 2018, 07:45 pm
@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....

#### gfvalvo

#4
##### Sep 19, 2018, 07:57 pm
Code works fine for me. Uno (equivalent) processor and Arduino v1.8.5.

It's counted past 'Difference: 200', still running, nothing odd.

#### cattledog

#5
##### Sep 19, 2018, 08:05 pmLast Edit: Sep 19, 2018, 08:05 pm by cattledog

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.

#### aklein9999

#6
##### Sep 19, 2018, 08:32 pm
Ha!

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,

Alex

Go Up