]> git.piffa.net Git - sketchbook_andrea/blob - basic/buttons/button_state_3/button_state_3.ino
2bc683b8a3bbd11eb90cf7a56ce9a79c680877a0
[sketchbook_andrea] / basic / buttons / button_state_3 / button_state_3.ino
1 /*
2    Stato di un bottone
3  
4  Legge lo stato di un input
5  
6  */
7
8 int switchPin = 2;              // switch is connected to pin 2
9 int statoAttuale;                        // variable for reading the pin status
10 int ultimoStato;                // variable to hold the last button state
11
12 void setup() {
13   pinMode(switchPin, INPUT);    // Set the switch pin as input
14
15   Serial.begin(9600);           // Set up serial communication at 9600bps
16   ultimoStato = digitalRead(switchPin);   // read the initial state
17 }
18
19
20 void loop(){
21   statoAttuale = digitalRead(switchPin);      // read input value and store it in val
22   // delay(20)                      // riduce leffetto bounce
23   if (statoAttuale != ultimoStato) {          // the button state has changed!
24     if (statoAttuale == HIGH) {                // check if the button is pressed
25       Serial.println("Bottone premuto");
26     } 
27     else {                         // the button is -not- pressed...
28       Serial.println("Bottone rilasciato");
29     }
30   }
31
32   ultimoStato = statoAttuale;                 // save the new state in our variable
33 }
34