Martin Allen
/
Task324SolutionWORKING
Toggle 2 Leds with two switches
Fork of Task324Solution by
main.cpp@1:b7cc6bbb077d, 2018-03-08 (annotated)
- Committer:
- martinjamesallen1991
- Date:
- Thu Mar 08 03:25:49 2018 +0000
- Revision:
- 1:b7cc6bbb077d
- Parent:
- 0:3590cc227050
- Child:
- 2:1874cdaec623
Working Revision
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:3590cc227050 | 1 | #include "mbed.h" |
noutram | 0:3590cc227050 | 2 | |
noutram | 0:3590cc227050 | 3 | DigitalOut red_led(D7); |
noutram | 0:3590cc227050 | 4 | DigitalOut green_led(D5); |
noutram | 0:3590cc227050 | 5 | DigitalIn SW1(D4); |
noutram | 0:3590cc227050 | 6 | DigitalIn SW2(D3); |
noutram | 0:3590cc227050 | 7 | Timer tmr1; |
noutram | 0:3590cc227050 | 8 | Timer tmr2; |
noutram | 0:3590cc227050 | 9 | |
noutram | 0:3590cc227050 | 10 | #define WAITING4PRESS 0 |
noutram | 0:3590cc227050 | 11 | #define WAITING4BOUNCE_RISING 1 |
noutram | 0:3590cc227050 | 12 | #define WAITING4RELEASE 2 |
noutram | 0:3590cc227050 | 13 | #define WAITING4BOUNCE_FALLING 4 |
noutram | 0:3590cc227050 | 14 | |
noutram | 0:3590cc227050 | 15 | // Update the state for a given switch and LED pair |
noutram | 0:3590cc227050 | 16 | // Parameters: |
noutram | 0:3590cc227050 | 17 | // sw is the switch input, 0 for released and 1 for pressed, passed by value |
noutram | 0:3590cc227050 | 18 | // swState is an integer that keeps a record of state, passed by reference (updated by the function) |
noutram | 0:3590cc227050 | 19 | // led is of type DigitalOut, connected to a LED |
noutram | 0:3590cc227050 | 20 | // tmr is of type Timer, a reference to unique instance of a timer |
noutram | 0:3590cc227050 | 21 | // This function cannot be called by an ISR |
martinjamesallen1991 | 1:b7cc6bbb077d | 22 | |
noutram | 0:3590cc227050 | 23 | void updateState(int sw, int &swState, DigitalOut &led, Timer &tmr) |
noutram | 0:3590cc227050 | 24 | { |
noutram | 0:3590cc227050 | 25 | //LED |
noutram | 0:3590cc227050 | 26 | switch (swState) { |
noutram | 0:3590cc227050 | 27 | |
noutram | 0:3590cc227050 | 28 | //Waiting for switch to be pressed |
noutram | 0:3590cc227050 | 29 | case WAITING4PRESS: |
noutram | 0:3590cc227050 | 30 | |
noutram | 0:3590cc227050 | 31 | if (sw == 1) { |
noutram | 0:3590cc227050 | 32 | //Output: start timer |
noutram | 0:3590cc227050 | 33 | tmr.reset(); |
noutram | 0:3590cc227050 | 34 | tmr.start(); |
noutram | 0:3590cc227050 | 35 | |
noutram | 0:3590cc227050 | 36 | //Next state |
noutram | 0:3590cc227050 | 37 | swState = WAITING4BOUNCE_RISING; |
noutram | 0:3590cc227050 | 38 | puts("Entering state: WAITING4BOUNCE_RISING"); |
noutram | 0:3590cc227050 | 39 | } |
noutram | 0:3590cc227050 | 40 | break; |
noutram | 0:3590cc227050 | 41 | |
noutram | 0:3590cc227050 | 42 | //Waiting for 50ms to elapse |
noutram | 0:3590cc227050 | 43 | case WAITING4BOUNCE_RISING: |
noutram | 0:3590cc227050 | 44 | if (tmr.read_ms() > 50) { |
noutram | 0:3590cc227050 | 45 | //Outputs: Stop timer |
noutram | 0:3590cc227050 | 46 | tmr.stop(); |
noutram | 0:3590cc227050 | 47 | //Next state |
noutram | 0:3590cc227050 | 48 | swState = WAITING4RELEASE; |
noutram | 0:3590cc227050 | 49 | puts("Entering state: WAITING4RELEASE"); |
noutram | 0:3590cc227050 | 50 | } |
noutram | 0:3590cc227050 | 51 | break; |
noutram | 0:3590cc227050 | 52 | |
noutram | 0:3590cc227050 | 53 | //Waiting for switch to be released |
noutram | 0:3590cc227050 | 54 | case WAITING4RELEASE: |
noutram | 0:3590cc227050 | 55 | if (sw == 0) { |
noutram | 0:3590cc227050 | 56 | //Outputs: Toggle the LED and start timer |
noutram | 0:3590cc227050 | 57 | led = !led; |
noutram | 0:3590cc227050 | 58 | tmr.reset(); |
noutram | 0:3590cc227050 | 59 | tmr.start(); |
noutram | 0:3590cc227050 | 60 | //Next state |
noutram | 0:3590cc227050 | 61 | swState = WAITING4BOUNCE_FALLING; |
noutram | 0:3590cc227050 | 62 | puts("Entering state: WAITING4BOUNCE_FALLING"); |
noutram | 0:3590cc227050 | 63 | } |
noutram | 0:3590cc227050 | 64 | break; |
noutram | 0:3590cc227050 | 65 | |
noutram | 0:3590cc227050 | 66 | //Waiting 50ms for switch bounce |
noutram | 0:3590cc227050 | 67 | case WAITING4BOUNCE_FALLING: |
noutram | 0:3590cc227050 | 68 | if (tmr.read_ms() > 50) { |
noutram | 0:3590cc227050 | 69 | //Outputs: Reset timer 1 |
noutram | 0:3590cc227050 | 70 | tmr.stop(); |
noutram | 0:3590cc227050 | 71 | tmr.reset(); |
noutram | 0:3590cc227050 | 72 | |
noutram | 0:3590cc227050 | 73 | //Next state: |
noutram | 0:3590cc227050 | 74 | swState = WAITING4PRESS; |
noutram | 0:3590cc227050 | 75 | puts("Entering state: WAITING4PRESS"); |
noutram | 0:3590cc227050 | 76 | } |
noutram | 0:3590cc227050 | 77 | break; |
noutram | 0:3590cc227050 | 78 | |
noutram | 0:3590cc227050 | 79 | default: |
noutram | 0:3590cc227050 | 80 | //Something has gone very wrong |
noutram | 0:3590cc227050 | 81 | tmr.stop(); |
noutram | 0:3590cc227050 | 82 | tmr.reset(); |
noutram | 0:3590cc227050 | 83 | led = 0; |
noutram | 0:3590cc227050 | 84 | swState = WAITING4PRESS; |
noutram | 0:3590cc227050 | 85 | puts("ERROR"); |
noutram | 0:3590cc227050 | 86 | break; |
noutram | 0:3590cc227050 | 87 | |
noutram | 0:3590cc227050 | 88 | } //end switch |
martinjamesallen1991 | 1:b7cc6bbb077d | 89 | } //end function |
martinjamesallen1991 | 1:b7cc6bbb077d | 90 | |
martinjamesallen1991 | 1:b7cc6bbb077d | 91 | |
martinjamesallen1991 | 1:b7cc6bbb077d | 92 | |
martinjamesallen1991 | 1:b7cc6bbb077d | 93 | int main() { |
martinjamesallen1991 | 1:b7cc6bbb077d | 94 | //Initial logging message |
martinjamesallen1991 | 1:b7cc6bbb077d | 95 | puts("START"); |
martinjamesallen1991 | 1:b7cc6bbb077d | 96 | |
martinjamesallen1991 | 1:b7cc6bbb077d | 97 | //Initial state |
martinjamesallen1991 | 1:b7cc6bbb077d | 98 | red_led = 0; //Set RED LED to OFF |
martinjamesallen1991 | 1:b7cc6bbb077d | 99 | green_led = 0; |
martinjamesallen1991 | 1:b7cc6bbb077d | 100 | |
martinjamesallen1991 | 1:b7cc6bbb077d | 101 | //Switch state |
martinjamesallen1991 | 1:b7cc6bbb077d | 102 | int sw1State = 0; |
martinjamesallen1991 | 1:b7cc6bbb077d | 103 | int sw2State = 0; |
martinjamesallen1991 | 1:b7cc6bbb077d | 104 | |
martinjamesallen1991 | 1:b7cc6bbb077d | 105 | //Timers |
martinjamesallen1991 | 1:b7cc6bbb077d | 106 | tmr1.stop(); |
martinjamesallen1991 | 1:b7cc6bbb077d | 107 | tmr1.reset(); |
martinjamesallen1991 | 1:b7cc6bbb077d | 108 | tmr2.stop(); |
martinjamesallen1991 | 1:b7cc6bbb077d | 109 | tmr2.reset(); |
martinjamesallen1991 | 1:b7cc6bbb077d | 110 | |
martinjamesallen1991 | 1:b7cc6bbb077d | 111 | //Initial logging message |
martinjamesallen1991 | 1:b7cc6bbb077d | 112 | puts("Entering state WAITING4PRESS"); |
martinjamesallen1991 | 1:b7cc6bbb077d | 113 | |
martinjamesallen1991 | 1:b7cc6bbb077d | 114 | //Main Polling Loop |
martinjamesallen1991 | 1:b7cc6bbb077d | 115 | while (true) { |
martinjamesallen1991 | 1:b7cc6bbb077d | 116 | |
martinjamesallen1991 | 1:b7cc6bbb077d | 117 | //Poll inputs (without blocking) |
martinjamesallen1991 | 1:b7cc6bbb077d | 118 | int sw1 = SW1; |
martinjamesallen1991 | 1:b7cc6bbb077d | 119 | int sw2 = SW2; |
martinjamesallen1991 | 1:b7cc6bbb077d | 120 | |
martinjamesallen1991 | 1:b7cc6bbb077d | 121 | updateState(sw1, sw1State, red_led, tmr1); |
martinjamesallen1991 | 1:b7cc6bbb077d | 122 | updateState(sw2, sw2State, green_led, tmr2); |
martinjamesallen1991 | 1:b7cc6bbb077d | 123 | |
martinjamesallen1991 | 1:b7cc6bbb077d | 124 | } //end while |
martinjamesallen1991 | 1:b7cc6bbb077d | 125 | |
martinjamesallen1991 | 1:b7cc6bbb077d | 126 | } |