/* Button & LED Test with variables and button debounce Turns on an LED on Pin 13 when button on Pin 2 is pressed and LED stays on until button is pressed again (with debounce). Created FEB-23, 2012 by Lars Wictorsson LAWICEL AB, http://www.lawicel-shop.se This example code is in the public domain. */ int LEDpin = 13; // LED is attached on I/O pin #13 int BTNpin = 2; // Button is attached on I/O pin #2 boolean LEDstatus = false; // Current LED status, false = OFF, true = ON void setup() { // initialize the digital pin as an output. // Pin 13 has an LED connected on most Arduino boards. pinMode(LEDpin, OUTPUT); // initialize the digital pin as an input. // Pin 2 is connected to a button tied to GND with external pullup 10K to VCC. pinMode(BTNpin, INPUT); } void loop() { if (digitalRead(BTNpin) == LOW) { // Check if button is pressed down LEDstatus = !LEDstatus; // Invert LED status digitalWrite(LEDpin, LEDstatus); // Update actual LED to current status delay(150); // Debounce delay 150ms while (digitalRead(BTNpin) == LOW); // Wait until button is released } }