Ciaran O'Malley
/
Debounce
Lab exercise 2.3.1
main.cpp@0:674c09240a19, 2020-06-20 (annotated)
- Committer:
- ciaranom
- Date:
- Sat Jun 20 11:15:38 2020 +0000
- Revision:
- 0:674c09240a19
Lab exercise 2.3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ciaranom | 0:674c09240a19 | 1 | #include "mbed.h" |
ciaranom | 0:674c09240a19 | 2 | #include "C12832.h" |
ciaranom | 0:674c09240a19 | 3 | |
ciaranom | 0:674c09240a19 | 4 | InterruptIn joystickcenter(p14); |
ciaranom | 0:674c09240a19 | 5 | InterruptIn button(p9); |
ciaranom | 0:674c09240a19 | 6 | DigitalOut led(LED1); |
ciaranom | 0:674c09240a19 | 7 | DigitalOut flash(LED4); |
ciaranom | 0:674c09240a19 | 8 | C12832 lcd(p5, p7, p6, p8, p11); |
ciaranom | 0:674c09240a19 | 9 | Timer debounce; |
ciaranom | 0:674c09240a19 | 10 | |
ciaranom | 0:674c09240a19 | 11 | int x; |
ciaranom | 0:674c09240a19 | 12 | |
ciaranom | 0:674c09240a19 | 13 | |
ciaranom | 0:674c09240a19 | 14 | void flip() { |
ciaranom | 0:674c09240a19 | 15 | lcd.cls(); |
ciaranom | 0:674c09240a19 | 16 | led = !led; // toggles the led when the joystick button is pressed. |
ciaranom | 0:674c09240a19 | 17 | x = 1; |
ciaranom | 0:674c09240a19 | 18 | //wait(0.5); |
ciaranom | 0:674c09240a19 | 19 | |
ciaranom | 0:674c09240a19 | 20 | if (debounce.read_ms() < 10000){ |
ciaranom | 0:674c09240a19 | 21 | //lcd.locate(0,0); |
ciaranom | 0:674c09240a19 | 22 | //lcd.printf("Debounce value: %.3f \n"+ debounce.read_ms()); |
ciaranom | 0:674c09240a19 | 23 | } |
ciaranom | 0:674c09240a19 | 24 | debounce.reset(); |
ciaranom | 0:674c09240a19 | 25 | } |
ciaranom | 0:674c09240a19 | 26 | int main() { |
ciaranom | 0:674c09240a19 | 27 | |
ciaranom | 0:674c09240a19 | 28 | joystickcenter.rise(&flip); // attach the function address to the rising edge |
ciaranom | 0:674c09240a19 | 29 | button.mode(PullUp); // With this, no external pullup resistor needed |
ciaranom | 0:674c09240a19 | 30 | button.rise(&flip); // attach the function address to the rising edge |
ciaranom | 0:674c09240a19 | 31 | |
ciaranom | 0:674c09240a19 | 32 | while(1) { // wait around, interrupts will interrupt this! |
ciaranom | 0:674c09240a19 | 33 | flash = !flash; // turns LED4 on if off, off if on |
ciaranom | 0:674c09240a19 | 34 | wait(0.25); // the instruction to wait for a quarter-second |
ciaranom | 0:674c09240a19 | 35 | if (x==1) |
ciaranom | 0:674c09240a19 | 36 | { |
ciaranom | 0:674c09240a19 | 37 | lcd.printf("Variable set"); |
ciaranom | 0:674c09240a19 | 38 | x=0; |
ciaranom | 0:674c09240a19 | 39 | } |
ciaranom | 0:674c09240a19 | 40 | } |
ciaranom | 0:674c09240a19 | 41 | } |
ciaranom | 0:674c09240a19 | 42 | |
ciaranom | 0:674c09240a19 | 43 | |
ciaranom | 0:674c09240a19 | 44 | //What's needed is a global variable with its own name, for example: |
ciaranom | 0:674c09240a19 | 45 | //int x; |
ciaranom | 0:674c09240a19 | 46 | |
ciaranom | 0:674c09240a19 | 47 | //Now, you set x to 1 inside the flip() function. In main(), inside the infinite loop starting with while (1) { , you check whether x ==1 and if it is, you print to the LCD and set x to 0. |
ciaranom | 0:674c09240a19 | 48 | |
ciaranom | 0:674c09240a19 | 49 | //(Be very careful about this. x=1 sets x to 1. x==1 compares x to 1.) |