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 buttonPresses = 0; // Counter for the button
11 pinMode(switchPin, INPUT_PULLUP); // Set the switch pin as input
13 Serial.begin(9600); // Set up serial communication at 9600bps
14 buttonState = digitalRead(switchPin); // read the initial state
19 val = digitalRead(switchPin); // read input value and store it in val
20 delay(100); // Debounce
21 if ((val != buttonState) && (val == HIGH)) { // check if the button is pressed
23 Serial.print("Button has been pressed ");
24 Serial.print(buttonPresses);
25 Serial.println(" times.");
27 buttonState = val; // save the new state in our variable