Task 4.3.6 Solution

Fork of Task436Solution by Nicholas Outram

Committer:
noutram
Date:
Wed Sep 18 13:56:18 2019 +0000
Revision:
2:3f65535dbccf
Parent:
0:315c40dcb63f
2019; Fixed sampling rate

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:315c40dcb63f 1 #include "mbed.h"
noutram 0:315c40dcb63f 2
noutram 0:315c40dcb63f 3 //Constants
noutram 0:315c40dcb63f 4 #define N 10
noutram 0:315c40dcb63f 5
noutram 0:315c40dcb63f 6 //Function Prototype
noutram 0:315c40dcb63f 7 void doCaptureSamples();
noutram 0:315c40dcb63f 8 float updateAverage(float newSample, float buffer[]);
noutram 0:315c40dcb63f 9
noutram 0:315c40dcb63f 10 //Global objects
noutram 0:315c40dcb63f 11 Serial pc(USBTX, USBRX);
noutram 0:315c40dcb63f 12
noutram 0:315c40dcb63f 13 BusOut binaryOutput(D5, D6, D7);
noutram 0:315c40dcb63f 14 AnalogIn POT_ADC_In(A0);
noutram 0:315c40dcb63f 15 AnalogIn LDR_ADC_In(A1);
noutram 0:315c40dcb63f 16 DigitalOut strobe(D8);
noutram 0:315c40dcb63f 17
noutram 0:315c40dcb63f 18 float xPOT[N]; //Aray of potentiometer samples
noutram 0:315c40dcb63f 19 float xLDR[N]; //Aray of LDR samples
noutram 0:315c40dcb63f 20
noutram 0:315c40dcb63f 21 //These variables hold the next sample
noutram 0:315c40dcb63f 22 static float nextLDRSample, nextPOTSample;
noutram 0:315c40dcb63f 23
noutram 0:315c40dcb63f 24 //The location of the oldest sample
noutram 0:315c40dcb63f 25 static int indexOfOldest=(N-1);
noutram 0:315c40dcb63f 26
noutram 0:315c40dcb63f 27 //These variables hold the average values
noutram 0:315c40dcb63f 28 float fPOTSum = 0.0;
noutram 0:315c40dcb63f 29 float fLDRSum = 0.0;
noutram 0:315c40dcb63f 30
noutram 0:315c40dcb63f 31 //The ticker, used to sample data at a fixed rate
noutram 0:315c40dcb63f 32 Ticker t;
noutram 0:315c40dcb63f 33
noutram 0:315c40dcb63f 34 //Main function
noutram 0:315c40dcb63f 35 int main()
noutram 0:315c40dcb63f 36 {
noutram 0:315c40dcb63f 37 //Set baud rate to 115200
noutram 0:315c40dcb63f 38 pc.baud(115200);
noutram 0:315c40dcb63f 39
noutram 0:315c40dcb63f 40 //Print header
noutram 0:315c40dcb63f 41 pc.printf("POT,LDR,avPOT, acLDR\n\n");
noutram 2:3f65535dbccf 42 wait(0.5);
noutram 0:315c40dcb63f 43
noutram 0:315c40dcb63f 44 //Set up the ticker - 100Hz
noutram 0:315c40dcb63f 45 t.attach(doCaptureSamples, 0.01);
noutram 0:315c40dcb63f 46
noutram 0:315c40dcb63f 47 while(1) {
noutram 0:315c40dcb63f 48
noutram 0:315c40dcb63f 49 //Sleep
noutram 0:315c40dcb63f 50 sleep();
noutram 0:315c40dcb63f 51
noutram 0:315c40dcb63f 52 //Update sums
noutram 0:315c40dcb63f 53 fPOTSum = fPOTSum + nextPOTSample - xPOT[indexOfOldest];
noutram 0:315c40dcb63f 54 fLDRSum = fLDRSum + nextLDRSample - xLDR[indexOfOldest];
noutram 0:315c40dcb63f 55
noutram 0:315c40dcb63f 56 //Write new sample over the oldest
noutram 0:315c40dcb63f 57 xPOT[indexOfOldest] = nextPOTSample;
noutram 0:315c40dcb63f 58 xLDR[indexOfOldest] = nextLDRSample;
noutram 0:315c40dcb63f 59
noutram 0:315c40dcb63f 60 //update positon of oldest sample
noutram 0:315c40dcb63f 61 indexOfOldest = (indexOfOldest==0) ? (N-1) : indexOfOldest-1;
noutram 0:315c40dcb63f 62
noutram 0:315c40dcb63f 63 //Write to terminal via Serial interface
noutram 0:315c40dcb63f 64 int middle = (indexOfOldest - (N/2)) % N;
noutram 0:315c40dcb63f 65 pc.printf("%6.4f,%6.4f,%6.4f,%6.4f\n", xPOT[middle], xLDR[middle], fPOTSum/(float)N, fLDRSum/(float)N);
noutram 2:3f65535dbccf 66 wait(0.0021); //Allow send time for 30 characters (at 115200 bits / s, 14,400 bytes / s)
noutram 0:315c40dcb63f 67
noutram 0:315c40dcb63f 68 //Check the threshold
noutram 0:315c40dcb63f 69 if (fLDRSum > fPOTSum) {
noutram 0:315c40dcb63f 70 binaryOutput = 0; //Binary 000
noutram 0:315c40dcb63f 71 } else {
noutram 0:315c40dcb63f 72 binaryOutput = 7; //Binary 111
noutram 0:315c40dcb63f 73 }
noutram 0:315c40dcb63f 74
noutram 0:315c40dcb63f 75 } //end while(1)
noutram 0:315c40dcb63f 76 } //end main
noutram 0:315c40dcb63f 77
noutram 0:315c40dcb63f 78 //ISR for the ticker - simply there to perform sampling
noutram 0:315c40dcb63f 79 void doCaptureSamples()
noutram 0:315c40dcb63f 80 {
noutram 0:315c40dcb63f 81 //Toggle digital out
noutram 0:315c40dcb63f 82 strobe = !strobe;
noutram 0:315c40dcb63f 83
noutram 0:315c40dcb63f 84 //READ ADC - store in static globals
noutram 0:315c40dcb63f 85 //(static globals are shared with all functions in the same C or CPP file)
noutram 0:315c40dcb63f 86 nextLDRSample = LDR_ADC_In;
noutram 0:315c40dcb63f 87 nextPOTSample = POT_ADC_In;
noutram 0:315c40dcb63f 88 }
noutram 0:315c40dcb63f 89