
Lab 2.3.1 Debounce
main.cpp@3:8409cacb0beb, 2020-07-30 (annotated)
- Committer:
- ciaranom
- Date:
- Thu Jul 30 10:18:27 2020 +0000
- Revision:
- 3:8409cacb0beb
- Parent:
- 2:af2bebc7b6c0
Added printf above flash
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ciaranom | 0:0853f795e63b | 1 | #include "mbed.h" |
ciaranom | 0:0853f795e63b | 2 | #include "C12832.h" |
ciaranom | 0:0853f795e63b | 3 | |
ciaranom | 0:0853f795e63b | 4 | InterruptIn joystickcenter(p14); |
ciaranom | 0:0853f795e63b | 5 | InterruptIn button(p9); |
ciaranom | 0:0853f795e63b | 6 | DigitalOut led(LED1); |
ciaranom | 0:0853f795e63b | 7 | DigitalOut flash(LED4); |
ciaranom | 0:0853f795e63b | 8 | C12832 lcd(p5, p7, p6, p8, p11); |
ciaranom | 2:af2bebc7b6c0 | 9 | Timer debounce; |
ciaranom | 2:af2bebc7b6c0 | 10 | |
ciaranom | 2:af2bebc7b6c0 | 11 | int x; //sets variable x to 0 |
ciaranom | 2:af2bebc7b6c0 | 12 | //debounce.start(); // start debounce counting |
ciaranom | 0:0853f795e63b | 13 | |
ciaranom | 0:0853f795e63b | 14 | |
ciaranom | 0:0853f795e63b | 15 | |
ciaranom | 0:0853f795e63b | 16 | |
ciaranom | 0:0853f795e63b | 17 | void flip() { |
ciaranom | 2:af2bebc7b6c0 | 18 | if (debounce.read_ms() >1000) // if deboucne value is greater that 1 second |
ciaranom | 2:af2bebc7b6c0 | 19 | { |
ciaranom | 0:0853f795e63b | 20 | led = !led; // toggles the led when the joystick button is pressed. |
ciaranom | 1:7149c0779e5b | 21 | x=1; // sets variable x to 1 |
ciaranom | 2:af2bebc7b6c0 | 22 | // lcd.locate(0,15); |
ciaranom | 2:af2bebc7b6c0 | 23 | // lcd.printf("DB %.2f", debounce.read()); |
ciaranom | 2:af2bebc7b6c0 | 24 | |
ciaranom | 2:af2bebc7b6c0 | 25 | debounce.reset(); // reset deboucne value |
ciaranom | 2:af2bebc7b6c0 | 26 | } |
ciaranom | 0:0853f795e63b | 27 | } |
ciaranom | 1:7149c0779e5b | 28 | |
ciaranom | 1:7149c0779e5b | 29 | void reset() |
ciaranom | 1:7149c0779e5b | 30 | { |
ciaranom | 1:7149c0779e5b | 31 | x=0; |
ciaranom | 1:7149c0779e5b | 32 | } |
ciaranom | 1:7149c0779e5b | 33 | |
ciaranom | 0:0853f795e63b | 34 | int main() { |
ciaranom | 1:7149c0779e5b | 35 | |
ciaranom | 0:0853f795e63b | 36 | joystickcenter.rise(&flip); // attach the function address to the rising edge |
ciaranom | 0:0853f795e63b | 37 | button.mode(PullUp); // With this, no external pullup resistor needed |
ciaranom | 0:0853f795e63b | 38 | button.rise(&flip); // attach the function address to the rising edge |
ciaranom | 0:0853f795e63b | 39 | |
ciaranom | 1:7149c0779e5b | 40 | |
ciaranom | 1:7149c0779e5b | 41 | |
ciaranom | 0:0853f795e63b | 42 | while(1) { // wait around, interrupts will interrupt this! |
ciaranom | 1:7149c0779e5b | 43 | |
ciaranom | 1:7149c0779e5b | 44 | if (x==1){ //if variable 'x' is equivalent to 0 |
ciaranom | 2:af2bebc7b6c0 | 45 | //lcd.locate(0,0); // Sets location on lcd to 0,0 |
ciaranom | 2:af2bebc7b6c0 | 46 | //lcd.printf("Variable set to 1"); // Prints Variable set if loop parameter is true |
ciaranom | 2:af2bebc7b6c0 | 47 | wait(1); //waits for 1 seconds, to show print on MBED lcd |
ciaranom | 2:af2bebc7b6c0 | 48 | //lcd.cls(); //clears lcd screen |
ciaranom | 1:7149c0779e5b | 49 | reset();; //resets x to 0 |
ciaranom | 0:0853f795e63b | 50 | } |
ciaranom | 0:0853f795e63b | 51 | |
ciaranom | 3:8409cacb0beb | 52 | lcd.locate(0,0); |
ciaranom | 3:8409cacb0beb | 53 | lcd.printf("DB %.2f", debounce.read()) |
ciaranom | 1:7149c0779e5b | 54 | flash = !flash; // turns LED4 on if off, off if on |
ciaranom | 1:7149c0779e5b | 55 | wait(0.25); // the instruction to wait for a quarter-second |
ciaranom | 0:0853f795e63b | 56 | } |
ciaranom | 1:7149c0779e5b | 57 | } |