Task 4.1.4 Solution
main.cpp@1:7d324d3fbed3, 2017-07-13 (annotated)
- Committer:
- noutram
- Date:
- Thu Jul 13 14:53:08 2017 +0000
- Revision:
- 1:7d324d3fbed3
- Parent:
- 0:7912f2086c2b
updated for mbed-os 5.5
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| noutram | 0:7912f2086c2b | 1 | #include "mbed.h" |
| noutram | 0:7912f2086c2b | 2 | |
| noutram | 0:7912f2086c2b | 3 | #define kRED 4 |
| noutram | 0:7912f2086c2b | 4 | #define kYELLOW 2 |
| noutram | 0:7912f2086c2b | 5 | #define kGREEN 1 |
| noutram | 0:7912f2086c2b | 6 | |
| noutram | 0:7912f2086c2b | 7 | #define fMargin 0.05f |
| noutram | 0:7912f2086c2b | 8 | #define kGYThreshold 0.4f |
| noutram | 0:7912f2086c2b | 9 | #define kGYThresholdL (kGYThreshold - fMargin) |
| noutram | 0:7912f2086c2b | 10 | #define kGYThresholdU (kGYThreshold + fMargin) |
| noutram | 0:7912f2086c2b | 11 | |
| noutram | 0:7912f2086c2b | 12 | #define kYRThreshold 0.6f |
| noutram | 0:7912f2086c2b | 13 | #define kYRThresholdL (kYRThreshold - fMargin) |
| noutram | 0:7912f2086c2b | 14 | #define kYRThresholdU (kYRThreshold + fMargin) |
| noutram | 0:7912f2086c2b | 15 | |
| noutram | 0:7912f2086c2b | 16 | |
| noutram | 0:7912f2086c2b | 17 | //Global objects |
| noutram | 0:7912f2086c2b | 18 | BusOut binaryOutput(D5, D6, D7); //Outputs as an integer |
| noutram | 0:7912f2086c2b | 19 | |
| noutram | 0:7912f2086c2b | 20 | DigitalIn SW1(D3); |
| noutram | 0:7912f2086c2b | 21 | DigitalIn SW2(D4); |
| noutram | 0:7912f2086c2b | 22 | |
| noutram | 0:7912f2086c2b | 23 | AnalogIn AIN(A0); |
| noutram | 0:7912f2086c2b | 24 | float fVin = 0.0; |
| noutram | 0:7912f2086c2b | 25 | unsigned state = kGREEN; |
| noutram | 0:7912f2086c2b | 26 | |
| noutram | 0:7912f2086c2b | 27 | //Main function |
| noutram | 0:7912f2086c2b | 28 | int main() { |
| noutram | 0:7912f2086c2b | 29 | |
| noutram | 0:7912f2086c2b | 30 | while(1) { |
| noutram | 0:7912f2086c2b | 31 | |
| noutram | 0:7912f2086c2b | 32 | //Read ADC |
| noutram | 0:7912f2086c2b | 33 | fVin = AIN; |
| noutram | 0:7912f2086c2b | 34 | |
| noutram | 0:7912f2086c2b | 35 | //Write to terminal |
| noutram | 0:7912f2086c2b | 36 | printf("Analog input = %6.4f\n", fVin); |
| noutram | 0:7912f2086c2b | 37 | |
| noutram | 0:7912f2086c2b | 38 | //Next State Logic |
| noutram | 0:7912f2086c2b | 39 | switch (state) { |
| noutram | 0:7912f2086c2b | 40 | |
| noutram | 0:7912f2086c2b | 41 | case kGREEN: |
| noutram | 0:7912f2086c2b | 42 | if (fVin > kGYThresholdU) { |
| noutram | 0:7912f2086c2b | 43 | state = kYELLOW; |
| noutram | 0:7912f2086c2b | 44 | } |
| noutram | 0:7912f2086c2b | 45 | break; |
| noutram | 0:7912f2086c2b | 46 | |
| noutram | 0:7912f2086c2b | 47 | case kYELLOW: |
| noutram | 0:7912f2086c2b | 48 | if (fVin > kYRThresholdU) { |
| noutram | 0:7912f2086c2b | 49 | state = kRED; |
| noutram | 0:7912f2086c2b | 50 | } else if (fVin < kGYThresholdL) { |
| noutram | 0:7912f2086c2b | 51 | state = kGREEN; |
| noutram | 0:7912f2086c2b | 52 | } |
| noutram | 0:7912f2086c2b | 53 | break; |
| noutram | 0:7912f2086c2b | 54 | |
| noutram | 0:7912f2086c2b | 55 | case kRED: |
| noutram | 0:7912f2086c2b | 56 | if (fVin < kYRThresholdL) { |
| noutram | 0:7912f2086c2b | 57 | state = kYELLOW; |
| noutram | 0:7912f2086c2b | 58 | } |
| noutram | 0:7912f2086c2b | 59 | break; |
| noutram | 0:7912f2086c2b | 60 | |
| noutram | 0:7912f2086c2b | 61 | default: |
| noutram | 0:7912f2086c2b | 62 | state = kGREEN; |
| noutram | 0:7912f2086c2b | 63 | } |
| noutram | 0:7912f2086c2b | 64 | |
| noutram | 0:7912f2086c2b | 65 | //Output logic = f(State) |
| noutram | 0:7912f2086c2b | 66 | binaryOutput = state; |
| noutram | 0:7912f2086c2b | 67 | |
| noutram | 0:7912f2086c2b | 68 | //Wait |
| noutram | 0:7912f2086c2b | 69 | wait(0.1); |
| noutram | 0:7912f2086c2b | 70 | |
| noutram | 0:7912f2086c2b | 71 | } //end while(1) |
| noutram | 0:7912f2086c2b | 72 | } //end main |
Nicholas Outram