]> git.piffa.net Git - sketchbook_andrea/blobdiff - basic/buttons/debounce_2_and_contratto/debounce_2_and_contratto.ino
operatori + analog
[sketchbook_andrea] / basic / buttons / debounce_2_and_contratto / debounce_2_and_contratto.ino
diff --git a/basic/buttons/debounce_2_and_contratto/debounce_2_and_contratto.ino b/basic/buttons/debounce_2_and_contratto/debounce_2_and_contratto.ino
new file mode 100644 (file)
index 0000000..83230a9
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+   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 = 0;             // varabile per mantenere lo stato del led
+
+long ultimoCambio = 0;  // Momento in cui e' stato attivato il PIN input
+long debounceDelay = 100;    // Tempo di debounce
+
+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
+}
+
+void loop(){
+  statoAttuale = digitalRead(buttonPin);      // read input value and store it in var
+
+  if (statoAttuale == HIGH && statoAttuale != ultimoStato && millis() - ultimoCambio > debounceDelay ) {
+    // 
+    Serial.println("Button premuto");
+
+    ledStatus = !ledStatus ;    // Inverte lo stato del LED 
+    Serial.print("Stato del LED: ");  // DEBUG
+    Serial.println(ledStatus) ;
+    ultimoCambio = millis() ;    // Registra il tempo attuale
+  } 
+
+  //  Serial.print("statoAttuale ");
+  //  Serial.println(statoAttuale);
+  //  Serial.println(ultimoStato);
+  //delay(400);
+  ultimoStato = statoAttuale;                 // save the new state in our variable
+  digitalWrite(led, ledStatus);      // setta il led allo stato richiesto
+
+
+}
+
+
+
+
+
+
+
+