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 //Move the samples in both arrays to the right 00032 for (unsigned int n=(N-1); n>0; n--) { 00033 xPOT[n] = xPOT[n-1]; 00034 xLDR[n] = xLDR[n-1]; 00035 } 00036 00037 //Insert the new sample at position 0 00038 xPOT[0] = POT_ADC_In; 00039 xLDR[0] = LDR_ADC_In; 00040 00041 //Calculate average for POT 00042 float fSum = 0.0; 00043 for (unsigned int n=0; n<N; n++) { 00044 fSum += xPOT[n]; 00045 } 00046 float fPOTAverage = fSum / (float)N; 00047 00048 //Calculate average for LDR 00049 fSum = 0.0; 00050 for (unsigned int n=0; n<N; n++) { 00051 fSum += xLDR[n]; 00052 } 00053 float fLDRAverage = fSum / (float)N; 00054 00055 //Write to terminal via Serial interface 00056 pc.printf("%6.4f,%6.4f,%6.4f,%6.4f\n", xPOT[N/2], xLDR[N/2], fPOTAverage, fLDRAverage); 00057 00058 //Check the threshold 00059 if (fLDRAverage > fPOTAverage) { 00060 binaryOutput = 0; //Binary 000 00061 } else { 00062 binaryOutput = 7; //Binary 111 00063 } 00064 00065 //Wait 1/100th second 00066 wait(0.01); 00067 00068 00069 } //end while(1) 00070 } //end main 00071 00072 00073 /* 00074 First version of the updateAverage function 00075 TODO 00076 */ 00077 float updateAverage(float newSample, float buffer[]) 00078 { 00079 //Local variable - remember to initialise! 00080 float average = 0.0; 00081 00082 //TODO: Move all samples to the right 00083 00084 00085 //TODO: Insert new sample 00086 00087 00088 //TODO: Calculate average 00089 00090 //Return the result 00091 return average; 00092 }
Generated on Thu Aug 18 2022 09:34:09 by
1.7.2