]> git.piffa.net Git - sketchbook_andrea/blob - basic/buttons/button_state_3/button_state_3.ino
PWM: errata and states
[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 connesso al pin 2
9 int statoAttuale;               // Variabile per leggere lo stato del bottone
10 int ultimoStato;                // Variabile per registrare l'ultimo stato del bottone
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);      // Legge lo stato del bottone e lo resistra in val
22    delay(20)                                // riduce l'effetto bounce
23   if (statoAttuale != ultimoStato) {          // lo stato del bottone e' cambiato
24     if (statoAttuale == HIGH) {               // il bottone e' stato premuto
25       Serial.println("Bottone premuto");
26     } 
27     else {                         // the button is -not- pressed...
28       Serial.println("Bottone rilasciato");
29     }
30   }
31
32   ultimoStato = statoAttuale;                 // Aggiorna lo stato finale al valore attuale
33 }
34