]> git.piffa.net Git - sketchbook_andrea/blob - interrupts/input_interrupt_1_debounce/input_interrupt_1_debounce.ino
Common: aggiunto shift temporale per pwm e blink
[sketchbook_andrea] / interrupts / input_interrupt_1_debounce / input_interrupt_1_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   // If interrupts come faster than 200ms, assume it's a bounce and ignore
34   if (millis() - last_interrupt_time > 200)
35   { // Azione da intraprendere
36     state = !state;
37     digitalWrite(ledPin, state);
38   }
39   last_interrupt_time = millis();
40 }
41