]> git.piffa.net Git - sketchbook_andrea/blobdiff - basic/buttons/button_state_3/button_state_3.ino
Buttons and serial
[sketchbook_andrea] / basic / buttons / button_state_3 / button_state_3.ino
index de29a13e734426c2d95b2c66c419f7f1b11bfa6d..2bc683b8a3bbd11eb90cf7a56ce9a79c680877a0 100644 (file)
@@ -1,29 +1,34 @@
 /*
- *  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 statoAttuale;                        // variable for reading the pin status
+int ultimoStato;                // variable to hold the last button state
 
 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);      // 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
+      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;                 // save the new state in our variable
 }
+