Arduino Forum

General Category => General Discussion => Topic started by: savila on Feb 08, 2016, 10:44 pm

Title: is there a bug in "blink without delay" scetch?
Post by: savila on Feb 08, 2016, 10:44 pm
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?
Title: Re: is there a bug in "blink without delay" scetch?
Post by: Koepel on Feb 08, 2016, 10:49 pm
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 (https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay)
Title: Re: is there a bug in "blink without delay" scetch?
Post by: savila on Feb 08, 2016, 10:51 pm
Thanks for the quick answer Koepel (y)