Toggle A LED With a Switch
after playing around bit, ive come code toggles led based on press of button. im sure lot of code redundant , unnecessary, learn through trial , error, , worked me. included debounce library, because im using jumper wires in breadboard , not actual switch.
ive commented code best could. if have suggestions or ways have done better, please feel free tell me!
ive commented code best could. if have suggestions or ways have done better, please feel free tell me!
code: [select]
#include <debounce.h>
bool washigh = false;
bool beenlow = true;
int ledpin = 13;
int switchpin = 9;
debounce debouncer = debounce(20, switchpin);
void setup()
{
pinmode(ledpin,output);
pinmode(switchpin,input);
}
void loop()
{
debouncer.update(); // update debouncer
if ((debouncer.read() == high) && beenlow) // if switch has been pressed , has been released (beenlow == true)
{
if (washigh) // , if light high last time
{
digitalwrite(ledpin,low); // toggle light low
washigh = false; // define light not high
}
else // otherwise light low last time
{
digitalwrite(ledpin, high); // toggle light high
washigh = true; // , define light high
}
beenlow = false; // define switch hasnt been released, prevent light flashing loop runs
}
if (debouncer.read() == low) // if switch has been released (and pulled low pulldown resistor)
{
if (washigh) // , if light on
digitalwrite(ledpin,high); // keep on
else // otherwise light off
digitalwrite(ledpin,low); // keep off
beenlow = true; // define switch has been released
}
}
this way of doing it: http://www.arduino.cc/cgi-bin/yabb2/yabb.pl?num=1247178706/3#1
i think code looks fine
i think code looks fine

Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Toggle A LED With a Switch
arduino
Comments
Post a Comment