Task 4.1.2 Solution

Dependencies:   mbed

Committer:
noutram
Date:
Thu Sep 24 12:28:30 2015 +0000
Revision:
0:88a5dc4e8e83
Initial version 24-09-2015

Who changed what in which revision?

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