Nicholas Outram
/
Task434
Task 4.3.4
main.cpp@1:05a6ace4de93, 2017-07-13 (annotated)
- Committer:
- noutram
- Date:
- Thu Jul 13 14:54:08 2017 +0000
- Revision:
- 1:05a6ace4de93
- Parent:
- 0:5f63e9ea5a3a
updated for mbed-os 5.5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:5f63e9ea5a3a | 1 | #include "mbed.h" |
noutram | 0:5f63e9ea5a3a | 2 | |
noutram | 0:5f63e9ea5a3a | 3 | //Constants |
noutram | 0:5f63e9ea5a3a | 4 | #define N 10 |
noutram | 0:5f63e9ea5a3a | 5 | |
noutram | 0:5f63e9ea5a3a | 6 | //Function Prototype |
noutram | 0:5f63e9ea5a3a | 7 | float updateAverage(float newSample, float buffer[]); |
noutram | 0:5f63e9ea5a3a | 8 | |
noutram | 0:5f63e9ea5a3a | 9 | //Global objects |
noutram | 0:5f63e9ea5a3a | 10 | Serial pc(USBTX, USBRX); |
noutram | 0:5f63e9ea5a3a | 11 | |
noutram | 0:5f63e9ea5a3a | 12 | BusOut binaryOutput(D5, D6, D7); |
noutram | 0:5f63e9ea5a3a | 13 | AnalogIn POT_ADC_In(A0); |
noutram | 0:5f63e9ea5a3a | 14 | AnalogIn LDR_ADC_In(A1); |
noutram | 0:5f63e9ea5a3a | 15 | |
noutram | 0:5f63e9ea5a3a | 16 | float xPOT[N]; //Aray of potentiometer samples |
noutram | 0:5f63e9ea5a3a | 17 | float xLDR[N]; //Aray of LDR samples |
noutram | 0:5f63e9ea5a3a | 18 | |
noutram | 0:5f63e9ea5a3a | 19 | //Main function |
noutram | 0:5f63e9ea5a3a | 20 | int main() |
noutram | 0:5f63e9ea5a3a | 21 | { |
noutram | 0:5f63e9ea5a3a | 22 | |
noutram | 0:5f63e9ea5a3a | 23 | //Set baud rate to 115200 |
noutram | 0:5f63e9ea5a3a | 24 | pc.baud(115200); |
noutram | 0:5f63e9ea5a3a | 25 | |
noutram | 0:5f63e9ea5a3a | 26 | //Print header |
noutram | 0:5f63e9ea5a3a | 27 | pc.printf("POT,LDR,avPOT, acLDR\n\n"); |
noutram | 0:5f63e9ea5a3a | 28 | |
noutram | 0:5f63e9ea5a3a | 29 | while(1) { |
noutram | 0:5f63e9ea5a3a | 30 | |
noutram | 0:5f63e9ea5a3a | 31 | //Move the samples in both arrays to the right |
noutram | 0:5f63e9ea5a3a | 32 | for (unsigned int n=(N-1); n>0; n--) { |
noutram | 0:5f63e9ea5a3a | 33 | xPOT[n] = xPOT[n-1]; |
noutram | 0:5f63e9ea5a3a | 34 | xLDR[n] = xLDR[n-1]; |
noutram | 0:5f63e9ea5a3a | 35 | } |
noutram | 0:5f63e9ea5a3a | 36 | |
noutram | 0:5f63e9ea5a3a | 37 | //Insert the new sample at position 0 |
noutram | 0:5f63e9ea5a3a | 38 | xPOT[0] = POT_ADC_In; |
noutram | 0:5f63e9ea5a3a | 39 | xLDR[0] = LDR_ADC_In; |
noutram | 0:5f63e9ea5a3a | 40 | |
noutram | 0:5f63e9ea5a3a | 41 | //Calculate average for POT |
noutram | 0:5f63e9ea5a3a | 42 | float fSum = 0.0; |
noutram | 0:5f63e9ea5a3a | 43 | for (unsigned int n=0; n<N; n++) { |
noutram | 0:5f63e9ea5a3a | 44 | fSum += xPOT[n]; |
noutram | 0:5f63e9ea5a3a | 45 | } |
noutram | 0:5f63e9ea5a3a | 46 | float fPOTAverage = fSum / (float)N; |
noutram | 0:5f63e9ea5a3a | 47 | |
noutram | 0:5f63e9ea5a3a | 48 | //Calculate average for LDR |
noutram | 0:5f63e9ea5a3a | 49 | fSum = 0.0; |
noutram | 0:5f63e9ea5a3a | 50 | for (unsigned int n=0; n<N; n++) { |
noutram | 0:5f63e9ea5a3a | 51 | fSum += xLDR[n]; |
noutram | 0:5f63e9ea5a3a | 52 | } |
noutram | 0:5f63e9ea5a3a | 53 | float fLDRAverage = fSum / (float)N; |
noutram | 0:5f63e9ea5a3a | 54 | |
noutram | 0:5f63e9ea5a3a | 55 | //Write to terminal via Serial interface |
noutram | 0:5f63e9ea5a3a | 56 | pc.printf("%6.4f,%6.4f,%6.4f,%6.4f\n", xPOT[N/2], xLDR[N/2], fPOTAverage, fLDRAverage); |
noutram | 0:5f63e9ea5a3a | 57 | |
noutram | 0:5f63e9ea5a3a | 58 | //Check the threshold |
noutram | 0:5f63e9ea5a3a | 59 | if (fLDRAverage > fPOTAverage) { |
noutram | 0:5f63e9ea5a3a | 60 | binaryOutput = 0; //Binary 000 |
noutram | 0:5f63e9ea5a3a | 61 | } else { |
noutram | 0:5f63e9ea5a3a | 62 | binaryOutput = 7; //Binary 111 |
noutram | 0:5f63e9ea5a3a | 63 | } |
noutram | 0:5f63e9ea5a3a | 64 | |
noutram | 0:5f63e9ea5a3a | 65 | //Wait 1/100th second |
noutram | 0:5f63e9ea5a3a | 66 | wait(0.01); |
noutram | 0:5f63e9ea5a3a | 67 | |
noutram | 0:5f63e9ea5a3a | 68 | |
noutram | 0:5f63e9ea5a3a | 69 | } //end while(1) |
noutram | 0:5f63e9ea5a3a | 70 | } //end main |
noutram | 0:5f63e9ea5a3a | 71 | |
noutram | 0:5f63e9ea5a3a | 72 | |
noutram | 0:5f63e9ea5a3a | 73 | /* |
noutram | 0:5f63e9ea5a3a | 74 | First version of the updateAverage function |
noutram | 0:5f63e9ea5a3a | 75 | TODO |
noutram | 0:5f63e9ea5a3a | 76 | */ |
noutram | 0:5f63e9ea5a3a | 77 | float updateAverage(float newSample, float buffer[]) |
noutram | 0:5f63e9ea5a3a | 78 | { |
noutram | 0:5f63e9ea5a3a | 79 | //Local variable - remember to initialise! |
noutram | 0:5f63e9ea5a3a | 80 | float average = 0.0; |
noutram | 0:5f63e9ea5a3a | 81 | |
noutram | 0:5f63e9ea5a3a | 82 | //TODO: Move all samples to the right |
noutram | 0:5f63e9ea5a3a | 83 | |
noutram | 0:5f63e9ea5a3a | 84 | |
noutram | 0:5f63e9ea5a3a | 85 | //TODO: Insert new sample |
noutram | 0:5f63e9ea5a3a | 86 | |
noutram | 0:5f63e9ea5a3a | 87 | |
noutram | 0:5f63e9ea5a3a | 88 | //TODO: Calculate average |
noutram | 0:5f63e9ea5a3a | 89 | |
noutram | 0:5f63e9ea5a3a | 90 | //Return the result |
noutram | 0:5f63e9ea5a3a | 91 | return average; |
noutram | 0:5f63e9ea5a3a | 92 | } |