]> git.piffa.net Git - sketchbook_andrea/blob - basic/buttons/button_state_4_state_and_condition/button_state_4_state_and_condition.ino
42fd3a05f82838fb705e564a14a41fd83261eeb5
[sketchbook_andrea] / basic / buttons / button_state_4_state_and_condition / button_state_4_state_and_condition.ino
1 /*
2    Stato di un bottone
3  
4  Legge lo stato di un input
5  
6  */
7 int led = 13;
8 int buttonPin = 2;              
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
12
13 void setup() {
14   pinMode(buttonPin, INPUT);    // Set the switch pin as input
15   pinMode(led, OUTPUT);    
16   Serial.begin(9600);           // Set up serial communication at 9600bps
17   ultimoStato = digitalRead(buttonPin);   // read the initial state
18   ledStatus = 0;
19 }
20
21 void loop(){
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");
27     
28       ledStatus = !ledStatus ;    // Inverte lo stato del LED 
29       // ledStatus = 1 - ledStatus ;    // Forma analoga
30       
31       Serial.print("Stato del LED: ");  // DEBUG
32       Serial.println(ledStatus) ;
33   }
34
35   ultimoStato = statoAttuale;                 // save the new state in our variable
36   digitalWrite(led, ledStatus);      // setta il led allo stato richiesto
37
38 }
39
40
41