![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
code using the sonic sensor to activate motor when the distance value dips beneath 50cm, seems to be a problem compiling with the else if statement. Apparently an unclosed bracket.
Dependencies: SRF02 Servo mbed
main.cpp
- Committer:
- mspeed93
- Date:
- 2017-02-16
- Revision:
- 0:928ee2609d39
- Child:
- 1:b74e9a2a3fa5
File content as of revision 0:928ee2609d39:
#include "mbed.h" #include "SRF02.h" #include "Servo.h" Serial pc(USBTX, USBRX); //tx, rx SRF02 sensor(p28,p27, 0xE0); Servo cr_srv(p21); DigitalOut myled(LED1); void init_servo(){ //calibrate the servos for +/-45deg cr_srv.calibrate(0.0005,45); } int main() { float pos_val=0.5; //variable to hold posistion values float step_size=0.01; //position increment/decrement value float cr_srv_cmd=0.5; //continuous rotation servo command value (range: 0.0 - 1.0) pc.baud(921600); while(1){ int distance = sensor.getDistanceCm(); pc.printf("Distance = %d cm\n",distance); if (distance <= 50) myled = 0; cr_srv_cmd = 0.5; //use a smaller scale for the cr servo due to its limited sensitivity cr_srv.write(cr_srv_cmd); //write to the servo pos_val = pos_val + step_size; if (pos_val > 1.0) pos_val = 0.0; wait(0.5); else if (distance > 50) myled = 1; cr_srv_cmd = 0.9; cr_srv.write(cr_srv_cmd); pos_val = pos_val + step_size; if (pos_val > 1.0) pos_val = 0.0; wait(0.5); } }