Go Down

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

savila

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?

Koepel

#1
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

savila


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy