Go Down

Topic: is there a bug in "blink without delay" scetch? (Read 628 times) previous topic - next topic


Hy guys.

What happens with the popular example when the millis overflow:

Code: [Select]
if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

let say currentMillis get to 50 days - previousMillis=49 days >=interval 2 days.

then currentMillis overflow.

now we have (0-49 days>=2 day) and the script will wait for 49  days instead of interval of 2 days, correct?


Feb 08, 2016, 10:49 pm Last Edit: Feb 08, 2016, 10:52 pm by Koepel
That is the beauty of binary calculation. If two unsigned long variables are used, and one has rollover, and they are substracted, then the result is still the difference.
But they have to be unsigned long, if one of them is a "long", then it won't work. The variable of the interval however doesn't have to be unsigned long.

There are some test sketches on this forum to test it. I could not find them right now.

If you have a question about something, please give links to the origin. For example: Blink Without Delay : https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay


Go Up