2 * Turn on / off LED with a switch.
3 When the lightmode is on the LED Blinks
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
14 pinMode(switchPin, INPUT_PULLUP); // Set the switch pin as input
17 buttonState = digitalRead(switchPin); // read the initial state
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
27 if ((val == valBounce) && (val != buttonState) && (val == HIGH)) { // check if the button is pressed
28 lightMode = 1 -lightMode ; // Now with DeBounce
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
38 digitalWrite(LED,LOW); // Almayes turn off the LED
39 // As lightMode is FALSE == 0 turn the LED off
41 buttonState = val; // save the new state in our variable