Elevator
/
Elevator
The first code
Diff: main.cpp
- Revision:
- 5:47d6ae63a1a3
- Parent:
- 4:059832a0c7b9
- Child:
- 6:922f762e7f92
--- a/main.cpp Thu Feb 12 16:42:25 2015 +0000 +++ b/main.cpp Sat Feb 14 02:30:41 2015 +0000 @@ -2,40 +2,67 @@ #include "time.h" #include "Motor.h" -AnalogIn ain(p17); +AnalogIn ain(p19); Serial pc(USBTX, USBRX); Timer t; Motor m(p25,p27,p28); -float error = Desired_distance - height; + float height; float Lower_distance = 7; // lower floor in inches. -float Upper_distance = 26; // upper floor in inches +float Upper_distance = 22; // upper floor in inches -float Desired_distance = Lower_distance; // start low -int main(void) +float Desired_distance = Upper_distance; // start low +float err; + +int time_count = 0; +int main() + { - t.reset(); //reset timer to zero - t.start(); //start timer data timestamp - m.speed(0.0); - while(1) - { -height = 193.125176919012*(pow(ain.read(),3))-353.927724612106*(pow(ain.read(),2))+218.749286479109*(ain.read())-23.2532105680997; pc.baud(9600); pc.format(7,SerialBase::None,1); - pc.printf("%f,",height); - pc.printf("%f,",ain.read()); //this prints the voltage - pc.printf("%f",t.read()); - pc.printf("\n"); - if (error > 0) - { - m.speed(0.5); + t.reset(); //reset timer to zero + t.start(); //start timer data timestamp + + while(1) { + while(time_count<300) { + + height = 193.125176919012*(pow(ain.read(),3))-353.927724612106*(pow(ain.read(),2))+218.749286479109*(ain.read())-23.2532105680997; + + + + pc.printf("%f,",ain.read()); //this prints the voltage + pc.printf("%f,",height); + pc.printf("%f",t.read()); + pc.printf("\n\r"); + + if(height < Desired_distance) { + + m.speed(0.7); + } else if(height > Desired_distance) { + + m.speed(-0.7); + } else if(height==Desired_distance) { + + m.speed(0.0); + } + + time_count = time_count+1; } - else(error<0) + if (height == Desired_distance) { - m.speed(-0.5) + Desired_distance = Lower_distance; + if(height < Desired_distance) { + + m.speed(0.7); + } else if(height > Desired_distance) { + + m.speed(-0.7); + } else if(height==Desired_distance) { + + m.speed(0.0); } + } } - -} +} \ No newline at end of file