Skip to content

"Blink without delay" example sketch has bugs #1618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nickgammon opened this issue Oct 11, 2013 · 3 comments
Closed

"Blink without delay" example sketch has bugs #1618

nickgammon opened this issue Oct 11, 2013 · 3 comments
Labels
Component: Documentation Related to Arduino's documentation content

Comments

@nickgammon
Copy link

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)
@drewfish
Copy link

It might be worthwhile to include a comment expressing the best practice for handling time, something like

It is considered best practice to use `unsigned long` for variables
that hold time (seconds, milliseconds, or microsecond).

@nickgammon
Copy link
Author

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.

@StevenJGreenfield
Copy link

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 unsigned long. 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.

@shfitz shfitz closed this as completed Nov 11, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Documentation Related to Arduino's documentation content
Projects
None yet
Development

No branches or pull requests

4 participants