The first code

Dependencies:   mbed Motor

Committer:
m172886
Date:
Thu Feb 19 15:46:03 2015 +0000
Revision:
6:922f762e7f92
Parent:
5:47d6ae63a1a3
Child:
7:987db6b7e1b8
Final logic code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
170198 0:2f38be5ec9dc 1 #include "mbed.h"
170198 1:a46a378d5561 2 #include "time.h"
m172886 3:7b0316e7cabc 3 #include "Motor.h"
170198 0:2f38be5ec9dc 4
m172886 5:47d6ae63a1a3 5 AnalogIn ain(p19);
170198 1:a46a378d5561 6 Serial pc(USBTX, USBRX);
170198 1:a46a378d5561 7 Timer t;
m172886 3:7b0316e7cabc 8 Motor m(p25,p27,p28);
m172886 5:47d6ae63a1a3 9
m172886 3:7b0316e7cabc 10 float height;
170198 2:559b5507c580 11
m172886 6:922f762e7f92 12 float Lower_distance = 10; // lower floor in inches.
m172886 3:7b0316e7cabc 13
m172886 6:922f762e7f92 14 float Upper_distance = 20; // upper floor in inches
m172886 3:7b0316e7cabc 15
m172886 6:922f762e7f92 16 float Desired_distance = Lower_distance; // start low
m172886 5:47d6ae63a1a3 17 float err;
m172886 5:47d6ae63a1a3 18
m172886 6:922f762e7f92 19 int time_count;
m172886 5:47d6ae63a1a3 20 int main()
m172886 5:47d6ae63a1a3 21
170198 1:a46a378d5561 22 {
170198 1:a46a378d5561 23 pc.baud(9600);
170198 1:a46a378d5561 24 pc.format(7,SerialBase::None,1);
m172886 5:47d6ae63a1a3 25 t.reset(); //reset timer to zero
m172886 5:47d6ae63a1a3 26 t.start(); //start timer data timestamp
m172886 5:47d6ae63a1a3 27
m172886 5:47d6ae63a1a3 28 while(1) {
m172886 6:922f762e7f92 29 time_count = 0;
m172886 5:47d6ae63a1a3 30 while(time_count<300) {
m172886 5:47d6ae63a1a3 31
m172886 6:922f762e7f92 32
m172886 6:922f762e7f92 33
m172886 5:47d6ae63a1a3 34 height = 193.125176919012*(pow(ain.read(),3))-353.927724612106*(pow(ain.read(),2))+218.749286479109*(ain.read())-23.2532105680997;
m172886 5:47d6ae63a1a3 35
m172886 5:47d6ae63a1a3 36
m172886 5:47d6ae63a1a3 37
m172886 5:47d6ae63a1a3 38 pc.printf("%f,",ain.read()); //this prints the voltage
m172886 5:47d6ae63a1a3 39 pc.printf("%f,",height);
m172886 5:47d6ae63a1a3 40 pc.printf("%f",t.read());
m172886 5:47d6ae63a1a3 41 pc.printf("\n\r");
m172886 5:47d6ae63a1a3 42
m172886 6:922f762e7f92 43 err = Desired_distance-height;
m172886 6:922f762e7f92 44 if(err>0) {
m172886 6:922f762e7f92 45
m172886 5:47d6ae63a1a3 46 m.speed(0.7);
m172886 6:922f762e7f92 47 } else if(err<0) {
m172886 6:922f762e7f92 48
m172886 5:47d6ae63a1a3 49 m.speed(-0.7);
m172886 6:922f762e7f92 50
m172886 5:47d6ae63a1a3 51 }
m172886 5:47d6ae63a1a3 52
m172886 6:922f762e7f92 53 time_count=time_count+1;
m172886 6:922f762e7f92 54
m172886 6:922f762e7f92 55
m172886 6:922f762e7f92 56
m172886 6:922f762e7f92 57
m172886 6:922f762e7f92 58
m172886 4:059832a0c7b9 59 }
m172886 6:922f762e7f92 60 if(Desired_distance==Lower_distance) {
m172886 6:922f762e7f92 61 Desired_distance = Upper_distance;
m172886 6:922f762e7f92 62 }
m172886 6:922f762e7f92 63 else if(Desired_distance==Upper_distance) {
m172886 5:47d6ae63a1a3 64 Desired_distance = Lower_distance;
m172886 5:47d6ae63a1a3 65 }
m172886 6:922f762e7f92 66
m172886 6:922f762e7f92 67
170198 1:a46a378d5561 68 }
m172886 6:922f762e7f92 69
m172886 6:922f762e7f92 70
m172886 5:47d6ae63a1a3 71 }