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.
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 00024 //The location of the oldest sample 00025 static int indexOfOldest=(N-1); 00026 00027 //These variables hold the average values 00028 float fPOTSum = 0.0; 00029 float fLDRSum = 0.0; 00030 00031 //The ticker, used to sample data at a fixed rate 00032 Ticker t; 00033 00034 //Main function 00035 int main() 00036 { 00037 //Set baud rate to 115200 00038 pc.baud(115200); 00039 00040 //Print header 00041 pc.printf("POT,LDR,avPOT, acLDR\n\n"); 00042 00043 //Set up the ticker - 100Hz 00044 t.attach(doCaptureSamples, 0.01); 00045 00046 while(1) { 00047 00048 //Sleep 00049 sleep(); 00050 00051 //Update sums 00052 fPOTSum = fPOTSum + nextPOTSample - xPOT[indexOfOldest]; 00053 fLDRSum = fLDRSum + nextLDRSample - xLDR[indexOfOldest]; 00054 00055 //Write new sample over the oldest 00056 xPOT[indexOfOldest] = nextPOTSample; 00057 xLDR[indexOfOldest] = nextLDRSample; 00058 00059 //update positon of oldest sample 00060 indexOfOldest = (indexOfOldest==0) ? (N-1) : indexOfOldest-1; 00061 00062 //Write to terminal via Serial interface 00063 int middle = (indexOfOldest - (N/2)) % N; 00064 pc.printf("%6.4f,%6.4f,%6.4f,%6.4f\n", xPOT[middle], xLDR[middle], fPOTSum/(float)N, fLDRSum/(float)N); 00065 00066 //Check the threshold 00067 if (fLDRSum > fPOTSum) { 00068 binaryOutput = 0; //Binary 000 00069 } else { 00070 binaryOutput = 7; //Binary 111 00071 } 00072 00073 } //end while(1) 00074 } //end main 00075 00076 //ISR for the ticker - simply there to perform sampling 00077 void doCaptureSamples() 00078 { 00079 //Toggle digital out 00080 strobe = !strobe; 00081 00082 //READ ADC - store in static globals 00083 //(static globals are shared with all functions in the same C or CPP file) 00084 nextLDRSample = LDR_ADC_In; 00085 nextPOTSample = POT_ADC_In; 00086 } 00087
Generated on Sun Jul 17 2022 10:25:49 by
1.7.2