Nicholas Outram / Mbed OS Task324
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 DigitalOut  red_led(D7);
00004 DigitalOut  green_led(D5);
00005 DigitalIn   SW1(D4);
00006 DigitalIn   SW2(D3);
00007 //This is the solution based on the proposed flowchart.
00008 //The precise delay required may need adjusting
00009 
00010 Timer tmr1;
00011 
00012 #define WAITING4PRESS 0
00013 #define WAITING4BOUNCE_RISING 1
00014 #define WAITING4RELEASE 2
00015 #define WAITING4BOUNCE_FALLING 4
00016 
00017 int main() {
00018     //Initial logging message
00019     puts("START");
00020     
00021     //Initial state
00022     red_led    = 0; //Set RED LED to OFF
00023     green_led  = 1;
00024     
00025     //Switch state
00026     int sw1State = 0;
00027     
00028     //Timers
00029     tmr1.stop();
00030     tmr1.reset();
00031 
00032     //Initial logging message
00033     puts("Entering state WAITING4PRESS");
00034 
00035     //Main Polling Loop
00036     while (true) {
00037         
00038         //Poll inputs (without blocking)
00039         int sw1 = SW1;
00040         
00041         //Poll Timers
00042         int tmr1Count = tmr1.read_ms();
00043         
00044         //LED1
00045         switch (sw1State) {
00046             
00047         //Waiting for switch to be pressed
00048         case WAITING4PRESS:
00049 
00050             if (sw1 == 1) {
00051                 //Output: start timer
00052                 tmr1.reset();
00053                 tmr1.start();
00054                 
00055                 //Next state
00056                 sw1State =  WAITING4BOUNCE_RISING;  
00057                 puts("Entering state: WAITING4BOUNCE_RISING");
00058             } 
00059             break;
00060             
00061         //Waiting for 50ms to elapse
00062         case WAITING4BOUNCE_RISING:      
00063             if (tmr1Count > 50) {
00064                 //Outputs: Stop timer
00065                 tmr1.stop();
00066                 tmr1.reset();
00067                 //Next state
00068                 sw1State =  WAITING4RELEASE;  
00069                 puts("Entering state: WAITING4RELEASE");                
00070             }    
00071             break;
00072         
00073         //Waiting for switch to be released
00074         case WAITING4RELEASE:
00075             if (sw1 == 0) {
00076                 //Outputs: Toggle the LED and start timer
00077                 red_led = !red_led;
00078                 tmr1.start();                
00079                 //Next state
00080                 sw1State =  WAITING4BOUNCE_FALLING;  
00081                 puts("Entering state: WAITING4BOUNCE_FALLING");  
00082             } 
00083             break;
00084             
00085         //Waiting 50ms for switch bounce         
00086         case WAITING4BOUNCE_FALLING:
00087             if (tmr1Count > 50) {
00088                 //Outputs: Reset timer 1
00089                 tmr1.stop();
00090                 tmr1.reset();
00091                 
00092                 //Next state:
00093                 sw1State = WAITING4PRESS;
00094                 puts("Entering state: WAITING4PRESS"); 
00095             }
00096             break;
00097             
00098         default:
00099             //Something has gone very wrong
00100             tmr1.stop();
00101             tmr1.reset();
00102             red_led = 0;
00103             sw1State = WAITING4PRESS;
00104             puts("ERROR");
00105             break;
00106         
00107         } //end switch
00108         
00109         
00110     } //end while
00111 
00112 }