Pushbutton-controlled blinking LED
i new arduino programming, , excercise in asynchronous digital i/o, i've extended "blinking led without using delay" tutorial example read input button. i thought might helpful others.
--randy
/* pushbutton-controlled blinking led
* ----------------------------------
*
* reads pushbutton determine rate @ led should
* blink. if hold pushbutton down x milliseconds, the
* led blink on , off every x milliseconds.
*
* created 25 september 2006
* randy cox
*
* extension of 1 of tutorial examples
* arduino website (http://www.arduino.cc/en/main/learnarduino):
*
* blinking led without using delay
* --------------------------------
*
* turns on , off light emitting diode(led) connected digital
* pin, without using delay() function. this means other code
* can run @ same time without being interrupted led code.
*
* created 14 february 2006
* david a. mellis
* http://arduino.berlios.de
*/
int ledpin = 13; // led connected digital pin 13
int ledvalue = low; // previous value of led
long ledstarttime = 0; // store last time led updated
long ledblinkinterval = 1000; // interval @ blink (milliseconds)
int buttonpin = 2; // active-high momentary pushbutton on pin 2
int buttonvalue = high; // current state of button
long buttonpresstime = 0; // store time button pressed
void setup()
{
pinmode(ledpin, output); // sets digital pin output
}
void loop()
{
// check see if button pressed. if so, mark time. when the
// button released, calculate how long pressed, , make led
// blink @ same rate.
buttonvalue = digitalread(buttonpin);
// button press
if (buttonvalue==low && buttonpresstime==0) {
buttonpresstime = millis(); // capture time of press
}
// button release
if (buttonvalue==high && buttonpresstime!=0) {
ledblinkinterval = millis() - buttonpresstime; // set new flash interval
buttonpresstime = 0; // clear button press time
}
// check see if it's time blink led; is, difference
// between current time , last time blinked led bigger than
// interval @ want blink led.
if (millis() - ledstarttime > ledblinkinterval) {
ledstarttime = millis(); // remember last time blinked led
// if led off turn on , vice-versa.
if (ledvalue == low)
ledvalue = high;
else
ledvalue = low;
digitalwrite(ledpin, ledvalue);
}
}
--randy
/* pushbutton-controlled blinking led
* ----------------------------------
*
* reads pushbutton determine rate @ led should
* blink. if hold pushbutton down x milliseconds, the
* led blink on , off every x milliseconds.
*
* created 25 september 2006
* randy cox
*
* extension of 1 of tutorial examples
* arduino website (http://www.arduino.cc/en/main/learnarduino):
*
* blinking led without using delay
* --------------------------------
*
* turns on , off light emitting diode(led) connected digital
* pin, without using delay() function. this means other code
* can run @ same time without being interrupted led code.
*
* created 14 february 2006
* david a. mellis
* http://arduino.berlios.de
*/
int ledpin = 13; // led connected digital pin 13
int ledvalue = low; // previous value of led
long ledstarttime = 0; // store last time led updated
long ledblinkinterval = 1000; // interval @ blink (milliseconds)
int buttonpin = 2; // active-high momentary pushbutton on pin 2
int buttonvalue = high; // current state of button
long buttonpresstime = 0; // store time button pressed
void setup()
{
pinmode(ledpin, output); // sets digital pin output
}
void loop()
{
// check see if button pressed. if so, mark time. when the
// button released, calculate how long pressed, , make led
// blink @ same rate.
buttonvalue = digitalread(buttonpin);
// button press
if (buttonvalue==low && buttonpresstime==0) {
buttonpresstime = millis(); // capture time of press
}
// button release
if (buttonvalue==high && buttonpresstime!=0) {
ledblinkinterval = millis() - buttonpresstime; // set new flash interval
buttonpresstime = 0; // clear button press time
}
// check see if it's time blink led; is, difference
// between current time , last time blinked led bigger than
// interval @ want blink led.
if (millis() - ledstarttime > ledblinkinterval) {
ledstarttime = millis(); // remember last time blinked led
// if led off turn on , vice-versa.
if (ledvalue == low)
ledvalue = high;
else
ledvalue = low;
digitalwrite(ledpin, ledvalue);
}
}
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Pushbutton-controlled blinking LED
arduino
Comments
Post a Comment