//Turn on LED when button is pressed and keep it on after release //If LED is on and button is pressed turn it off #define LED 9 //the pin for the LED #define BUTTON 7 //the input pin where the push button is connected int val = 0; //integer variable "val" used to store the state of the input (button) pin int old_val = 0; //integer variable "old_val" used to store previous value of "val" int state = 0; //0 = LED off and 1 = LED on void setup() { pinMode(LED, OUTPUT); //tell Arduino LED is an output - the "load" of the circuit pinMode(BUTTON, INPUT); //tell Arduino BUTTON is the input } void loop() { val = digitalRead(BUTTON); //read input value and store it //check whether the input is HIGH (button pressed) if ((val == HIGH) && (old_val == LOW)) { state = 1 - state; delay(10); } old_val = val; //val is now old, store it if (state == 1) { digitalWrite(LED, HIGH); //turn LED on } else { digitalWrite(LED, LOW); //otherwise turn LED off } }