The millis()-function

Hey there!

I am not sure if my understanding of the millis()-function is correct so maybe someone can help me out on this.

I am using this function to avoid delay() and to make sure that a certain function is called only twice a second. So I compare the "current millis()" minus the "previous millis()"to a given intervall variable. You all know that drill.

But: As the reference states, the value will overflow at some point and "go back to zero". So my understanding of this is, that I still can use millis() to fire off my timed function, even if the value of millis will overflow AND it does not produce any errors*. Did I get that correct?

Greetings - Christian

*) There will be no errors, since internally the millis() value is treated as a binary number on which the two's complement is used when a subtraction occurs.

I still can use millis() to fire off my timed function, even if the value of millis will overflow

Correct as long as you use unsigned long variables

UKHeliBob:
Correct as long as you use unsigned long variables

and subtraction to get the intervals.

Here is an example:

If previousMillis was taken just before rollover and has the value 4294967290 and the currentMillis is taken after rollover and has a value of 5. What do you think 5 - 4294967290 equals? If you guessed some negative number then you need to stop doing math like a human and do it like a computer. Remember, this 32 bit unsigned number rolls over too. So it equals 11, exactly the number of milliseconds between the two times.

The demo Several Things at a Time illustrates the use of millis() for timing.

...R