]> git.piffa.net Git - sketchbook_andrea/blob - basic/buttons/button_presses_LED_deBounce_7/button_presses_LED_deBounce_7.ino
buttons reordered
[sketchbook_andrea] / basic / buttons / button_presses_LED_deBounce_7 / button_presses_LED_deBounce_7.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 valBounce ;                 // variable for debouncing
8 int buttonState;                // variable to hold the last button state
9 int lightMode = 0;              // State of the light
10 int LED = 12;
11
12 void setup() {
13   pinMode(switchPin, INPUT_PULLUP);    // Set the switch pin as input
14   pinMode(LED, OUTPUT);
15
16   buttonState = digitalRead(switchPin);   // read the initial state
17
18 }
19
20
21 void loop(){
22   val = digitalRead(switchPin);      // read input value and store it in val
23   delay(10);                        // Debounce
24   valBounce = digitalRead(switchPin);      // read input value and store it in val
25
26   if ((val == valBounce) && (val != buttonState) && (val == HIGH)) {     // check if the button is pressed
27     lightMode = 1 -lightMode ;    // Now with DeBounce
28   }
29   digitalWrite(LED,lightMode);
30   buttonState = val;                 // save the new state in our variable
31 }
32
33
34
35