Marina Muenster
/
ES20_ElevatorControl
Diff: main.cpp
- Revision:
- 0:19db2de32450
- Child:
- 1:38c836c4bc0f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Mar 09 03:36:06 2016 +0000 @@ -0,0 +1,59 @@ +#include "mbed.h" //includes a library of basic c commands +#include "Motor.h" + +Serial pc(USBTX,USBRX); //enables Serial connection between the mbed and teraterm +AnalogIn dale(p18); //declares input +Motor RickyBobby(p25,p27,p28); //declares output to motor + +int go,cnt,pwm; //These two variables are integers +float number,counter,x,mesHeight,desHeight,err; +int main() +{ + pc.baud(115200); //sets baud rate + + while(1) { //Infinite loop + cnt=0; + number=60; + + //these two lines communicate from the mbed to the teraterm window to collect + //desired values + + pc.printf("Enter one to start, along with the desired height.\r\n"); + pc.scanf("%d,%f",&go,&desHeight); + + while(go==1) { //after the loop has been initialized + + while(cnt<number) //this while loop reads the data from the sensor that corresponds to dale + //and then prints each iteration into the teraterm window + { + x=dale.read(); + mesHeight = ((267.3918*x*x*x) + (-431.3231*x*x) + 236.6525*x + -22.2608); + err = desHeight-mesHeight; + //pc.printf("mesHeight = %f\r\n", mesHeight); + + if (err > 0) { + RickyBobby.speed(1); + pwm = 1; + wait(.05); + } + else if (err < 0) { + RickyBobby.speed(-1); + pwm = -1; + wait(.05); + } + + else { + RickyBobby.speed(0); + pwm = 0; + } + + cnt=cnt+1; + wait(.05); + pc.printf("%d,%f,%d\r\n",cnt,mesHeight,pwm); + + } + RickyBobby.speed(0); + go = 0; + } + } +} \ No newline at end of file