X-Git-Url: http://git.piffa.net/web?a=blobdiff_plain;f=interrupts%2Finput_interrupt_1_debounce%2Finput_interrupt_1_debounce.ino;fp=interrupts%2Finput_interrupt_1_debounce%2Finput_interrupt_1_debounce.ino;h=936134523e4221c9bf9aa7ae01ffa25203f171d7;hb=844a1b814024ee2fe78ff473a380fe16183231f6;hp=0000000000000000000000000000000000000000;hpb=54a478908b8865add69e24463a1c87aee2184b6c;p=sketchbook_andrea diff --git a/interrupts/input_interrupt_1_debounce/input_interrupt_1_debounce.ino b/interrupts/input_interrupt_1_debounce/input_interrupt_1_debounce.ino new file mode 100644 index 0000000..9361345 --- /dev/null +++ b/interrupts/input_interrupt_1_debounce/input_interrupt_1_debounce.ino @@ -0,0 +1,41 @@ +/* + Interrupts : input deboounce + + Utilizzare un interrupt per intercettare + l'input di un interruttore momentaneo. + + Debounce software nella funzione richiamata. + Per debounce hardware: http://www.all-electric.com/schematic/debounce.htm + + */ + +int ledPin = 13; +volatile int state = LOW; + +void setup() +{ + pinMode(ledPin, OUTPUT); + pinMode(2, INPUT_PULLUP); + attachInterrupt(0, blink, FALLING); +} + +void loop() +{ + //digitalWrite(ledPin, state); + delay(10000); // Mette in pausa Arduino per 10sec +} + +// Funzioni +void blink() +// Modifica dello stato del LED +{ + static unsigned long last_interrupt_time = 0; + // If interrupts come faster than 200ms, assume it's a bounce and ignore + if (millis() - last_interrupt_time > 200) + { // Azione da intraprendere + state = !state; + digitalWrite(ledPin, state); + } + last_interrupt_time = millis(); +} +