Ciaran O'Malley
/
LAB_2_3_interrupt
Cleaner code
main.cpp@2:d07750adfe89, 2020-07-31 (annotated)
- Committer:
- ciaranom
- Date:
- Fri Jul 31 15:32:53 2020 +0000
- Revision:
- 2:d07750adfe89
- Parent:
- 1:7149c0779e5b
Lab 2.3
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 | 0:0853f795e63b | 9 | |
ciaranom | 0:0853f795e63b | 10 | |
ciaranom | 1:7149c0779e5b | 11 | int x=0; //sets variable x to 0 |
ciaranom | 0:0853f795e63b | 12 | |
ciaranom | 0:0853f795e63b | 13 | |
ciaranom | 0:0853f795e63b | 14 | void flip() { |
ciaranom | 0:0853f795e63b | 15 | led = !led; // toggles the led when the joystick button is pressed. |
ciaranom | 1:7149c0779e5b | 16 | x=1; // sets variable x to 1 |
ciaranom | 0:0853f795e63b | 17 | } |
ciaranom | 1:7149c0779e5b | 18 | |
ciaranom | 2:d07750adfe89 | 19 | void reset() //reset variable to 0 |
ciaranom | 1:7149c0779e5b | 20 | { |
ciaranom | 1:7149c0779e5b | 21 | x=0; |
ciaranom | 1:7149c0779e5b | 22 | } |
ciaranom | 1:7149c0779e5b | 23 | |
ciaranom | 0:0853f795e63b | 24 | int main() { |
ciaranom | 1:7149c0779e5b | 25 | |
ciaranom | 0:0853f795e63b | 26 | joystickcenter.rise(&flip); // attach the function address to the rising edge |
ciaranom | 0:0853f795e63b | 27 | button.mode(PullUp); // With this, no external pullup resistor needed |
ciaranom | 0:0853f795e63b | 28 | button.rise(&flip); // attach the function address to the rising edge |
ciaranom | 0:0853f795e63b | 29 | |
ciaranom | 1:7149c0779e5b | 30 | |
ciaranom | 1:7149c0779e5b | 31 | |
ciaranom | 0:0853f795e63b | 32 | while(1) { // wait around, interrupts will interrupt this! |
ciaranom | 1:7149c0779e5b | 33 | |
ciaranom | 1:7149c0779e5b | 34 | if (x==1){ //if variable 'x' is equivalent to 0 |
ciaranom | 1:7149c0779e5b | 35 | lcd.locate(0,0); // Sets location on lcd to 0,0 |
ciaranom | 1:7149c0779e5b | 36 | lcd.printf("Variable set to 1"); // Prints Variable set if loop parameter is true |
ciaranom | 1:7149c0779e5b | 37 | wait(10); //waits for 10 seconds, to show print on MBED lcd |
ciaranom | 1:7149c0779e5b | 38 | lcd.cls(); //clears lcd screen |
ciaranom | 1:7149c0779e5b | 39 | reset();; //resets x to 0 |
ciaranom | 0:0853f795e63b | 40 | } |
ciaranom | 0:0853f795e63b | 41 | |
ciaranom | 0:0853f795e63b | 42 | |
ciaranom | 1:7149c0779e5b | 43 | flash = !flash; // turns LED4 on if off, off if on |
ciaranom | 1:7149c0779e5b | 44 | wait(0.25); // the instruction to wait for a quarter-second |
ciaranom | 0:0853f795e63b | 45 | } |
ciaranom | 1:7149c0779e5b | 46 | } |