Task 4.1.4

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);
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       //3 decimal places, fieldwidth=5
00027       printf("Analog input = %6.4f\n", fVin);
00028       
00029       if (fVin < 0.4f)  {
00030          binaryOutput = kGREEN;
00031       } else if (fVin < 0.6f) {
00032          binaryOutput = kYELLOW;
00033       } else {
00034          binaryOutput = kRED;
00035       }
00036       
00037       //Wait
00038       wait(0.1);
00039       
00040    } //end while(1)
00041 } //end main