step 2
Dependencies: mbed
main.cpp@0:aeb4b85ccd83, 2014-09-04 (annotated)
- Committer:
- LanierUSNA16
- Date:
- Thu Sep 04 03:05:20 2014 +0000
- Revision:
- 0:aeb4b85ccd83
step 2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
LanierUSNA16 | 0:aeb4b85ccd83 | 1 | |
LanierUSNA16 | 0:aeb4b85ccd83 | 2 | #include "mbed.h" |
LanierUSNA16 | 0:aeb4b85ccd83 | 3 | //Lab 2, part 2 |
LanierUSNA16 | 0:aeb4b85ccd83 | 4 | |
LanierUSNA16 | 0:aeb4b85ccd83 | 5 | //code given by professor |
LanierUSNA16 | 0:aeb4b85ccd83 | 6 | Serial pc(USBTX, USBRX); |
LanierUSNA16 | 0:aeb4b85ccd83 | 7 | |
LanierUSNA16 | 0:aeb4b85ccd83 | 8 | //Establish analog input on pin 20, called ain |
LanierUSNA16 | 0:aeb4b85ccd83 | 9 | AnalogIn ain1(p20); |
LanierUSNA16 | 0:aeb4b85ccd83 | 10 | //create BusOut on MBED leds |
LanierUSNA16 | 0:aeb4b85ccd83 | 11 | BusOut leds(LED1, LED2, LED3, LED4); |
LanierUSNA16 | 0:aeb4b85ccd83 | 12 | //create float variable to store the analog voltage |
LanierUSNA16 | 0:aeb4b85ccd83 | 13 | float signal1 = 0.0; |
LanierUSNA16 | 0:aeb4b85ccd83 | 14 | |
LanierUSNA16 | 0:aeb4b85ccd83 | 15 | int main() |
LanierUSNA16 | 0:aeb4b85ccd83 | 16 | {//begin main |
LanierUSNA16 | 0:aeb4b85ccd83 | 17 | |
LanierUSNA16 | 0:aeb4b85ccd83 | 18 | while(1) |
LanierUSNA16 | 0:aeb4b85ccd83 | 19 | { |
LanierUSNA16 | 0:aeb4b85ccd83 | 20 | //set computer sampling rate |
LanierUSNA16 | 0:aeb4b85ccd83 | 21 | pc.baud(921600); |
LanierUSNA16 | 0:aeb4b85ccd83 | 22 | //store the input analog voltage in the variable signal1 |
LanierUSNA16 | 0:aeb4b85ccd83 | 23 | signal1 = ain1; |
LanierUSNA16 | 0:aeb4b85ccd83 | 24 | |
LanierUSNA16 | 0:aeb4b85ccd83 | 25 | if (signal1<=0.2) |
LanierUSNA16 | 0:aeb4b85ccd83 | 26 | {//if the analog input is less than 0.2, turn all the lights off |
LanierUSNA16 | 0:aeb4b85ccd83 | 27 | leds=0; |
LanierUSNA16 | 0:aeb4b85ccd83 | 28 | } |
LanierUSNA16 | 0:aeb4b85ccd83 | 29 | |
LanierUSNA16 | 0:aeb4b85ccd83 | 30 | if((signal1>0.2) && (signal1<=0.4)) |
LanierUSNA16 | 0:aeb4b85ccd83 | 31 | {//if the analog input is between 0.2 and 0.4, turn on only LED1 |
LanierUSNA16 | 0:aeb4b85ccd83 | 32 | leds=1; |
LanierUSNA16 | 0:aeb4b85ccd83 | 33 | } |
LanierUSNA16 | 0:aeb4b85ccd83 | 34 | |
LanierUSNA16 | 0:aeb4b85ccd83 | 35 | if((signal1>0.4) && (signal1<=0.6)) |
LanierUSNA16 | 0:aeb4b85ccd83 | 36 | {//if the analog input is between 0.4 and 0.6, turn on LED1 and LED2 |
LanierUSNA16 | 0:aeb4b85ccd83 | 37 | leds=3; |
LanierUSNA16 | 0:aeb4b85ccd83 | 38 | } |
LanierUSNA16 | 0:aeb4b85ccd83 | 39 | if((signal1>0.6) && (signal1<=0.8)) |
LanierUSNA16 | 0:aeb4b85ccd83 | 40 | {//if the analog input is between 0.6 and 0.8, turn on all lights, but LED4 |
LanierUSNA16 | 0:aeb4b85ccd83 | 41 | leds=7; |
LanierUSNA16 | 0:aeb4b85ccd83 | 42 | } |
LanierUSNA16 | 0:aeb4b85ccd83 | 43 | if((signal1>0.8) && (signal1<=1.0)) |
LanierUSNA16 | 0:aeb4b85ccd83 | 44 | {//if the analog input is between 0.8 and 1.0, turn on all lights |
LanierUSNA16 | 0:aeb4b85ccd83 | 45 | leds=15; |
LanierUSNA16 | 0:aeb4b85ccd83 | 46 | } |
LanierUSNA16 | 0:aeb4b85ccd83 | 47 | //print the input voltage to the Tera Term window, so we can double check the lights are turning on when they're supposed to |
LanierUSNA16 | 0:aeb4b85ccd83 | 48 | printf("%f\n", signal1); |
LanierUSNA16 | 0:aeb4b85ccd83 | 49 | //wait 0.5 seconds before running the loop again. Keeps the Tera Term window readable. |
LanierUSNA16 | 0:aeb4b85ccd83 | 50 | wait(0.5); |
LanierUSNA16 | 0:aeb4b85ccd83 | 51 | } |
LanierUSNA16 | 0:aeb4b85ccd83 | 52 | |
LanierUSNA16 | 0:aeb4b85ccd83 | 53 | }//end main |