Nicholas Outram
/
Task431Solution
Task 4.3.1 Solution
main.cpp@1:1fd1b4a299eb, 2017-07-13 (annotated)
- Committer:
- noutram
- Date:
- Thu Jul 13 14:53:48 2017 +0000
- Revision:
- 1:1fd1b4a299eb
- Parent:
- 0:8f4002068b49
updated for mbed-os 5.5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:8f4002068b49 | 1 | #include "mbed.h" |
noutram | 0:8f4002068b49 | 2 | |
noutram | 0:8f4002068b49 | 3 | //Global objects |
noutram | 0:8f4002068b49 | 4 | BusOut binaryOutput(D5, D6, D7); |
noutram | 0:8f4002068b49 | 5 | AnalogIn POT_ADC_In(A0); |
noutram | 0:8f4002068b49 | 6 | AnalogIn LDD_ADC_In(A1); |
noutram | 0:8f4002068b49 | 7 | |
noutram | 0:8f4002068b49 | 8 | float fPOT, fLDR = 0.0; |
noutram | 0:8f4002068b49 | 9 | float fPOTAverage = 0.0, fLDRAverage = 0.0; |
noutram | 0:8f4002068b49 | 10 | unsigned int n = 0; |
noutram | 0:8f4002068b49 | 11 | unsigned int N = 10; |
noutram | 0:8f4002068b49 | 12 | |
noutram | 0:8f4002068b49 | 13 | //Main function |
noutram | 0:8f4002068b49 | 14 | int main() { |
noutram | 0:8f4002068b49 | 15 | |
noutram | 0:8f4002068b49 | 16 | while(1) { |
noutram | 0:8f4002068b49 | 17 | |
noutram | 0:8f4002068b49 | 18 | //Read ADC |
noutram | 0:8f4002068b49 | 19 | fPOT = POT_ADC_In; |
noutram | 0:8f4002068b49 | 20 | fLDR = LDD_ADC_In; |
noutram | 0:8f4002068b49 | 21 | |
noutram | 0:8f4002068b49 | 22 | //Calculate the average of both fPOT and fLDR |
noutram | 0:8f4002068b49 | 23 | fPOTAverage += fPOT; |
noutram | 0:8f4002068b49 | 24 | fLDRAverage += fLDR; |
noutram | 0:8f4002068b49 | 25 | |
noutram | 0:8f4002068b49 | 26 | //Update the sample count |
noutram | 0:8f4002068b49 | 27 | n++; |
noutram | 0:8f4002068b49 | 28 | |
noutram | 0:8f4002068b49 | 29 | //Wait |
noutram | 0:8f4002068b49 | 30 | wait(0.01); |
noutram | 0:8f4002068b49 | 31 | |
noutram | 0:8f4002068b49 | 32 | //Do we have N samples? |
noutram | 0:8f4002068b49 | 33 | if (n == N) { |
noutram | 0:8f4002068b49 | 34 | |
noutram | 0:8f4002068b49 | 35 | //Calculate average |
noutram | 0:8f4002068b49 | 36 | fPOTAverage /= (float)N; |
noutram | 0:8f4002068b49 | 37 | fLDRAverage /= (float)N; |
noutram | 0:8f4002068b49 | 38 | |
noutram | 0:8f4002068b49 | 39 | //Write to terminal |
noutram | 0:8f4002068b49 | 40 | printf("POT = %6.4f\tLDR = %6.4f\n", fPOTAverage, fLDRAverage); |
noutram | 0:8f4002068b49 | 41 | |
noutram | 0:8f4002068b49 | 42 | //Check the threshold |
noutram | 0:8f4002068b49 | 43 | if (fLDRAverage > fPOTAverage) { |
noutram | 0:8f4002068b49 | 44 | binaryOutput = 0; //Binary 000 |
noutram | 0:8f4002068b49 | 45 | } else { |
noutram | 0:8f4002068b49 | 46 | binaryOutput = 7; //Binary 111 |
noutram | 0:8f4002068b49 | 47 | } |
noutram | 0:8f4002068b49 | 48 | |
noutram | 0:8f4002068b49 | 49 | //RESET averages and sample count |
noutram | 0:8f4002068b49 | 50 | n = 0; |
noutram | 0:8f4002068b49 | 51 | fPOTAverage = 0.0; |
noutram | 0:8f4002068b49 | 52 | fLDRAverage = 0.0; |
noutram | 0:8f4002068b49 | 53 | } |
noutram | 0:8f4002068b49 | 54 | |
noutram | 0:8f4002068b49 | 55 | |
noutram | 0:8f4002068b49 | 56 | } //end while(1) |
noutram | 0:8f4002068b49 | 57 | } //end main |