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

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #define kRED    4
00003 #define kYELLOW 2
00004 #define kGREEN  1
00005 
00006 
00007 //Global objects
00008 BusOut binaryOutput(D5, D6, D7);    //Outputs as an integer
00009 
00010 DigitalIn SW1(D3);
00011 DigitalIn SW2(D4);
00012 
00013 AnalogIn AIN(A0);
00014 float fVin = 0.0;
00015 
00016 //Main function
00017 int main() {
00018 
00019 
00020     while(1) {
00021         
00022         //Read ADC         
00023         fVin = AIN;
00024         
00025         //Write to terminal
00026         printf("Analog input = %6.4f\n", fVin); //3 decimal places, fieldwidth=5
00027         
00028         if (fVin < 0.4f)  {
00029             binaryOutput = kGREEN;    
00030         } else if (fVin < 0.6f) {
00031             binaryOutput = kYELLOW;    
00032         } else {
00033             binaryOutput = kRED;    
00034         }
00035         
00036         //Wait
00037         wait(0.1);
00038             
00039     } //end while(1)
00040 } //end main
00041 
00042