4 Legge lo stato di un input
9 int statoAttuale; // variable for reading the pin status
10 int ultimoStato; // variable to hold the last button state
11 int ledStatus = 0; // varabile per mantenere lo stato del led
13 long ultimoCambio = 0; // Momento in cui e' stato attivato il PIN input
14 long debounceDelay = 100; // Tempo di debounce
17 pinMode(buttonPin, INPUT); // Set the switch pin as input
19 Serial.begin(9600); // Set up serial communication at 9600bps
20 ultimoStato = digitalRead(buttonPin); // read the initial state
24 statoAttuale = digitalRead(buttonPin); // read input value and store it in var
26 if (statoAttuale == HIGH && statoAttuale != ultimoStato && millis() - ultimoCambio > debounceDelay ) {
28 Serial.println("Button premuto");
30 ledStatus = !ledStatus ; // Inverte lo stato del LED
31 Serial.print("Stato del LED: "); // DEBUG
32 Serial.println(ledStatus) ;
33 ultimoCambio = millis() ; // Registra il tempo attuale
36 // Serial.print("statoAttuale ");
37 // Serial.println(statoAttuale);
38 // Serial.println(ultimoStato);
40 ultimoStato = statoAttuale; // save the new state in our variable
41 digitalWrite(led, ledStatus); // setta il led allo stato richiesto