Toggle 2 Leds with two switches

Fork of Task324Solution by University of Plymouth - Stages 1, 2 and 3

Committer:
martinjamesallen1991
Date:
Sat Mar 10 00:38:49 2018 +0000
Revision:
2:1874cdaec623
Parent:
1:b7cc6bbb077d
new

Who changed what in which revision?

UserRevisionLine numberNew 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 void updateState(int sw, int &swState, DigitalOut &led, Timer &tmr)
noutram 0:3590cc227050 16 {
noutram 0:3590cc227050 17 switch (swState) {
noutram 0:3590cc227050 18
noutram 0:3590cc227050 19 //Waiting for switch to be pressed
noutram 0:3590cc227050 20 case WAITING4PRESS:
noutram 0:3590cc227050 21
noutram 0:3590cc227050 22 if (sw == 1) {
noutram 0:3590cc227050 23 //Output: start timer
noutram 0:3590cc227050 24 tmr.reset();
noutram 0:3590cc227050 25 tmr.start();
noutram 0:3590cc227050 26
noutram 0:3590cc227050 27 //Next state
noutram 0:3590cc227050 28 swState = WAITING4BOUNCE_RISING;
noutram 0:3590cc227050 29 }
noutram 0:3590cc227050 30 break;
noutram 0:3590cc227050 31
noutram 0:3590cc227050 32 //Waiting for 50ms to elapse
noutram 0:3590cc227050 33 case WAITING4BOUNCE_RISING:
noutram 0:3590cc227050 34 if (tmr.read_ms() > 50) {
noutram 0:3590cc227050 35 //Outputs: Stop timer
noutram 0:3590cc227050 36 tmr.stop();
noutram 0:3590cc227050 37 //Next state
martinjamesallen1991 2:1874cdaec623 38 swState = WAITING4RELEASE;
noutram 0:3590cc227050 39 }
noutram 0:3590cc227050 40 break;
noutram 0:3590cc227050 41
noutram 0:3590cc227050 42 //Waiting for switch to be released
noutram 0:3590cc227050 43 case WAITING4RELEASE:
noutram 0:3590cc227050 44 if (sw == 0) {
noutram 0:3590cc227050 45 //Outputs: Toggle the LED and start timer
noutram 0:3590cc227050 46 led = !led;
noutram 0:3590cc227050 47 tmr.reset();
noutram 0:3590cc227050 48 tmr.start();
noutram 0:3590cc227050 49 //Next state
noutram 0:3590cc227050 50 swState = WAITING4BOUNCE_FALLING;
noutram 0:3590cc227050 51 }
noutram 0:3590cc227050 52 break;
noutram 0:3590cc227050 53
noutram 0:3590cc227050 54 //Waiting 50ms for switch bounce
noutram 0:3590cc227050 55 case WAITING4BOUNCE_FALLING:
noutram 0:3590cc227050 56 if (tmr.read_ms() > 50) {
noutram 0:3590cc227050 57 //Outputs: Reset timer 1
noutram 0:3590cc227050 58 tmr.stop();
noutram 0:3590cc227050 59 tmr.reset();
noutram 0:3590cc227050 60
noutram 0:3590cc227050 61 //Next state:
noutram 0:3590cc227050 62 swState = WAITING4PRESS;
noutram 0:3590cc227050 63 }
noutram 0:3590cc227050 64 break;
noutram 0:3590cc227050 65
noutram 0:3590cc227050 66 default:
noutram 0:3590cc227050 67 //Something has gone very wrong
noutram 0:3590cc227050 68 tmr.stop();
noutram 0:3590cc227050 69 tmr.reset();
noutram 0:3590cc227050 70 led = 0;
noutram 0:3590cc227050 71 swState = WAITING4PRESS;
noutram 0:3590cc227050 72 break;
noutram 0:3590cc227050 73
noutram 0:3590cc227050 74 } //end switch
martinjamesallen1991 1:b7cc6bbb077d 75 } //end function
martinjamesallen1991 1:b7cc6bbb077d 76
martinjamesallen1991 1:b7cc6bbb077d 77
martinjamesallen1991 1:b7cc6bbb077d 78
martinjamesallen1991 1:b7cc6bbb077d 79 int main() {
martinjamesallen1991 1:b7cc6bbb077d 80
martinjamesallen1991 1:b7cc6bbb077d 81 //Initial state
martinjamesallen1991 1:b7cc6bbb077d 82 red_led = 0; //Set RED LED to OFF
martinjamesallen1991 1:b7cc6bbb077d 83 green_led = 0;
martinjamesallen1991 1:b7cc6bbb077d 84
martinjamesallen1991 1:b7cc6bbb077d 85 //Switch state
martinjamesallen1991 1:b7cc6bbb077d 86 int sw1State = 0;
martinjamesallen1991 1:b7cc6bbb077d 87 int sw2State = 0;
martinjamesallen1991 1:b7cc6bbb077d 88
martinjamesallen1991 1:b7cc6bbb077d 89 //Timers
martinjamesallen1991 1:b7cc6bbb077d 90 tmr1.stop();
martinjamesallen1991 1:b7cc6bbb077d 91 tmr1.reset();
martinjamesallen1991 1:b7cc6bbb077d 92 tmr2.stop();
martinjamesallen1991 1:b7cc6bbb077d 93 tmr2.reset();
martinjamesallen1991 1:b7cc6bbb077d 94
martinjamesallen1991 1:b7cc6bbb077d 95
martinjamesallen1991 1:b7cc6bbb077d 96 //Main Polling Loop
martinjamesallen1991 1:b7cc6bbb077d 97 while (true) {
martinjamesallen1991 1:b7cc6bbb077d 98
martinjamesallen1991 1:b7cc6bbb077d 99 //Poll inputs (without blocking)
martinjamesallen1991 1:b7cc6bbb077d 100 int sw1 = SW1;
martinjamesallen1991 1:b7cc6bbb077d 101 int sw2 = SW2;
martinjamesallen1991 1:b7cc6bbb077d 102
martinjamesallen1991 1:b7cc6bbb077d 103 updateState(sw1, sw1State, red_led, tmr1);
martinjamesallen1991 1:b7cc6bbb077d 104 updateState(sw2, sw2State, green_led, tmr2);
martinjamesallen1991 1:b7cc6bbb077d 105
martinjamesallen1991 1:b7cc6bbb077d 106 } //end while
martinjamesallen1991 1:b7cc6bbb077d 107
martinjamesallen1991 1:b7cc6bbb077d 108 }