]> git.piffa.net Git - sketchbook_andrea/blobdiff - basic/buttons/button_state_4_state/button_state_4_state.ino
Buttons and serial
[sketchbook_andrea] / basic / buttons / button_state_4_state / button_state_4_state.ino
diff --git a/basic/buttons/button_state_4_state/button_state_4_state.ino b/basic/buttons/button_state_4_state/button_state_4_state.ino
new file mode 100644 (file)
index 0000000..87ceffa
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+   Stato di un bottone
+ Legge lo stato di un input
+ */
+int led = 13;
+int buttonPin = 2;              
+int statoAttuale;                        // variable for reading the pin status
+int ultimoStato;                // variable to hold the last button state
+int ledStatus;             // varabile per mantenere lo stato del led
+
+void setup() {
+  pinMode(buttonPin, INPUT);    // Set the switch pin as input
+  pinMode(led, OUTPUT);    
+  Serial.begin(9600);           // Set up serial communication at 9600bps
+  ultimoStato = digitalRead(buttonPin);   // read the initial state
+  ledStatus = 0;
+}
+
+void loop(){
+  statoAttuale = digitalRead(buttonPin);      // read input value and store it in var
+  delay(20);                      // riduce l'effetto bounce
+  if (statoAttuale != ultimoStato) {          // the button state has changed!
+    if (statoAttuale == HIGH) {                // check if the button is pressed
+      Serial.println("Button premuto");
+     
+      ledStatus = !ledStatus ;    // Inverte lo stato del LED 
+      // ledStatus = 1 - ledStatus ;    // Forma analoga
+      
+      Serial.print("Stato del LED: ");  // DEBUG
+      Serial.println(ledStatus) ;
+    } 
+  }
+
+  ultimoStato = statoAttuale;                 // save the new state in our variable
+  digitalWrite(led, ledStatus);      // setta il led allo stato richiesto
+
+}
+
+
+