Task 4.1.4

Committer:
noutram
Date:
Thu Jul 13 14:52:56 2017 +0000
Revision:
1:9f488d81a54a
Parent:
0:a4f5053c990a
updated for mbed-os 5.5

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:a4f5053c990a 1 #include "mbed.h"
noutram 0:a4f5053c990a 2 #define kRED 4
noutram 0:a4f5053c990a 3 #define kYELLOW 2
noutram 0:a4f5053c990a 4 #define kGREEN 1
noutram 0:a4f5053c990a 5
noutram 0:a4f5053c990a 6
noutram 0:a4f5053c990a 7 //Global objects
noutram 0:a4f5053c990a 8 BusOut binaryOutput(D5, D6, D7);
noutram 0:a4f5053c990a 9
noutram 0:a4f5053c990a 10 DigitalIn SW1(D3);
noutram 0:a4f5053c990a 11 DigitalIn SW2(D4);
noutram 0:a4f5053c990a 12
noutram 0:a4f5053c990a 13 AnalogIn AIN(A0);
noutram 0:a4f5053c990a 14 float fVin = 0.0;
noutram 0:a4f5053c990a 15
noutram 0:a4f5053c990a 16 //Main function
noutram 0:a4f5053c990a 17 int main() {
noutram 0:a4f5053c990a 18
noutram 0:a4f5053c990a 19
noutram 0:a4f5053c990a 20 while(1) {
noutram 0:a4f5053c990a 21
noutram 0:a4f5053c990a 22 //Read ADC
noutram 0:a4f5053c990a 23 fVin = AIN;
noutram 0:a4f5053c990a 24
noutram 0:a4f5053c990a 25 //Write to terminal
noutram 0:a4f5053c990a 26 //3 decimal places, fieldwidth=5
noutram 0:a4f5053c990a 27 printf("Analog input = %6.4f\n", fVin);
noutram 0:a4f5053c990a 28
noutram 0:a4f5053c990a 29 if (fVin < 0.4f) {
noutram 0:a4f5053c990a 30 binaryOutput = kGREEN;
noutram 0:a4f5053c990a 31 } else if (fVin < 0.6f) {
noutram 0:a4f5053c990a 32 binaryOutput = kYELLOW;
noutram 0:a4f5053c990a 33 } else {
noutram 0:a4f5053c990a 34 binaryOutput = kRED;
noutram 0:a4f5053c990a 35 }
noutram 0:a4f5053c990a 36
noutram 0:a4f5053c990a 37 //Wait
noutram 0:a4f5053c990a 38 wait(0.1);
noutram 0:a4f5053c990a 39
noutram 0:a4f5053c990a 40 } //end while(1)
noutram 0:a4f5053c990a 41 } //end main