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

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 //Constants
00004 #define N 10
00005 
00006 //Function Prototype
00007 void doCaptureSamples();
00008 float updateAverage(float newSample, float buffer[]);
00009 
00010 //Global objects
00011 Serial pc(USBTX, USBRX);
00012 
00013 BusOut binaryOutput(D5, D6, D7);
00014 AnalogIn POT_ADC_In(A0);
00015 AnalogIn LDR_ADC_In(A1);
00016 DigitalOut strobe(D8);
00017 
00018 float xPOT[N]; //Aray of potentiometer samples
00019 float xLDR[N]; //Aray of LDR samples
00020 
00021 //These variables hold the next sample
00022 static float nextLDRSample, nextPOTSample;
00023 Ticker t;
00024 
00025 //Main function
00026 int main()
00027 {
00028     //Set baud rate to 115200
00029     pc.baud(115200);
00030 
00031     //Print header
00032     pc.printf("POT,LDR,avPOT, acLDR\n\n");
00033 
00034     //Set up the ticker - 100Hz
00035     t.attach(doCaptureSamples, 0.01);
00036 
00037     while(1) {
00038 
00039         //Sleep
00040         sleep();
00041 
00042         //Calculate averages
00043         float fPOTAverage = updateAverage(nextPOTSample, xPOT);
00044         float fLDRAverage = updateAverage(nextLDRSample, xLDR);
00045         
00046         //Write to terminal via Serial interface
00047         pc.printf("%6.4f,%6.4f,%6.4f,%6.4f\n", xPOT[N/2], xLDR[N/2], fPOTAverage, fLDRAverage);
00048 
00049         //Check the threshold
00050         if (fLDRAverage > fPOTAverage) {
00051             binaryOutput = 0;   //Binary 000
00052         } else {
00053             binaryOutput = 7;   //Binary 111
00054         }
00055 
00056     } //end while(1)
00057 } //end main
00058 
00059 //ISR for the ticker - simply there to perform sampling
00060 void doCaptureSamples()
00061 {
00062     //Toggle digital out
00063     strobe = !strobe;
00064     
00065     //READ ADC - store in static globals
00066     //(static globals are shared with all functions in the same C or CPP file)
00067     nextLDRSample = LDR_ADC_In;
00068     nextPOTSample = POT_ADC_In;
00069 }
00070 
00071 /** Update and calculate the average. This has not been "hand optimised" yet 
00072  *
00073  * @param newSample The new sample to be added to the buffer
00074  * @param buffer[] The array used to store N samples
00075  * @returns
00076  *   average of the samples in the array buffer
00077  */
00078 float updateAverage(float newSample, float buffer[])
00079 {
00080     //Local variable - remember to initialise!
00081     float average = 0.0;
00082     
00083     //Move all samples to the right
00084     for (unsigned int n=(N-1); n>0; n--) {
00085         buffer[n] = buffer[n-1];    
00086     }    
00087     
00088     //Insert new sample
00089     buffer[0] = newSample;
00090     
00091     //Calculate average
00092     for (unsigned int n=0; n<N; n++) {
00093         average += buffer[n];    
00094     }
00095     average /= (float)N;
00096     
00097     //Return the result
00098     return average;
00099 }