Pages: [1]   Go Down
Author Topic: Pushbutton-controlled blinking LED  (Read 2662 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am new to Arduino programming, and as an excercise in asynchronous digital I/O, I've extended the "Blinking LED without using delay" tutorial example to read input from a button.  I thought it might be helpful to others.

--Randy

/* Pushbutton-controlled blinking LED
 * ----------------------------------
 *
 * Reads a pushbutton to determine the rate at which the LED should
 * blink.  If you hold the pushbutton down for x milliseconds, the
 * LED will blink on and off every x milliseconds.
 *
 * Created 25 September 2006
 * Randy Cox
 *
 * This is an extension of one of the tutorial examples from the
 * Arduino website (http://www.arduino.cc/en/Main/LearnArduino):
 *
 * 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 ledValue = LOW;             // previous value of the LED
long ledStartTime = 0;          // will store last time LED was updated
long ledBlinkInterval = 1000;   // interval at which to blink (milliseconds)

int buttonPin = 2;              // an active-high momentary pushbutton on pin 2
int buttonValue = HIGH;         // the current state of the button
long buttonPressTime = 0;       // will store the time that the button was pressed


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

void loop()
{
  // Check to see if the button is pressed.  If so, mark the time.  When the
  // button is released, calculate how long it was pressed, and make the led
  // blink at the same rate.
  buttonValue = digitalRead(buttonPin);

  // button press
  if (buttonValue==LOW && buttonPressTime==0) {
    buttonPressTime = millis();  // capture the time of the press
  }

  // button release
  if (buttonValue==HIGH && buttonPressTime!=0) {
    ledBlinkInterval = millis() - buttonPressTime;  // set the new flash interval
    buttonPressTime = 0;                            // clear the button press 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() - ledStartTime > ledBlinkInterval) {
    ledStartTime = millis();   // remember the last time we blinked the LED

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

    digitalWrite(ledPin, ledValue);
  }
}
Logged

Pages: [1]   Go Up
Jump to: