]> git.piffa.net Git - sketchbook_andrea/blobdiff - basic/buttons/button_state_3/button_state_3.ino
Sensors analog
[sketchbook_andrea] / basic / buttons / button_state_3 / button_state_3.ino
index 2bc683b8a3bbd11eb90cf7a56ce9a79c680877a0..2f43ec5751de1e3bb8fd024c4cd70c18dab00fa0 100644 (file)
@@ -5,9 +5,11 @@
  
  */
 
-int switchPin = 2;              // switch is connected to pin 2
-int statoAttuale;                        // variable for reading the pin status
-int ultimoStato;                // variable to hold the last button state
+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
 
 void setup() {
   pinMode(switchPin, INPUT);    // Set the switch pin as input
@@ -18,17 +20,27 @@ void setup() {
 
 
 void loop(){
-  statoAttuale = digitalRead(switchPin);      // read input value and store it in val
-  // delay(20)                      // riduce leffetto bounce
-  if (statoAttuale != ultimoStato) {          // the button state has changed!
-    if (statoAttuale == HIGH) {                // check if the button is pressed
+  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...
+    else {                                    // il bottone non e' premuto...
       Serial.println("Bottone rilasciato");
     }
   }
 
-  ultimoStato = statoAttuale;                 // save the new state in our variable
+  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?
+ */