]> git.piffa.net Git - sketchbook_andrea/blob - advanced_projects/interrupts/debounce/debounce.ino
interrupts
[sketchbook_andrea] / advanced_projects / interrupts / debounce / debounce.ino
1 /*
2   Interrupts : input deboounce
3
4  Utilizzare un interrupt per intercettare
5  l'input di un interruttore momentaneo.
6
7  Debounce software nella funzione richiamata.
8  Per debounce hardware: http://www.all-electric.com/schematic/debounce.htm
9
10  */
11
12 int ledPin = 13;
13 volatile int state = LOW;
14
15 void setup()
16 {
17     pinMode(ledPin, OUTPUT);
18     pinMode(2, INPUT_PULLUP);
19     attachInterrupt(0, blink, FALLING);
20 }
21
22 void loop()
23 {
24     //digitalWrite(ledPin, state);
25     delay(10000); // Mette in pausa Arduino per 10sec
26 }
27
28 // Funzioni
29 void blink()
30 // Modifica dello stato del LED
31 {
32     static unsigned long last_interrupt_time = 0;
33     // This is not supposed to be global
34     // as it's olny accesed inside this ISR
35
36     // If interrupts come faster than 20ms, assume it's a bounce and ignore
37     if (millis() - last_interrupt_time > 20)
38     {   // Azione da intraprendere
39         state = !state;
40         digitalWrite(ledPin, state);
41         last_interrupt_time = millis();
42     }
43 }
44