step 2

Dependencies:   mbed

main.cpp

Committer:
LanierUSNA16
Date:
2014-09-04
Revision:
0:aeb4b85ccd83

File content as of revision 0:aeb4b85ccd83:


#include "mbed.h"
//Lab 2, part 2

//code given by professor
Serial pc(USBTX, USBRX);

//Establish analog input on pin 20, called ain
AnalogIn ain1(p20); 
//create BusOut on MBED leds
BusOut leds(LED1, LED2, LED3, LED4);
//create float variable to store the analog voltage
float signal1 = 0.0;

int main()
    {//begin main
    
        while(1)
        {
            //set computer sampling rate
        pc.baud(921600);
        //store the input analog voltage in the variable signal1
        signal1 = ain1;
        
        if (signal1<=0.2)
        {//if the analog input is less than 0.2, turn all the lights off
            leds=0;  
        }
        
        if((signal1>0.2) && (signal1<=0.4))
        {//if the analog input is between 0.2 and 0.4, turn on only LED1
            leds=1;   
        }
        
        if((signal1>0.4) && (signal1<=0.6))
        {//if the analog input is between 0.4 and 0.6, turn on LED1 and LED2
            leds=3;   
        }
        if((signal1>0.6) && (signal1<=0.8))
        {//if the analog input is between 0.6 and 0.8, turn on all lights, but LED4
            leds=7;   
        }
        if((signal1>0.8) && (signal1<=1.0))
        {//if the analog input is between 0.8 and 1.0, turn on all lights
            leds=15;   
        }
        //print the input voltage to the Tera Term window, so we can double check the lights are turning on when they're supposed to
        printf("%f\n", signal1);
        //wait 0.5 seconds before running the loop again. Keeps the Tera Term window readable. 
        wait(0.5);
        }
    
    }//end main