Task 4.3.5 Solution

Fork of Task435Solution by Nicholas Outram

Committer:
noutram
Date:
Wed Sep 18 13:52:54 2019 +0000
Revision:
3:84dbfb7f1896
Parent:
0:e6cfe26c00ef
2019 ; Fixed sampling rate

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:e6cfe26c00ef 1 #include "mbed.h"
noutram 0:e6cfe26c00ef 2
noutram 0:e6cfe26c00ef 3 //Constants
noutram 0:e6cfe26c00ef 4 #define N 10
noutram 0:e6cfe26c00ef 5
noutram 0:e6cfe26c00ef 6 //Function Prototype
noutram 0:e6cfe26c00ef 7 void doCaptureSamples();
noutram 0:e6cfe26c00ef 8 float updateAverage(float newSample, float buffer[]);
noutram 0:e6cfe26c00ef 9
noutram 0:e6cfe26c00ef 10 //Global objects
noutram 0:e6cfe26c00ef 11 Serial pc(USBTX, USBRX);
noutram 0:e6cfe26c00ef 12
noutram 0:e6cfe26c00ef 13 BusOut binaryOutput(D5, D6, D7);
noutram 0:e6cfe26c00ef 14 AnalogIn POT_ADC_In(A0);
noutram 0:e6cfe26c00ef 15 AnalogIn LDR_ADC_In(A1);
noutram 0:e6cfe26c00ef 16 DigitalOut strobe(D8);
noutram 0:e6cfe26c00ef 17
noutram 0:e6cfe26c00ef 18 float xPOT[N]; //Aray of potentiometer samples
noutram 0:e6cfe26c00ef 19 float xLDR[N]; //Aray of LDR samples
noutram 0:e6cfe26c00ef 20
noutram 0:e6cfe26c00ef 21 //These variables hold the next sample
noutram 0:e6cfe26c00ef 22 static float nextLDRSample, nextPOTSample;
noutram 0:e6cfe26c00ef 23 Ticker t;
noutram 0:e6cfe26c00ef 24
noutram 0:e6cfe26c00ef 25 //Main function
noutram 0:e6cfe26c00ef 26 int main()
noutram 0:e6cfe26c00ef 27 {
noutram 0:e6cfe26c00ef 28 //Set baud rate to 115200
noutram 0:e6cfe26c00ef 29 pc.baud(115200);
noutram 0:e6cfe26c00ef 30
noutram 0:e6cfe26c00ef 31 //Print header
noutram 0:e6cfe26c00ef 32 pc.printf("POT,LDR,avPOT, acLDR\n\n");
noutram 0:e6cfe26c00ef 33
noutram 0:e6cfe26c00ef 34 //Set up the ticker - 100Hz
noutram 0:e6cfe26c00ef 35 t.attach(doCaptureSamples, 0.01);
noutram 0:e6cfe26c00ef 36
noutram 0:e6cfe26c00ef 37 while(1) {
noutram 0:e6cfe26c00ef 38
noutram 0:e6cfe26c00ef 39 //Sleep
noutram 0:e6cfe26c00ef 40 sleep();
noutram 0:e6cfe26c00ef 41
noutram 0:e6cfe26c00ef 42 //Calculate averages
noutram 0:e6cfe26c00ef 43 float fPOTAverage = updateAverage(nextPOTSample, xPOT);
noutram 0:e6cfe26c00ef 44 float fLDRAverage = updateAverage(nextLDRSample, xLDR);
noutram 0:e6cfe26c00ef 45
noutram 0:e6cfe26c00ef 46 //Write to terminal via Serial interface
noutram 0:e6cfe26c00ef 47 pc.printf("%6.4f,%6.4f,%6.4f,%6.4f\n", xPOT[N/2], xLDR[N/2], fPOTAverage, fLDRAverage);
noutram 3:84dbfb7f1896 48 wait(0.0021); //Allow send time for 30 characters (at 115200 bits / s, 14,400 bytes / s)
noutram 3:84dbfb7f1896 49
noutram 0:e6cfe26c00ef 50 //Check the threshold
noutram 0:e6cfe26c00ef 51 if (fLDRAverage > fPOTAverage) {
noutram 0:e6cfe26c00ef 52 binaryOutput = 0; //Binary 000
noutram 0:e6cfe26c00ef 53 } else {
noutram 0:e6cfe26c00ef 54 binaryOutput = 7; //Binary 111
noutram 0:e6cfe26c00ef 55 }
noutram 0:e6cfe26c00ef 56
noutram 0:e6cfe26c00ef 57 } //end while(1)
noutram 0:e6cfe26c00ef 58 } //end main
noutram 0:e6cfe26c00ef 59
noutram 0:e6cfe26c00ef 60 //ISR for the ticker - simply there to perform sampling
noutram 0:e6cfe26c00ef 61 void doCaptureSamples()
noutram 0:e6cfe26c00ef 62 {
noutram 0:e6cfe26c00ef 63 //Toggle digital out
noutram 0:e6cfe26c00ef 64 strobe = !strobe;
noutram 0:e6cfe26c00ef 65
noutram 0:e6cfe26c00ef 66 //READ ADC - store in static globals
noutram 0:e6cfe26c00ef 67 //(static globals are shared with all functions in the same C or CPP file)
noutram 0:e6cfe26c00ef 68 nextLDRSample = LDR_ADC_In;
noutram 0:e6cfe26c00ef 69 nextPOTSample = POT_ADC_In;
noutram 0:e6cfe26c00ef 70 }
noutram 0:e6cfe26c00ef 71
noutram 0:e6cfe26c00ef 72 /** Update and calculate the average. This has not been "hand optimised" yet
noutram 0:e6cfe26c00ef 73 *
noutram 0:e6cfe26c00ef 74 * @param newSample The new sample to be added to the buffer
noutram 0:e6cfe26c00ef 75 * @param buffer[] The array used to store N samples
noutram 0:e6cfe26c00ef 76 * @returns
noutram 0:e6cfe26c00ef 77 * average of the samples in the array buffer
noutram 0:e6cfe26c00ef 78 */
noutram 0:e6cfe26c00ef 79 float updateAverage(float newSample, float buffer[])
noutram 0:e6cfe26c00ef 80 {
noutram 0:e6cfe26c00ef 81 //Local variable - remember to initialise!
noutram 0:e6cfe26c00ef 82 float average = 0.0;
noutram 0:e6cfe26c00ef 83
noutram 0:e6cfe26c00ef 84 //Move all samples to the right
noutram 0:e6cfe26c00ef 85 for (unsigned int n=(N-1); n>0; n--) {
noutram 0:e6cfe26c00ef 86 buffer[n] = buffer[n-1];
noutram 0:e6cfe26c00ef 87 }
noutram 0:e6cfe26c00ef 88
noutram 0:e6cfe26c00ef 89 //Insert new sample
noutram 0:e6cfe26c00ef 90 buffer[0] = newSample;
noutram 0:e6cfe26c00ef 91
noutram 0:e6cfe26c00ef 92 //Calculate average
noutram 0:e6cfe26c00ef 93 for (unsigned int n=0; n<N; n++) {
noutram 0:e6cfe26c00ef 94 average += buffer[n];
noutram 0:e6cfe26c00ef 95 }
noutram 0:e6cfe26c00ef 96 average /= (float)N;
noutram 0:e6cfe26c00ef 97
noutram 0:e6cfe26c00ef 98 //Return the result
noutram 0:e6cfe26c00ef 99 return average;
noutram 0:e6cfe26c00ef 100 }