Lu-Hsuan Chen
/
autocar_3sensor_mbed
An auto car with 3 IR sensors.
Diff: main.cpp
- Revision:
- 2:e0a553b64315
- Parent:
- 1:ecbf974d0733
- Child:
- 3:812a72bbd664
diff -r ecbf974d0733 -r e0a553b64315 main.cpp --- a/main.cpp Thu Jun 28 07:30:44 2018 +0000 +++ b/main.cpp Fri Jun 29 05:21:46 2018 +0000 @@ -37,14 +37,16 @@ bool left = false; bool middle = false; bool right = false; + + // here I use 500 as threshold + int threshold = 500; - while (true) { - // here I use 500 as threshold - // black: > 500 - // white: < 500 - left = leftIR.read_u16() > 500 ? true : false; - middle = middleIR.read_u16() > 500 ? true : false; - right = rightIR.read_u16() > 500 ? true : false; + while (true) { + // not on track: > 500 + // on track (black): < 500 + left = leftIR.read_u16() < threshold ? true : false; + middle = middleIR.read_u16() < threshold ? true : false; + right = rightIR.read_u16() < threshold ? true : false; led1 = 1; // light on on-board LED @@ -56,7 +58,7 @@ DriveSingleMotor(MOTOR_M1, PWR_STOP, DIR_FORWARD); DriveSingleMotor(MOTOR_M2, PWR_STOP, DIR_FORWARD); // pause for 0.5 sec - wait_ms(1000); + wait_ms(100); // then go backward DriveSingleMotor(MOTOR_M1, 2, DIR_BACKWARD); @@ -90,7 +92,7 @@ DriveSingleMotor(MOTOR_M1, PWR_STOP, DIR_FORWARD); DriveSingleMotor(MOTOR_M2, PWR_STOP, DIR_FORWARD); led1 = 0; // turn of on-board LED - wait_ms(1000); + wait_ms(100); } }