Task 4.3.5 Solution

Committer:
noutram
Date:
Thu Jul 13 14:54:30 2017 +0000
Revision:
1:e7fcac2ee68d
Parent:
0:e6cfe26c00ef
updated for mbed-os 5.5

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 0:e6cfe26c00ef 48
noutram 0:e6cfe26c00ef 49 //Check the threshold
noutram 0:e6cfe26c00ef 50 if (fLDRAverage > fPOTAverage) {
noutram 0:e6cfe26c00ef 51 binaryOutput = 0; //Binary 000
noutram 0:e6cfe26c00ef 52 } else {
noutram 0:e6cfe26c00ef 53 binaryOutput = 7; //Binary 111
noutram 0:e6cfe26c00ef 54 }
noutram 0:e6cfe26c00ef 55
noutram 0:e6cfe26c00ef 56 } //end while(1)
noutram 0:e6cfe26c00ef 57 } //end main
noutram 0:e6cfe26c00ef 58
noutram 0:e6cfe26c00ef 59 //ISR for the ticker - simply there to perform sampling
noutram 0:e6cfe26c00ef 60 void doCaptureSamples()
noutram 0:e6cfe26c00ef 61 {
noutram 0:e6cfe26c00ef 62 //Toggle digital out
noutram 0:e6cfe26c00ef 63 strobe = !strobe;
noutram 0:e6cfe26c00ef 64
noutram 0:e6cfe26c00ef 65 //READ ADC - store in static globals
noutram 0:e6cfe26c00ef 66 //(static globals are shared with all functions in the same C or CPP file)
noutram 0:e6cfe26c00ef 67 nextLDRSample = LDR_ADC_In;
noutram 0:e6cfe26c00ef 68 nextPOTSample = POT_ADC_In;
noutram 0:e6cfe26c00ef 69 }
noutram 0:e6cfe26c00ef 70
noutram 0:e6cfe26c00ef 71 /** Update and calculate the average. This has not been "hand optimised" yet
noutram 0:e6cfe26c00ef 72 *
noutram 0:e6cfe26c00ef 73 * @param newSample The new sample to be added to the buffer
noutram 0:e6cfe26c00ef 74 * @param buffer[] The array used to store N samples
noutram 0:e6cfe26c00ef 75 * @returns
noutram 0:e6cfe26c00ef 76 * average of the samples in the array buffer
noutram 0:e6cfe26c00ef 77 */
noutram 0:e6cfe26c00ef 78 float updateAverage(float newSample, float buffer[])
noutram 0:e6cfe26c00ef 79 {
noutram 0:e6cfe26c00ef 80 //Local variable - remember to initialise!
noutram 0:e6cfe26c00ef 81 float average = 0.0;
noutram 0:e6cfe26c00ef 82
noutram 0:e6cfe26c00ef 83 //Move all samples to the right
noutram 0:e6cfe26c00ef 84 for (unsigned int n=(N-1); n>0; n--) {
noutram 0:e6cfe26c00ef 85 buffer[n] = buffer[n-1];
noutram 0:e6cfe26c00ef 86 }
noutram 0:e6cfe26c00ef 87
noutram 0:e6cfe26c00ef 88 //Insert new sample
noutram 0:e6cfe26c00ef 89 buffer[0] = newSample;
noutram 0:e6cfe26c00ef 90
noutram 0:e6cfe26c00ef 91 //Calculate average
noutram 0:e6cfe26c00ef 92 for (unsigned int n=0; n<N; n++) {
noutram 0:e6cfe26c00ef 93 average += buffer[n];
noutram 0:e6cfe26c00ef 94 }
noutram 0:e6cfe26c00ef 95 average /= (float)N;
noutram 0:e6cfe26c00ef 96
noutram 0:e6cfe26c00ef 97 //Return the result
noutram 0:e6cfe26c00ef 98 return average;
noutram 0:e6cfe26c00ef 99 }