Val Glynn
/
2_3_1_Debouncing
CA 2.3.1 Debouncing
main.cpp@0:c85a8aa92f33, 2020-07-26 (annotated)
- Committer:
- vmg
- Date:
- Sun Jul 26 20:54:08 2020 +0000
- Revision:
- 0:c85a8aa92f33
CA 2.3.1 Debouncing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vmg | 0:c85a8aa92f33 | 1 | #include "mbed.h" |
vmg | 0:c85a8aa92f33 | 2 | #include "C12832.h" |
vmg | 0:c85a8aa92f33 | 3 | C12832 lcd(p5,p7,p6,p8,p11); |
vmg | 0:c85a8aa92f33 | 4 | InterruptIn joystickcenter(p14); |
vmg | 0:c85a8aa92f33 | 5 | InterruptIn button(p9); |
vmg | 0:c85a8aa92f33 | 6 | DigitalOut led(LED1); |
vmg | 0:c85a8aa92f33 | 7 | DigitalOut flash(LED4); |
vmg | 0:c85a8aa92f33 | 8 | Timer debounce; |
vmg | 0:c85a8aa92f33 | 9 | int x; |
vmg | 0:c85a8aa92f33 | 10 | |
vmg | 0:c85a8aa92f33 | 11 | void flip(){ |
vmg | 0:c85a8aa92f33 | 12 | debounce.start(); |
vmg | 0:c85a8aa92f33 | 13 | if (debounce.read_ms()>1000) |
vmg | 0:c85a8aa92f33 | 14 | debounce.reset(); |
vmg | 0:c85a8aa92f33 | 15 | led = !led; // toggles the led when the joystick button is pressed. |
vmg | 0:c85a8aa92f33 | 16 | x=1; |
vmg | 0:c85a8aa92f33 | 17 | } |
vmg | 0:c85a8aa92f33 | 18 | int main(){ |
vmg | 0:c85a8aa92f33 | 19 | joystickcenter.rise(&flip); // attach the function address to the rising edge |
vmg | 0:c85a8aa92f33 | 20 | button.mode(PullUp); // With this, no external pullup resistor needed |
vmg | 0:c85a8aa92f33 | 21 | button.rise(&flip); // attach the function address to the rising edge |
vmg | 0:c85a8aa92f33 | 22 | while(1) { // wait around, interrupts will interrupt this! |
vmg | 0:c85a8aa92f33 | 23 | if(x==1) { |
vmg | 0:c85a8aa92f33 | 24 | lcd.locate(0,0); |
vmg | 0:c85a8aa92f33 | 25 | lcd.printf("Variable has been SET!"); |
vmg | 0:c85a8aa92f33 | 26 | flash = !flash; // turns LED4 on if off, off if on |
vmg | 0:c85a8aa92f33 | 27 | x=0; |
vmg | 0:c85a8aa92f33 | 28 | wait(0.25); // the instruction to wait for a quarter-second |
vmg | 0:c85a8aa92f33 | 29 | } |
vmg | 0:c85a8aa92f33 | 30 | } |
vmg | 0:c85a8aa92f33 | 31 | } |
vmg | 0:c85a8aa92f33 | 32 |