Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp
00001 #include "mbed.h" 00002 00003 //Constants 00004 #define N 10 00005 00006 //Function Prototype 00007 float updateAverage(float newSample, float buffer[]); 00008 00009 //Global objects 00010 Serial pc(USBTX, USBRX); 00011 00012 BusOut binaryOutput(D5, D6, D7); 00013 AnalogIn POT_ADC_In(A0); 00014 AnalogIn LDR_ADC_In(A1); 00015 00016 float xPOT[N]; //Aray of potentiometer samples 00017 float xLDR[N]; //Aray of LDR samples 00018 00019 //Main function 00020 int main() 00021 { 00022 00023 //Set baud rate to 115200 00024 pc.baud(115200); 00025 00026 //Print header 00027 pc.printf("POT,LDR,avPOT, acLDR\n\n"); 00028 00029 while(1) { 00030 00031 //Calculate averages 00032 float fPOTAverage = updateAverage(POT_ADC_In, xPOT); 00033 float fLDRAverage = updateAverage(LDR_ADC_In, xLDR); 00034 00035 //Write to terminal via Serial interface 00036 pc.printf("%6.4f,%6.4f,%6.4f,%6.4f\n", xPOT[N/2], xLDR[N/2], fPOTAverage, fLDRAverage); 00037 00038 //Check the threshold 00039 if (fLDRAverage > fPOTAverage) { 00040 binaryOutput = 0; //Binary 000 00041 } else { 00042 binaryOutput = 7; //Binary 111 00043 } 00044 00045 //Wait 1/100th second 00046 wait(0.01); 00047 00048 00049 } //end while(1) 00050 } //end main 00051 00052 00053 /** Update and calculate the average. This has not been "hand optimised" yet 00054 * 00055 * @param newSample The new sample to be added to the buffer 00056 * @param buffer[] The array used to store N samples 00057 * @returns 00058 * average of the samples in the array buffer 00059 */ 00060 float updateAverage(float newSample, float buffer[]) 00061 { 00062 //Local variable - remember to initialise! 00063 float average = 0.0; 00064 00065 //Move all samples to the right 00066 for (unsigned int n=(N-1); n>0; n--) { 00067 buffer[n] = buffer[n-1]; 00068 } 00069 00070 //Insert new sample 00071 buffer[0] = newSample; 00072 00073 //Calculate average 00074 for (unsigned int n=0; n<N; n++) { 00075 average += buffer[n]; 00076 } 00077 average /= (float)N; 00078 00079 //Return the result 00080 return average; 00081 }
Generated on Sun Jul 17 2022 04:57:14 by
