Switch Debouncing (oscillazione bottoni)
Dependencies: mbed
main.cpp@0:1dac861f0eae, 2016-11-06 (annotated)
- Committer:
- Mattinico
- Date:
- Sun Nov 06 12:44:28 2016 +0000
- Revision:
- 0:1dac861f0eae
k
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mattinico | 0:1dac861f0eae | 1 | /* Program Example 10: Event driven LED switching with switch debounce |
Mattinico | 0:1dac861f0eae | 2 | */ |
Mattinico | 0:1dac861f0eae | 3 | #include "mbed.h" |
Mattinico | 0:1dac861f0eae | 4 | InterruptIn button(PC_13); // Interrupt on digital pushbutton input p18 |
Mattinico | 0:1dac861f0eae | 5 | DigitalOut led1(LED1); // digital out to LED1 |
Mattinico | 0:1dac861f0eae | 6 | Timer debounce; // define debounce timer |
Mattinico | 0:1dac861f0eae | 7 | void toggle(void); // function prototype |
Mattinico | 0:1dac861f0eae | 8 | |
Mattinico | 0:1dac861f0eae | 9 | int main() |
Mattinico | 0:1dac861f0eae | 10 | { |
Mattinico | 0:1dac861f0eae | 11 | debounce.start(); |
Mattinico | 0:1dac861f0eae | 12 | button.rise(&toggle); // attach the address of the toggle |
Mattinico | 0:1dac861f0eae | 13 | } |
Mattinico | 0:1dac861f0eae | 14 | |
Mattinico | 0:1dac861f0eae | 15 | // function to the rising edge |
Mattinico | 0:1dac861f0eae | 16 | void toggle() |
Mattinico | 0:1dac861f0eae | 17 | { |
Mattinico | 0:1dac861f0eae | 18 | if (debounce.read_ms()>10) // only allow toggle if debounce timer |
Mattinico | 0:1dac861f0eae | 19 | led1=!led1; // has passed 10 ms |
Mattinico | 0:1dac861f0eae | 20 | debounce.reset(); // restart timer when the toggle is performed |
Mattinico | 0:1dac861f0eae | 21 | } |