University of Plymouth - Stages 1, 2 and 3
/
Task413-FZ429ZI
Updated for the FZ429ZI platform
Fork of Task413 by
main.cpp@2:680e5fdd6d4a, 2018-02-27 (annotated)
- Committer:
- noutram
- Date:
- Tue Feb 27 14:25:03 2018 +0000
- Revision:
- 2:680e5fdd6d4a
- Parent:
- 0:87df0368c584
Silly update with comment
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:87df0368c584 | 1 | #include "mbed.h" |
noutram | 2:680e5fdd6d4a | 2 | //Now with added comments |
noutram | 0:87df0368c584 | 3 | |
noutram | 0:87df0368c584 | 4 | #define kRED (1 << 2) //4 |
noutram | 0:87df0368c584 | 5 | #define kYELLOW (1 << 1) //2 |
noutram | 0:87df0368c584 | 6 | #define kGREEN (1 << 0) //1 |
noutram | 0:87df0368c584 | 7 | |
noutram | 0:87df0368c584 | 8 | //Global objects |
noutram | 0:87df0368c584 | 9 | BusOut binaryOutput(D5, D6, D7); //Outputs as an integer |
noutram | 0:87df0368c584 | 10 | |
noutram | 0:87df0368c584 | 11 | DigitalIn SW1(D3); |
noutram | 0:87df0368c584 | 12 | DigitalIn SW2(D4); |
noutram | 0:87df0368c584 | 13 | |
noutram | 0:87df0368c584 | 14 | AnalogIn AIN(A0); |
noutram | 0:87df0368c584 | 15 | float fVin = 0.0; |
noutram | 0:87df0368c584 | 16 | |
noutram | 0:87df0368c584 | 17 | //Main function |
noutram | 0:87df0368c584 | 18 | int main() { |
noutram | 0:87df0368c584 | 19 | |
noutram | 0:87df0368c584 | 20 | //This represents which state we are in |
noutram | 0:87df0368c584 | 21 | //RED or GREEN |
noutram | 0:87df0368c584 | 22 | int state = 0; |
noutram | 0:87df0368c584 | 23 | |
noutram | 0:87df0368c584 | 24 | while(1) { |
noutram | 0:87df0368c584 | 25 | |
noutram | 0:87df0368c584 | 26 | //Read ADC |
noutram | 0:87df0368c584 | 27 | fVin = AIN; |
noutram | 0:87df0368c584 | 28 | |
noutram | 0:87df0368c584 | 29 | //Write to terminal |
noutram | 0:87df0368c584 | 30 | printf("Analog input = %6.4f\n", fVin); |
noutram | 0:87df0368c584 | 31 | |
noutram | 0:87df0368c584 | 32 | //Now the "state machine" - next state logic |
noutram | 0:87df0368c584 | 33 | switch (state) { |
noutram | 0:87df0368c584 | 34 | //The RED state |
noutram | 0:87df0368c584 | 35 | case 0: |
noutram | 0:87df0368c584 | 36 | //Condition to switch state |
noutram | 0:87df0368c584 | 37 | if (fVin > 0.6f) { |
noutram | 0:87df0368c584 | 38 | state = 1; |
noutram | 0:87df0368c584 | 39 | } |
noutram | 0:87df0368c584 | 40 | break; |
noutram | 0:87df0368c584 | 41 | |
noutram | 0:87df0368c584 | 42 | //The GREEN state |
noutram | 0:87df0368c584 | 43 | case 1: |
noutram | 0:87df0368c584 | 44 | |
noutram | 0:87df0368c584 | 45 | //Condition to switch state |
noutram | 0:87df0368c584 | 46 | if (fVin < 0.4f) { |
noutram | 0:87df0368c584 | 47 | state = 0; |
noutram | 0:87df0368c584 | 48 | } |
noutram | 0:87df0368c584 | 49 | break; |
noutram | 0:87df0368c584 | 50 | |
noutram | 0:87df0368c584 | 51 | default: |
noutram | 0:87df0368c584 | 52 | state = 0; |
noutram | 0:87df0368c584 | 53 | } |
noutram | 0:87df0368c584 | 54 | |
noutram | 0:87df0368c584 | 55 | //Output logic |
noutram | 0:87df0368c584 | 56 | switch (state) { |
noutram | 0:87df0368c584 | 57 | case 0: |
noutram | 0:87df0368c584 | 58 | binaryOutput = kGREEN; |
noutram | 0:87df0368c584 | 59 | break; |
noutram | 0:87df0368c584 | 60 | case 1: |
noutram | 0:87df0368c584 | 61 | binaryOutput = kRED; |
noutram | 0:87df0368c584 | 62 | break; |
noutram | 0:87df0368c584 | 63 | default: |
noutram | 0:87df0368c584 | 64 | binaryOutput = 0; |
noutram | 0:87df0368c584 | 65 | } |
noutram | 0:87df0368c584 | 66 | |
noutram | 0:87df0368c584 | 67 | //Wait |
noutram | 0:87df0368c584 | 68 | wait(0.1); |
noutram | 0:87df0368c584 | 69 | |
noutram | 0:87df0368c584 | 70 | } //end while(1) |
noutram | 0:87df0368c584 | 71 | } //end main |