Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Task422Solution by
main.cpp@0:6d9e82b27127, 2015-09-24 (annotated)
- Committer:
- noutram
- Date:
- Thu Sep 24 12:29:49 2015 +0000
- Revision:
- 0:6d9e82b27127
Initial version 24-09-2015
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| noutram | 0:6d9e82b27127 | 1 | #include "mbed.h" |
| noutram | 0:6d9e82b27127 | 2 | |
| noutram | 0:6d9e82b27127 | 3 | #define kRED (1 << 2) //4 |
| noutram | 0:6d9e82b27127 | 4 | #define kYELLOW (1 << 1) //2 |
| noutram | 0:6d9e82b27127 | 5 | #define kGREEN (1 << 0) //1 |
| noutram | 0:6d9e82b27127 | 6 | |
| noutram | 0:6d9e82b27127 | 7 | //Global objects |
| noutram | 0:6d9e82b27127 | 8 | BusOut binaryOutput(D5, D6, D7); |
| noutram | 0:6d9e82b27127 | 9 | DigitalIn SW1(D3); |
| noutram | 0:6d9e82b27127 | 10 | DigitalIn SW2(D4); |
| noutram | 0:6d9e82b27127 | 11 | |
| noutram | 0:6d9e82b27127 | 12 | AnalogIn POT_ADC_In(A0); |
| noutram | 0:6d9e82b27127 | 13 | AnalogIn LDD_ADC_In(A1); |
| noutram | 0:6d9e82b27127 | 14 | float fPOT, fLDR = 0.0; |
| noutram | 0:6d9e82b27127 | 15 | |
| noutram | 0:6d9e82b27127 | 16 | //Main function |
| noutram | 0:6d9e82b27127 | 17 | int main() { |
| noutram | 0:6d9e82b27127 | 18 | |
| noutram | 0:6d9e82b27127 | 19 | while(1) { |
| noutram | 0:6d9e82b27127 | 20 | unsigned op = 0u; |
| noutram | 0:6d9e82b27127 | 21 | |
| noutram | 0:6d9e82b27127 | 22 | //Read ADC |
| noutram | 0:6d9e82b27127 | 23 | fPOT = POT_ADC_In; //Threshold |
| noutram | 0:6d9e82b27127 | 24 | fLDR = LDD_ADC_In; //Light reading |
| noutram | 0:6d9e82b27127 | 25 | |
| noutram | 0:6d9e82b27127 | 26 | //Write to terminal |
| noutram | 0:6d9e82b27127 | 27 | printf("POT = %6.4f\tLDR = %6.4f\n", fPOT, fLDR); |
| noutram | 0:6d9e82b27127 | 28 | |
| noutram | 0:6d9e82b27127 | 29 | if (fLDR < 0.1f) { |
| noutram | 0:6d9e82b27127 | 30 | op = kRED | kYELLOW | kGREEN; |
| noutram | 0:6d9e82b27127 | 31 | } else if (fLDR < 0.15f) { |
| noutram | 0:6d9e82b27127 | 32 | op = kYELLOW | kRED; |
| noutram | 0:6d9e82b27127 | 33 | } else if (fLDR < 0.2f) { |
| noutram | 0:6d9e82b27127 | 34 | op = kRED; |
| noutram | 0:6d9e82b27127 | 35 | } else { |
| noutram | 0:6d9e82b27127 | 36 | op = 0; |
| noutram | 0:6d9e82b27127 | 37 | } |
| noutram | 0:6d9e82b27127 | 38 | |
| noutram | 0:6d9e82b27127 | 39 | //Write to port |
| noutram | 0:6d9e82b27127 | 40 | binaryOutput = op; |
| noutram | 0:6d9e82b27127 | 41 | |
| noutram | 0:6d9e82b27127 | 42 | //Wait |
| noutram | 0:6d9e82b27127 | 43 | wait(0.1); |
| noutram | 0:6d9e82b27127 | 44 | |
| noutram | 0:6d9e82b27127 | 45 | } //end while(1) |
| noutram | 0:6d9e82b27127 | 46 | } //end main |
