]> git.piffa.net Git - sketchbook_andrea/blob - basic/buttons/button_state_3/button_state_3.ino
buttons reordered
[sketchbook_andrea] / basic / buttons / button_state_3 / button_state_3.ino
1 /*
2  *  Alternating switch
3  */
4
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
9 void setup() {
10   pinMode(switchPin, INPUT);    // Set the switch pin as input
11
12   Serial.begin(9600);           // Set up serial communication at 9600bps
13   buttonState = digitalRead(switchPin);   // read the initial state
14 }
15
16
17 void loop(){
18   val = digitalRead(switchPin);      // read input value and store it in val
19
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");
25     }
26   }
27
28   buttonState = val;                 // save the new state in our variable
29 }