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
10 pinMode(switchPin, INPUT); // Set the switch pin as input
12 Serial.begin(9600); // Set up serial communication at 9600bps
13 buttonState = digitalRead(switchPin); // read the initial state
18 val = digitalRead(switchPin); // read input value and store it in val
20 if (val != buttonState) { // the button state has changed!
21 if (val == LOW) { // check if the button is pressed
22 Serial.println("Button just pressed");
23 } else { // the button is -not- pressed...
24 Serial.println("Button just released");
28 buttonState = val; // save the new state in our variable