stop cart

Dependencies:   mbed ContinuousServo

Committer:
m211002
Date:
Wed Apr 17 15:40:42 2019 +0000
Revision:
0:9952fdc226ac
Child:
1:cf9ba941d8c2
Stop cart 4.17

Who changed what in which revision?

UserRevisionLine numberNew contents of line
m211002 0:9952fdc226ac 1 #include "mbed.h"
m211002 0:9952fdc226ac 2 Serial pc(USBTX,USBRX);
m211002 0:9952fdc226ac 3 //sonar sensor
m211002 0:9952fdc226ac 4 AnalogIn sonar(p19); //range sensor 9.8mV/inch
m211002 0:9952fdc226ac 5
m211002 0:9952fdc226ac 6 int main()
m211002 0:9952fdc226ac 7 {
m211002 0:9952fdc226ac 8 while(1) {
m211002 0:9952fdc226ac 9 float distance,x;
m211002 0:9952fdc226ac 10 int num_iterations,i;
m211002 0:9952fdc226ac 11 pc.scanf("%d",&num_iterations);//reads in values from the Matlab
m211002 0:9952fdc226ac 12 for(i=1; i<=num_iterations; i++) { //will go until it meets the number of iterations
m211002 0:9952fdc226ac 13 x = sonar.read();//takes the sensor redings and converts them to x
m211002 0:9952fdc226ac 14 distance=x;
m211002 0:9952fdc226ac 15 //if distance is == to desired distance than stop left and right wheels
m211002 0:9952fdc226ac 16 stopvalue = .033
m211002 0:9952fdc226ac 17 if(x>stopvalue){
m211002 0:9952fdc226ac 18
m211002 0:9952fdc226ac 19 left.speed(.5);
m211002 0:9952fdc226ac 20 right.speed(.5);
m211002 0:9952fdc226ac 21
m211002 0:9952fdc226ac 22 }else{
m211002 0:9952fdc226ac 23 left.stop();
m211002 0:9952fdc226ac 24 right.stop();
m211002 0:9952fdc226ac 25 }
m211002 0:9952fdc226ac 26
m211002 0:9952fdc226ac 27 wait(0.1);
m211002 0:9952fdc226ac 28 pc.printf("%f\n",x);//mbed send out float values for Matlab
m211002 0:9952fdc226ac 29 }
m211002 0:9952fdc226ac 30 }
m211002 0:9952fdc226ac 31 }