Tutorial.BlinkWithoutDelay History

Hide minor edits - Show changes to markup

March 01, 2013, at 05:36 PM by Roberto Guido - corrected typo about anode and cathode. Thanks to Salman Sheikh for alert
Changed lines 17-18 from:

To build the circuit, grab an LED and attach its long, positive leg (called the anode) to pin 13. Attach the short, negative leg (called the anode) to ground. Then plug your Arduino board into your computer, start the Arduino program, and enter the code below.

to:

To build the circuit, grab an LED and attach its long, positive leg (called the anode) to pin 13. Attach the short, negative leg (called the cathode) to ground. Then plug your Arduino board into your computer, start the Arduino program, and enter the code below.

Changed line 48 from:
to:
May 02, 2012, at 03:35 AM by Scott Fitzgerald -
Changed lines 35-36 from:
to:
November 16, 2011, at 04:04 AM by Scott Fitzgerald -
Changed lines 35-36 from:
to:
September 30, 2011, at 03:02 AM by Scott Fitzgerald -
Changed lines 35-36 from:
to:
February 06, 2011, at 02:57 PM by Tom Igoe -
Changed line 47 from:
  • Blink? -turn an LED on and off.
to:
  • Blink -turn an LED on and off.
September 23, 2010, at 10:29 PM by Christian Cerrito -
Changed lines 3-4 from:

Blink Without Delay

to:

Blink Without Delay

September 23, 2010, at 09:01 PM by Christian Cerrito -
Changed lines 32-33 from:

The code below uses the millis() function, a function that returns the number of milliseconds since the Arduino board started running its current program, to blink an LED.

to:

The code below uses the millis() function, a command that returns the number of milliseconds since the Arduino board started running its current program, to blink an LED.

September 23, 2010, at 09:00 PM by Christian Cerrito -
Changed lines 23-25 from:

to:

image developed using Fritzing. For more circuit examples, see the Fritzing project page

September 17, 2010, at 05:35 PM by Tom Igoe -
Changed lines 31-32 from:
to:
September 17, 2010, at 05:34 PM by Tom Igoe -
Changed lines 31-32 from:
to:
Changed lines 42-44 from:
to:
September 17, 2010, at 05:32 PM by Tom Igoe -
September 17, 2010, at 05:31 PM by Tom Igoe -
Changed lines 42-44 from:
to:
September 16, 2010, at 10:02 PM by Tom Igoe -
Changed lines 31-32 from:
to:
September 08, 2010, at 08:21 PM by Tom Igoe -
Changed lines 5-6 from:

Sometimes you need to blink an LED (or some other time sensitive function) at the same time as something else (like watching for a button press). That means you can't use delay(), or you'd stop everything else the program while the LED blinked. Here's some code that demonstrates how to blink the LED without using delay(). It keeps track of the last time it turned the LED on or off. Then, each time through loop() it checks if a sufficient interval has passed - if it has, it turns the LED off if it was on and vice-versa.

to:

Sometimes you need to do two things at once. For example you might want to blink an LED (or some other time-sensitive function) while reading a button press or other input. In this case, you can't use delay(), or you'd stop everything else the program while the LED blinked. The program might miss the button press if it happens during the delay(). This sketch demonstrates how to blink the LED without using delay(). It keeps track of the last time the Arduino turned the LED on or off. Then, each time through loop(), it checks if a long enough interval has passed. If it has, it toggles the LED on or off.

August 24, 2010, at 10:31 PM by Christian Cerrito -
Changed lines 7-8 from:

Hardware Required

to:

Hardware Required

August 24, 2010, at 10:30 PM by Christian Cerrito -
Added lines 7-11:

Hardware Required

  • Arduino Board
  • LED
August 24, 2010, at 08:03 PM by Christian Cerrito -
Deleted lines 39-40:
  • AnalogReadSerial - read a potentiometer, print the state out to the Serial Monitor
  • Analog Input - use a potentiometer to control an LED's brightness
August 24, 2010, at 08:00 PM by Christian Cerrito -
Changed lines 28-41 from:
to:
August 24, 2010, at 07:58 PM by Christian Cerrito -
Changed lines 23-24 from:

The code below uses the millis(), a function that returns the number of milliseconds since the Arduino board started running its current program, to blink an LED.

to:

The code below uses the millis() function, a function that returns the number of milliseconds since the Arduino board started running its current program, to blink an LED.

August 24, 2010, at 07:58 PM by Christian Cerrito -
Changed lines 23-24 from:

The code below uses the milliis()

to:

The code below uses the millis(), a function that returns the number of milliseconds since the Arduino board started running its current program, to blink an LED.

August 24, 2010, at 07:54 PM by Christian Cerrito -
Added lines 22-23:

The code below uses the milliis()

August 24, 2010, at 07:48 PM by Christian Cerrito -
Changed line 22 from:
div class=code
)
to:
August 24, 2010, at 07:47 PM by Christian Cerrito -
Added lines 12-13:

To build the circuit, grab an LED and attach its long, positive leg (called the anode) to pin 13. Attach the short, negative leg (called the anode) to ground. Then plug your Arduino board into your computer, start the Arduino program, and enter the code below.

