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

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 //Global objects
00004 BusOut binaryOutput(D5, D6, D7);    
00005 AnalogIn POT_ADC_In(A0);
00006 AnalogIn LDD_ADC_In(A1);
00007 
00008 float fPOT, fLDR = 0.0;
00009 float fPOTAverage = 0.0, fLDRAverage = 0.0;
00010 unsigned int n = 0;
00011 unsigned int N = 10;
00012 
00013 //Main function
00014 int main() {
00015     
00016     while(1) {
00017         
00018         //Read ADC
00019         fPOT = POT_ADC_In;
00020         fLDR = LDD_ADC_In;
00021         
00022         //Calculate the average of both fPOT and fLDR
00023         fPOTAverage += fPOT;
00024         fLDRAverage += fLDR;
00025         
00026         //Update the sample count
00027         n++;
00028         
00029         //Wait
00030         wait(0.01);        
00031         
00032         //Do we have N samples?
00033         if (n == N) {
00034             
00035             //Calculate average
00036             fPOTAverage /= (float)N;
00037             fLDRAverage /= (float)N;
00038             
00039             //Write to terminal
00040             printf("POT = %6.4f\tLDR = %6.4f\n", fPOTAverage, fLDRAverage);
00041         
00042             //Check the threshold
00043             if (fLDRAverage > fPOTAverage) {
00044                 binaryOutput = 0;   //Binary 000    
00045             } else {
00046                 binaryOutput = 7;   //Binary 111
00047             }  
00048             
00049             //RESET averages and sample count
00050             n = 0;
00051             fPOTAverage = 0.0;
00052             fLDRAverage = 0.0;        
00053         }
00054 
00055         
00056     } //end while(1)
00057 } //end main