]> git.piffa.net Git - sketchbook_andrea/blob - basic/button_presses_LED_blinking_deBounce_8/button_presses_LED_blinking_deBounce_8.ino
3116b42970726cf2a7847dd2d15fcf759406a9a9
[sketchbook_andrea] / basic / button_presses_LED_blinking_deBounce_8 / button_presses_LED_blinking_deBounce_8.ino
1 /*
2  *  Turn on / off LED with a switch.
3  When the lightmode is on the LED Blinks
4  */
5
6 int switchPin = 2;              // switch is connected to pin 2
7 int val;                        // variable for reading the pin status
8 int valBounce ;                 // variable for debouncing
9 int buttonState;                // variable to hold the last button state
10 int lightMode = 0;              // State of the light
11 int LED = 12;
12
13 void setup() {
14   pinMode(switchPin, INPUT_PULLUP);    // Set the switch pin as input
15   pinMode(LED, OUTPUT);
16
17   buttonState = digitalRead(switchPin);   // read the initial state
18
19 }
20
21
22 void loop(){
23   val = digitalRead(switchPin);      // read input value and store it in val
24   delay(10);                        // Debounce
25   valBounce = digitalRead(switchPin);      // read input value and store it in val
26
27   if ((val == valBounce) && (val != buttonState) && (val == HIGH)) {     // check if the button is pressed
28     lightMode = 1 -lightMode ;    // Now with DeBounce
29   }
30   if (lightMode) {  // Check if light mode is TRUE == 1 or FALSE == 0
31     delay(50);               // Keep the LED LOW for 50ms 
32     digitalWrite(LED,HIGH);  // Blink the LED
33     delay(50);               // Keep the LED HIGH for 50ms
34     //  digitalWrite(LED,LOW); // We don't need to turn it LOW
35                                // It will go off anyway later 
36   }
37
38   digitalWrite(LED,LOW); // Almayes turn off the LED
39                          // As lightMode is FALSE == 0 turn the LED off
40                          // Turn it off
41   buttonState = val;                 // save the new state in our variable
42 }
43
44
45
46
47
48
49
50