August 24, 2010, at 07:45 PM by Christian Cerrito -
Changed lines 9-10 from:
to:
August 24, 2010, at 08:06 AM by Christian Cerrito -
Changed lines 20-21 from:
to:
August 24, 2010, at 08:06 AM by Christian Cerrito -
Changed lines 20-21 from:
to:
February 09, 2010, at 04:51 AM by Tom Igoe -
February 09, 2010, at 04:50 AM by Tom Igoe -
Changed lines 19-79 from:
to:
July 05, 2009, at 07:12 PM by Tom Igoe -
Changed line 80 from:
to:
July 05, 2009, at 07:09 PM by Tom Igoe -
Changed lines 19-21 from:

[@ /* Blink without Delay

to:
Changed lines 41-43 from:
 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
to:
 // constants won't change. Used here to 
 // set pin numbers:
 const int ledPin =  13;      // the number of the LED pin
Changed lines 45-48 from:
  The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
to:
 // Variables will change:
 int ledState = LOW;             // ledState used to set the LED
 long previousMillis = 0;        // will store last time LED was updated
Added lines 49-51:
 // 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)
Changed lines 53-56 from:
 created 2007
 by David A. Mellis
 modified 17 Jun 2009
 by Tom Igoe
to:
 void setup() {
   // set the digital pin as output:
   pinMode(ledPin, OUTPUT);      
 }
Added lines 58-60:
 void loop()
 {
   // here is where you'd put code that needs to be running all the time.
Changed lines 62-103 from:
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to // set pin numbers: const int ledPin = 13; // the number of the LED pin

// Variables will change: int ledState = LOW; // ledState used to set the LED long previousMillis = 0; // will store last time LED was updated

// 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)

void setup() {

  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      

}

void loop() {

  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, is the difference
  // between the current time and last time we blinked the LED bigger than
  // the interval at which we want to blink the LED.
  if (millis() - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = millis();   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }

} @]

to:
June 25, 2009, at 01:55 PM by Tom Igoe -
Added lines 7-17:

Circuit

Schematic:

click the image to enlarge

June 17, 2009, at 11:17 PM by Tom Igoe -
Changed lines 9-11 from:
 [=

int ledPin = 13; // LED connected to digital pin 13 int value = LOW; // previous value of the LED

to:

[@ /* Blink without Delay

 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.

  The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.

 created 2007
 by David A. Mellis
 modified 17 Jun 2009
 by Tom Igoe

 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to // set pin numbers: const int ledPin = 13; // the number of the LED pin

// Variables will change: int ledState = LOW; // ledState used to set the LED

Added lines 38-40:

// 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.

Changed lines 43-45 from:

void setup() {

  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
to:

void setup() {

  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      
Changed lines 56-60 from:
    previousMillis = millis();   // remember the last time we blinked the LED

    // if the LED is off turn it on and vice-versa.
    if (value == LOW)
      value = HIGH;
to:
    // save the last time you blinked the LED 
    previousMillis = millis();   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
Changed line 63 from:
      value = LOW;
to:
      ledState = LOW;
Changed lines 65-66 from:
    digitalWrite(ledPin, value);
to:
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
Changed line 69 from:
 =]
to:

@]

December 20, 2007, at 06:41 AM by David A. Mellis -
Changed lines 7-8 from:

Code

to:

Code

December 20, 2007, at 06:41 AM by David A. Mellis -
Changed lines 1-2 from:

Blinking an LED without using the delay() function.

to:

Examples > Digital I/O

Blink Without Delay

December 20, 2007, at 06:40 AM by David A. Mellis -
Deleted lines 7-18:

/* Blinking LED without using delay

 * --------------------------------
 *
 * turns on and off a light emitting diode(LED) connected to a digital  
 * pin, without using the delay() function.  this means that other code
 * can run at the same time without being interrupted by the LED code.
 *
 * Created 14 February 2006
 * David A. Mellis
 * http://arduino.berlios.de
 */
May 09, 2006, at 12:14 PM by David A. Mellis - int -> long
Deleted line 20:

int previousMillis = 0; // will store last time LED was updated

Changed lines 22-23 from:

int interval = 1000; // interval at which to blink (milliseconds)

to:

long previousMillis = 0; // will store last time LED was updated long interval = 1000; // interval at which to blink (milliseconds)

February 14, 2006, at 06:08 PM by 85.18.81.162 -
Added lines 1-49:

Blinking an LED without using the delay() function.

Sometimes you need to blink an LED (or some other time sensitive function) at the same time as something else (like watching for a button press). That means you can't use delay(), or you'd stop everything else the program while the LED blinked. Here's some code that demonstrates how to blink the LED without using delay(). It keeps track of the last time it turned the LED on or off. Then, each time through loop() it checks if a sufficient interval has passed - if it has, it turns the LED off if it was on and vice-versa.

Code

 
/* Blinking LED without using delay
 * --------------------------------
 *
 * turns on and off a light emitting diode(LED) connected to a digital  
 * pin, without using the delay() function.  this means that other code
 * can run at the same time without being interrupted by the LED code.
 *
 * Created 14 February 2006
 * David A. Mellis
 * http://arduino.berlios.de
 */

int ledPin = 13;                // LED connected to digital pin 13
int previousMillis = 0;         // will store last time LED was updated
int value = LOW;                // previous value of the LED
int interval = 1000;            // interval at which to blink (milliseconds)

void setup()
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, is the difference
  // between the current time and last time we blinked the LED bigger than
  // the interval at which we want to blink the LED.
  if (millis() - previousMillis > interval) {
    previousMillis = millis();   // remember the last time we blinked the LED

    // if the LED is off turn it on and vice-versa.
    if (value == LOW)
      value = HIGH;
    else
      value = LOW;

    digitalWrite(ledPin, value);
  }
}
 

Share