Task 4.3.3 Solution

Dependencies:   mbed

Committer:
noutram
Date:
Thu Sep 24 12:30:19 2015 +0000
Revision:
0:fa5ec79b26fb
Initial version 24-09-2015

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:fa5ec79b26fb 1 #include "mbed.h"
noutram 0:fa5ec79b26fb 2
noutram 0:fa5ec79b26fb 3 //Constants
noutram 0:fa5ec79b26fb 4 #define N 10
noutram 0:fa5ec79b26fb 5
noutram 0:fa5ec79b26fb 6 //Global objects
noutram 0:fa5ec79b26fb 7 Serial pc(USBTX, USBRX);
noutram 0:fa5ec79b26fb 8
noutram 0:fa5ec79b26fb 9 BusOut binaryOutput(D5, D6, D7);
noutram 0:fa5ec79b26fb 10 AnalogIn POT_ADC_In(A0);
noutram 0:fa5ec79b26fb 11 AnalogIn LDR_ADC_In(A1);
noutram 0:fa5ec79b26fb 12
noutram 0:fa5ec79b26fb 13 float xPOT[N]; //Aray of potentiometer samples
noutram 0:fa5ec79b26fb 14 float xLDR[N]; //Aray of LDR samples
noutram 0:fa5ec79b26fb 15
noutram 0:fa5ec79b26fb 16 //Main function
noutram 0:fa5ec79b26fb 17 int main()
noutram 0:fa5ec79b26fb 18 {
noutram 0:fa5ec79b26fb 19
noutram 0:fa5ec79b26fb 20 //Set baud rate to 115200
noutram 0:fa5ec79b26fb 21 pc.baud(115200);
noutram 0:fa5ec79b26fb 22
noutram 0:fa5ec79b26fb 23 //Print header
noutram 0:fa5ec79b26fb 24 pc.printf("POT,LDR,avPOT, acLDR\n\n");
noutram 0:fa5ec79b26fb 25
noutram 0:fa5ec79b26fb 26 while(1) {
noutram 0:fa5ec79b26fb 27
noutram 0:fa5ec79b26fb 28 //Move the samples in both arrays to the right
noutram 0:fa5ec79b26fb 29 for (unsigned int n=(N-1); n>0; n--) {
noutram 0:fa5ec79b26fb 30 xPOT[n] = xPOT[n-1];
noutram 0:fa5ec79b26fb 31 xLDR[n] = xLDR[n-1];
noutram 0:fa5ec79b26fb 32 }
noutram 0:fa5ec79b26fb 33
noutram 0:fa5ec79b26fb 34 //Insert the new sample at position 0
noutram 0:fa5ec79b26fb 35 xPOT[0] = POT_ADC_In;
noutram 0:fa5ec79b26fb 36 xLDR[0] = LDR_ADC_In;
noutram 0:fa5ec79b26fb 37
noutram 0:fa5ec79b26fb 38 //Calculate average for POT
noutram 0:fa5ec79b26fb 39 float fSum = 0.0;
noutram 0:fa5ec79b26fb 40 for (unsigned int n=0; n<N; n++) {
noutram 0:fa5ec79b26fb 41 fSum += xPOT[n];
noutram 0:fa5ec79b26fb 42 }
noutram 0:fa5ec79b26fb 43 float fPOTAverage = fSum / (float)N;
noutram 0:fa5ec79b26fb 44
noutram 0:fa5ec79b26fb 45 //Calculate average for LDR
noutram 0:fa5ec79b26fb 46 fSum = 0.0;
noutram 0:fa5ec79b26fb 47 for (unsigned int n=0; n<N; n++) {
noutram 0:fa5ec79b26fb 48 fSum += xLDR[n];
noutram 0:fa5ec79b26fb 49 }
noutram 0:fa5ec79b26fb 50 float fLDRAverage = fSum / (float)N;
noutram 0:fa5ec79b26fb 51
noutram 0:fa5ec79b26fb 52 //Write to terminal via Serial interface
noutram 0:fa5ec79b26fb 53 pc.printf("%6.4f,%6.4f,%6.4f,%6.4f\n", xPOT[N/2], xLDR[N/2], fPOTAverage, fLDRAverage);
noutram 0:fa5ec79b26fb 54
noutram 0:fa5ec79b26fb 55 //Check the threshold
noutram 0:fa5ec79b26fb 56 if (fLDRAverage > fPOTAverage) {
noutram 0:fa5ec79b26fb 57 binaryOutput = 0; //Binary 000
noutram 0:fa5ec79b26fb 58 } else {
noutram 0:fa5ec79b26fb 59 binaryOutput = 7; //Binary 111
noutram 0:fa5ec79b26fb 60 }
noutram 0:fa5ec79b26fb 61
noutram 0:fa5ec79b26fb 62 //Wait 1/100th second
noutram 0:fa5ec79b26fb 63 wait(0.01);
noutram 0:fa5ec79b26fb 64
noutram 0:fa5ec79b26fb 65
noutram 0:fa5ec79b26fb 66 } //end while(1)
noutram 0:fa5ec79b26fb 67 } //end main