X-Git-Url: http://git.piffa.net/web?a=blobdiff_plain;f=basic%2Fbuttons%2Fbutton_state_4_state_and_condition%2Fbutton_state_4_state_and_condition.ino;h=5ac087927d40dbc49e4b345791375a3ffde06fca;hb=2833f00daaa67fa16122dc20873a1f00a346abf8;hp=3cbc8698f4e6c681bf19b20e22c57667e704ca64;hpb=e50f2cf8e7402ea56cd01835be9f88c53876bfd1;p=sketchbook_andrea diff --git a/basic/buttons/button_state_4_state_and_condition/button_state_4_state_and_condition.ino b/basic/buttons/button_state_4_state_and_condition/button_state_4_state_and_condition.ino index 3cbc869..5ac0879 100644 --- a/basic/buttons/button_state_4_state_and_condition/button_state_4_state_and_condition.ino +++ b/basic/buttons/button_state_4_state_and_condition/button_state_4_state_and_condition.ino @@ -11,7 +11,7 @@ int ultimoStato; // Variabile per registrare l'ultimo stato del botton int ledStatus; // varabile per mantenere lo stato del led void setup() { - pinMode(buttonPin, INPUT); // Set the switch pin as input + pinMode(buttonPin, INPUT); // Set the switch pin as input pinMode(led, OUTPUT); Serial.begin(9600); // Attiva la comunicazione seriale a 9600bps ultimoStato = digitalRead(buttonPin); // Prima lettura del bottone @@ -20,7 +20,7 @@ void setup() { void loop(){ statoAttuale = digitalRead(buttonPin); // Legge lo stato del bottone e lo resistra in val - delay(20); // riduce l'effetto bounce + delay(20); // riduce l'effetto bounce if (statoAttuale != ultimoStato && statoAttuale == HIGH) { // due condizione contemporanee // lo stato del bottone e' camabiato AND lo stato attuale e' HIGH Serial.println("Button premuto"); @@ -39,3 +39,34 @@ void loop(){ +/* Domande: + + 1. La variabile ledstatus serve per tenere traccia dello stato del LED: + si potrebbe fare a meno di questa? + Cosa fa Arduino quando deve accendere o spegnere un LED? + Come funziona DigiralRead() ? + + . + . + . + . + . + . + . + . + . + . + . + . + . + . + . + Soluzione: + + 1. Per accendere o spegnere un LED Arduino imposta il valore del registro corrispondente + al PIN: se questo e' 0 il circuito e' aperto mentre se e' 1 il circuito e' chiuso. + Allo stesso modo con DigitalRead() e' possibile leggere lo stato di quel registro + e conoscere se il LED e' acceso o spento. + - https://www.arduino.cc/en/Reference/PortManipulation + - http://www.instructables.com/id/Microcontroller-Register-Manipulation/ + */