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

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 
00004 #define kRED    (1 << 2)    //4
00005 #define kYELLOW (1 << 1)    //2
00006 #define kGREEN  (1 << 0)    //1
00007 
00008 //Global objects
00009 BusOut binaryOutput(D5, D6, D7);    //Outputs as an integer
00010 
00011 DigitalIn SW1(D3);
00012 DigitalIn SW2(D4);
00013 
00014 AnalogIn AIN(A0);
00015 float fVin = 0.0;
00016 
00017 //Main function
00018 int main() {
00019     
00020     //This represents which state we are in
00021     //RED or GREEN
00022     int state = 0;
00023     
00024     while(1) {
00025         
00026         //Read ADC
00027         fVin = AIN;
00028         
00029         //Write to terminal
00030         printf("Analog input = %6.4f\n", fVin);
00031         
00032         //Now the "state machine" - next state logic
00033         switch (state) {
00034             //The RED state
00035             case 0:
00036                 //Condition to switch state
00037                 if (fVin > 0.6f) {
00038                     state = 1;
00039                 }
00040                 break;
00041                 
00042             //The GREEN state
00043             case 1:
00044                 
00045                 //Condition to switch state
00046                 if (fVin < 0.4f) {
00047                     state = 0;
00048                 }
00049                 break;
00050                 
00051             default:
00052                 state = 0;
00053         }
00054         
00055         //Output logic
00056         switch (state) {
00057         case 0:
00058             binaryOutput = kGREEN;
00059             break;
00060         case 1:
00061             binaryOutput = kRED;
00062             break;
00063         default:
00064             binaryOutput = 0;        
00065         }
00066         
00067         //Wait
00068         wait(0.1);
00069         
00070     } //end while(1)
00071 } //end main