]> git.piffa.net Git - sketchbook_andrea/blob - 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
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 = 0;             // varabile per mantenere lo stato del led
12
13 long ultimoCambio = 0;  // Momento in cui e' stato attivato il PIN input
14 long debounceDelay = 100;    // Tempo di debounce
15
16 void setup() {
17   pinMode(buttonPin, INPUT);    // Set the switch pin as input
18   pinMode(led, OUTPUT);    
19   Serial.begin(9600);           // Set up serial communication at 9600bps
20   ultimoStato = digitalRead(buttonPin);   // read the initial state
21 }
22
23 void loop(){
24   statoAttuale = digitalRead(buttonPin);      // read input value and store it in var
25
26   if (statoAttuale == HIGH && statoAttuale != ultimoStato && millis() - ultimoCambio > debounceDelay ) {
27     // 
28     Serial.println("Button premuto");
29
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
34   } 
35
36   //  Serial.print("statoAttuale ");
37   //  Serial.println(statoAttuale);
38   //  Serial.println(ultimoStato);
39   //delay(400);
40   ultimoStato = statoAttuale;                 // save the new state in our variable
41   digitalWrite(led, ledStatus);      // setta il led allo stato richiesto
42
43
44 }
45
46
47
48
49
50
51
52