![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
stop cart
Dependencies: mbed ContinuousServo
Diff: main.cpp
- Revision:
- 0:9952fdc226ac
- Child:
- 1:cf9ba941d8c2
diff -r 000000000000 -r 9952fdc226ac main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Apr 17 15:40:42 2019 +0000 @@ -0,0 +1,31 @@ +#include "mbed.h" +Serial pc(USBTX,USBRX); +//sonar sensor +AnalogIn sonar(p19); //range sensor 9.8mV/inch + +int main() +{ + while(1) { + float distance,x; + int num_iterations,i; + pc.scanf("%d",&num_iterations);//reads in values from the Matlab + for(i=1; i<=num_iterations; i++) { //will go until it meets the number of iterations + x = sonar.read();//takes the sensor redings and converts them to x + distance=x; + //if distance is == to desired distance than stop left and right wheels + stopvalue = .033 + if(x>stopvalue){ + + left.speed(.5); + right.speed(.5); + + }else{ + left.stop(); + right.stop(); + } + + wait(0.1); + pc.printf("%f\n",x);//mbed send out float values for Matlab + } + } +} \ No newline at end of file