X-Git-Url: http://git.piffa.net/web?a=blobdiff_plain;f=basic%2Fbuttons%2Fbutton_state_3%2Fbutton_state_3.ino;h=7eee7e4a1df9a87ab3844038f2c03defbf23c130;hb=fa3554ca7f433786f4f65bd266f0a356a89bed0b;hp=de29a13e734426c2d95b2c66c419f7f1b11bfa6d;hpb=6a0496fdc97f180319df0a3270d1a78920e7ec32;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 de29a13..7eee7e4 100644 --- a/basic/buttons/button_state_3/button_state_3.ino +++ b/basic/buttons/button_state_3/button_state_3.ino @@ -1,29 +1,37 @@ /* - * Alternating switch + Stato di un bottone + + Legge lo stato di un input + */ -int switchPin = 2; // switch is connected to pin 2 -int val; // variable for reading the pin status -int buttonState; // variable to hold the last button state +int switchPin = 2; // switch connesso al pin 2 + // Nota: le prossime due variabili sono + // solo "dichiarate" e non "definite" +int statoAttuale; // Variabile per leggere lo stato del bottone +int ultimoStato; // Variabile per registrare l'ultimo stato del bottone void setup() { pinMode(switchPin, INPUT); // Set the switch pin as input Serial.begin(9600); // Set up serial communication at 9600bps - buttonState = digitalRead(switchPin); // read the initial state + ultimoStato = digitalRead(switchPin); // read the initial state } void loop(){ - val = digitalRead(switchPin); // read input value and store it in val - - if (val != buttonState) { // the button state has changed! - if (val == LOW) { // check if the button is pressed - Serial.println("Button just pressed"); - } else { // the button is -not- pressed... - Serial.println("Button just released"); + statoAttuale = digitalRead(switchPin); // Legge lo stato del bottone e lo resistra in val + 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... + Serial.println("Bottone rilasciato"); } } - buttonState = val; // save the new state in our variable + ultimoStato = statoAttuale; // Aggiorna lo stato finale al valore attuale } +