]> git.piffa.net Git - sketchbook_andrea/blob - basic/button_presses_LED_6/button_presses_LED_6.ino
blinks while
[sketchbook_andrea] / basic / button_presses_LED_6 / button_presses_LED_6.ino
1 /*
2  *  Alternating switch
3  */
4
5 int switchPin = 2;              // switch is connected to pin 2
6 int val;                        // variable for reading the pin status
7 int buttonState;                // variable to hold the last button state
8 int lightMode = 0;              // State of the light
9 int LED = 12;
10
11 void setup() {
12   pinMode(switchPin, INPUT_PULLUP);    // Set the switch pin as input
13   pinMode(LED, OUTPUT);
14
15   buttonState = digitalRead(switchPin);   // read the initial state
16
17 }
18
19
20 void loop(){
21   val = digitalRead(switchPin);      // read input value and store it in val
22   delay(100);                        // Debounce
23   if ((val != buttonState) && (val == HIGH)) {     // check if the button is pressed
24     lightMode = 1 -lightMode ;
25   }
26   digitalWrite(LED,lightMode);
27   buttonState = val;                 // save the new state in our variable
28 }
29
30
31