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
13 pinMode(switchPin, INPUT_PULLUP); // Set the switch pin as input
16 buttonState = digitalRead(switchPin); // read the initial state
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
26 if ((val == valBounce) && (val != buttonState) && (val == HIGH)) { // check if the button is pressed
27 lightMode = 1 -lightMode ; // Now with DeBounce
29 digitalWrite(LED,lightMode);
30 buttonState = val; // save the new state in our variable