University of Plymouth - Stages 1, 2 and 3
/
Task434Solution
Task 4.3.4 Solution
Fork of Task434Solution by
main.cpp@2:4db5d0ff9c27, 2019-09-18 (annotated)
- Committer:
- noutram
- Date:
- Wed Sep 18 13:27:14 2019 +0000
- Revision:
- 2:4db5d0ff9c27
- Parent:
- 0:6a8980b23272
2019
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:6a8980b23272 | 1 | #include "mbed.h" |
noutram | 0:6a8980b23272 | 2 | |
noutram | 0:6a8980b23272 | 3 | //Constants |
noutram | 0:6a8980b23272 | 4 | #define N 10 |
noutram | 0:6a8980b23272 | 5 | |
noutram | 0:6a8980b23272 | 6 | //Function Prototype |
noutram | 0:6a8980b23272 | 7 | float updateAverage(float newSample, float buffer[]); |
noutram | 0:6a8980b23272 | 8 | |
noutram | 0:6a8980b23272 | 9 | //Global objects |
noutram | 0:6a8980b23272 | 10 | Serial pc(USBTX, USBRX); |
noutram | 0:6a8980b23272 | 11 | |
noutram | 0:6a8980b23272 | 12 | BusOut binaryOutput(D5, D6, D7); |
noutram | 0:6a8980b23272 | 13 | AnalogIn POT_ADC_In(A0); |
noutram | 0:6a8980b23272 | 14 | AnalogIn LDR_ADC_In(A1); |
noutram | 0:6a8980b23272 | 15 | |
noutram | 0:6a8980b23272 | 16 | float xPOT[N]; //Aray of potentiometer samples |
noutram | 0:6a8980b23272 | 17 | float xLDR[N]; //Aray of LDR samples |
noutram | 0:6a8980b23272 | 18 | |
noutram | 0:6a8980b23272 | 19 | //Main function |
noutram | 0:6a8980b23272 | 20 | int main() |
noutram | 0:6a8980b23272 | 21 | { |
noutram | 0:6a8980b23272 | 22 | |
noutram | 0:6a8980b23272 | 23 | //Set baud rate to 115200 |
noutram | 0:6a8980b23272 | 24 | pc.baud(115200); |
noutram | 0:6a8980b23272 | 25 | |
noutram | 0:6a8980b23272 | 26 | //Print header |
noutram | 0:6a8980b23272 | 27 | pc.printf("POT,LDR,avPOT, acLDR\n\n"); |
noutram | 0:6a8980b23272 | 28 | |
noutram | 0:6a8980b23272 | 29 | while(1) { |
noutram | 0:6a8980b23272 | 30 | |
noutram | 0:6a8980b23272 | 31 | //Calculate averages |
noutram | 0:6a8980b23272 | 32 | float fPOTAverage = updateAverage(POT_ADC_In, xPOT); |
noutram | 0:6a8980b23272 | 33 | float fLDRAverage = updateAverage(LDR_ADC_In, xLDR); |
noutram | 0:6a8980b23272 | 34 | |
noutram | 0:6a8980b23272 | 35 | //Write to terminal via Serial interface |
noutram | 0:6a8980b23272 | 36 | pc.printf("%6.4f,%6.4f,%6.4f,%6.4f\n", xPOT[N/2], xLDR[N/2], fPOTAverage, fLDRAverage); |
noutram | 0:6a8980b23272 | 37 | |
noutram | 0:6a8980b23272 | 38 | //Check the threshold |
noutram | 0:6a8980b23272 | 39 | if (fLDRAverage > fPOTAverage) { |
noutram | 0:6a8980b23272 | 40 | binaryOutput = 0; //Binary 000 |
noutram | 0:6a8980b23272 | 41 | } else { |
noutram | 0:6a8980b23272 | 42 | binaryOutput = 7; //Binary 111 |
noutram | 0:6a8980b23272 | 43 | } |
noutram | 0:6a8980b23272 | 44 | |
noutram | 0:6a8980b23272 | 45 | //Wait 1/100th second |
noutram | 0:6a8980b23272 | 46 | wait(0.01); |
noutram | 0:6a8980b23272 | 47 | |
noutram | 0:6a8980b23272 | 48 | |
noutram | 0:6a8980b23272 | 49 | } //end while(1) |
noutram | 0:6a8980b23272 | 50 | } //end main |
noutram | 0:6a8980b23272 | 51 | |
noutram | 0:6a8980b23272 | 52 | |
noutram | 0:6a8980b23272 | 53 | /** Update and calculate the average. This has not been "hand optimised" yet |
noutram | 0:6a8980b23272 | 54 | * |
noutram | 0:6a8980b23272 | 55 | * @param newSample The new sample to be added to the buffer |
noutram | 0:6a8980b23272 | 56 | * @param buffer[] The array used to store N samples |
noutram | 0:6a8980b23272 | 57 | * @returns |
noutram | 0:6a8980b23272 | 58 | * average of the samples in the array buffer |
noutram | 0:6a8980b23272 | 59 | */ |
noutram | 0:6a8980b23272 | 60 | float updateAverage(float newSample, float buffer[]) |
noutram | 0:6a8980b23272 | 61 | { |
noutram | 0:6a8980b23272 | 62 | //Local variable - remember to initialise! |
noutram | 0:6a8980b23272 | 63 | float average = 0.0; |
noutram | 0:6a8980b23272 | 64 | |
noutram | 0:6a8980b23272 | 65 | //Move all samples to the right |
noutram | 0:6a8980b23272 | 66 | for (unsigned int n=(N-1); n>0; n--) { |
noutram | 0:6a8980b23272 | 67 | buffer[n] = buffer[n-1]; |
noutram | 0:6a8980b23272 | 68 | } |
noutram | 0:6a8980b23272 | 69 | |
noutram | 0:6a8980b23272 | 70 | //Insert new sample |
noutram | 0:6a8980b23272 | 71 | buffer[0] = newSample; |
noutram | 0:6a8980b23272 | 72 | |
noutram | 0:6a8980b23272 | 73 | //Calculate average |
noutram | 0:6a8980b23272 | 74 | for (unsigned int n=0; n<N; n++) { |
noutram | 0:6a8980b23272 | 75 | average += buffer[n]; |
noutram | 0:6a8980b23272 | 76 | } |
noutram | 0:6a8980b23272 | 77 | average /= (float)N; |
noutram | 0:6a8980b23272 | 78 | |
noutram | 0:6a8980b23272 | 79 | //Return the result |
noutram | 0:6a8980b23272 | 80 | return average; |
noutram | 0:6a8980b23272 | 81 | } |