X-Git-Url: http://git.piffa.net/web?a=blobdiff_plain;f=basic%2Fbuttons%2Fbutton_state_3%2Fbutton_state_3.ino;h=2afa6d9af30a8744c98ec9b9f29611271e8ef7b5;hb=HEAD;hp=c008ff182246fef81fa9b36f26642d5e46f0842b;hpb=e50f2cf8e7402ea56cd01835be9f88c53876bfd1;p=sketchbook_andrea diff --git a/basic/buttons/button_state_3/button_state_3.ino b/basic/buttons/button_state_3/button_state_3.ino index c008ff1..2afa6d9 100644 --- a/basic/buttons/button_state_3/button_state_3.ino +++ b/basic/buttons/button_state_3/button_state_3.ino @@ -6,6 +6,8 @@ */ int switchPin = 2; // switch connesso al pin 2 + // Nota: le prossime due variabili sono + // solo "definite" e non "inizializzate" int statoAttuale; // Variabile per leggere lo stato del bottone int ultimoStato; // Variabile per registrare l'ultimo stato del bottone @@ -18,13 +20,15 @@ void setup() { void loop(){ - statoAttuale = digitalRead(switchPin); // Legge lo stato del bottone e lo resistra in val - // delay(20) // riduce l'effetto bounce - if (statoAttuale != ultimoStato) { // lo stato del bottone e' cambiato + statoAttuale = digitalRead(switchPin); // Legge lo stato del bottone e + // lo resistra nella variabile + delay(20); // riduce l'effetto bounce + if (statoAttuale != ultimoStato) { + // verifica due condizioni che devono realizzarsi contemporaneamente if (statoAttuale == HIGH) { // il bottone e' stato premuto Serial.println("Bottone premuto"); } - else { // the button is -not- pressed... + else { // il bottone non e' premuto... Serial.println("Bottone rilasciato"); } } @@ -32,3 +36,12 @@ void loop(){ ultimoStato = statoAttuale; // Aggiorna lo stato finale al valore attuale } +/* Domande: + + 1. Cosa succde se non uso un delay(20) alla lettura del bottone? + 2. Implementare un LED che cambia stato quando viene premuto il bottone. + 3. Quanti stati ha il LED? + 4. Sarebbe possibile passare rapidamente da uno stato all'altro? + + */ +