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
Diff: main.cpp
- Revision:
- 0:928ee2609d39
- Child:
- 1:b74e9a2a3fa5
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Feb 16 22:41:28 2017 +0000 @@ -0,0 +1,46 @@ +#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); + + } +} \ No newline at end of file