![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Toggle 2 Leds with two switches
Fork of Task324Solution by
Diff: main.cpp
- Revision:
- 1:b7cc6bbb077d
- Parent:
- 0:3590cc227050
- Child:
- 2:1874cdaec623
diff -r 3590cc227050 -r b7cc6bbb077d main.cpp --- a/main.cpp Thu Jul 13 14:48:11 2017 +0000 +++ b/main.cpp Thu Mar 08 03:25:49 2018 +0000 @@ -1,14 +1,9 @@ #include "mbed.h" -void updateState(int &sw, int &swState, DigitalOut &led, Timer &tmr) ; - DigitalOut red_led(D7); DigitalOut green_led(D5); DigitalIn SW1(D4); DigitalIn SW2(D3); -//This is the solution based on the proposed flowchart. -//The precise delay required may need adjusting - Timer tmr1; Timer tmr2; @@ -17,41 +12,6 @@ #define WAITING4RELEASE 2 #define WAITING4BOUNCE_FALLING 4 -int main() { - //Initial logging message - puts("START"); - - //Initial state - red_led = 0; //Set RED LED to OFF - green_led = 0; - - //Switch state - int sw1State = 0; - int sw2State = 0; - - //Timers - tmr1.stop(); - tmr1.reset(); - tmr2.stop(); - tmr2.reset(); - - //Initial logging message - puts("Entering state WAITING4PRESS"); - - //Main Polling Loop - while (true) { - - //Poll inputs (without blocking) - int sw1 = SW1; - int sw2 = SW2; - - updateState(sw1, sw1State, red_led, tmr1); - updateState(sw2, sw2State, green_led, tmr2); - - } //end while - -} - // Update the state for a given switch and LED pair // Parameters: // sw is the switch input, 0 for released and 1 for pressed, passed by value @@ -59,6 +19,7 @@ // led is of type DigitalOut, connected to a LED // tmr is of type Timer, a reference to unique instance of a timer // This function cannot be called by an ISR + void updateState(int sw, int &swState, DigitalOut &led, Timer &tmr) { //LED @@ -125,4 +86,41 @@ break; } //end switch - } //end function \ No newline at end of file + } //end function + + + +int main() { + //Initial logging message + puts("START"); + + //Initial state + red_led = 0; //Set RED LED to OFF + green_led = 0; + + //Switch state + int sw1State = 0; + int sw2State = 0; + + //Timers + tmr1.stop(); + tmr1.reset(); + tmr2.stop(); + tmr2.reset(); + + //Initial logging message + puts("Entering state WAITING4PRESS"); + + //Main Polling Loop + while (true) { + + //Poll inputs (without blocking) + int sw1 = SW1; + int sw2 = SW2; + + updateState(sw1, sw1State, red_led, tmr1); + updateState(sw2, sw2State, green_led, tmr2); + + } //end while + +} \ No newline at end of file