1 // Example 03C: Turn on LED when the button is pressed
2 // and keep it on after it is released
3 // including simple de-bouncing
4 // Now with another new and improved formula!!
10 // the input pin where the
11 // pushbutton is connected
13 // val will be used to store the state
15 int old_val = 0; // this variable stores the previous
18 // 0 = LED off and 1 = LED on
21 // tell Arduino LED is an output
22 pinMode(BUTTON, INPUT); // and BUTTON is an input
25 val = digitalRead(BUTTON); // read input value and store it
27 // check if there was a transition
28 if ((val == HIGH) && (old_val == LOW)){
32 old_val = val; // val is now old, let's store it
34 digitalWrite(LED, HIGH); // turn LED ON
37 digitalWrite(LED, LOW);