You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The example sketch "Blink without delay" has bugs.
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
The variable previousMillis should be unsigned long.
if(currentMillis - previousMillis > interval) {
That should be:
if(currentMillis - previousMillis >= interval) {
I mention this because newbies are referred to this sketch dozens of times a day in the forum.
This comment is misleading:
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000; // interval at which to blink (milliseconds)
It is better to suggest unsigned long for time variables, to get beginners into the right method from the start.
Also, interval does not "quickly become a bigger number than can be stored in an int" because it in fact does not change.
Since interval does not change it should be const, and preferably:
const unsigned long interval = 1000; // interval at which to blink (milliseconds)
The text was updated successfully, but these errors were encountered:
To clarify my last point above, the variable interval does not need to be a long for the purpose for which it is being used. As long as it holds the desired value (in this case 1000) then any appropriate type would do - int for example, or unsigned int.
Well, to be clear, when only a value of 1000 is loaded into interval, it does not require a long, but if you were to assign any arbitrarily large value to it within the limits of millis() or micros(), interval really should be an unsignedlong. As drewfish suggests, with comment.
I might be able to claim bragging rights to being the one who first noticed, or at least first mentioned the bug of previousmillis on arduino.cc. Or maybe I just don't get out much.
The example sketch "Blink without delay" has bugs.
The variable previousMillis should be unsigned long.
That should be:
I mention this because newbies are referred to this sketch dozens of times a day in the forum.
This comment is misleading:
It is better to suggest unsigned long for time variables, to get beginners into the right method from the start.
Also, interval does not "quickly become a bigger number than can be stored in an int" because it in fact does not change.
Since interval does not change it should be const, and preferably:
The text was updated successfully, but these errors were encountered: