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; // varabile per mantenere lo stato del led
14 pinMode(buttonPin, INPUT); // Set the switch pin as input
16 Serial.begin(9600); // Set up serial communication at 9600bps
17 ultimoStato = digitalRead(buttonPin); // read the initial state
22 statoAttuale = digitalRead(buttonPin); // read input value and store it in var
23 delay(20); // riduce l'effetto bounce
24 if (statoAttuale != ultimoStato && statoAttuale == HIGH) {
25 // the button state has changed AND the button is pressed
26 Serial.println("Button premuto");
28 ledStatus = !ledStatus ; // Inverte lo stato del LED
29 // ledStatus = 1 - ledStatus ; // Forma analoga
31 Serial.print("Stato del LED: "); // DEBUG
32 Serial.println(ledStatus) ;
35 ultimoStato = statoAttuale; // save the new state in our variable
36 digitalWrite(led, ledStatus); // setta il led allo stato richiesto