An auto car with 3 IR sensors.

Dependencies:   Ping

Revision:
1:ecbf974d0733
Parent:
0:190c4784b6f4
Child:
2:e0a553b64315
--- a/main.cpp	Wed Jun 27 16:23:33 2018 +0000
+++ b/main.cpp	Thu Jun 28 07:30:44 2018 +0000
@@ -63,12 +63,12 @@
                 DriveSingleMotor(MOTOR_M2, 2, DIR_BACKWARD);
             } else if(left) { // turn left
                 pc.printf("turn left\r\n");
+                DriveSingleMotor(MOTOR_M1, PWR_STOP, DIR_FORWARD);
+                DriveSingleMotor(MOTOR_M2, 2, DIR_FORWARD);
+            } else if(right) { // turn right
+                pc.printf("turn right\r\n");
                 DriveSingleMotor(MOTOR_M1, 2, DIR_FORWARD);
                 DriveSingleMotor(MOTOR_M2, PWR_STOP, DIR_FORWARD);
-            } else if(right) { // turn right
-                pc.printf("turn right\r\n");
-                DriveSingleMotor(MOTOR_M1, PWR_STOP, DIR_FORWARD);
-                DriveSingleMotor(MOTOR_M2, 2, DIR_FORWARD);
             } else { // go straight
                 pc.printf("go straight\r\n");
                 DriveSingleMotor(MOTOR_M1, 2, DIR_FORWARD);
@@ -79,7 +79,7 @@
         }
 
         // if all detects white, which means the car is out of track
-        if(middle && left && right) { // go backward
+        if(!middle && !left && !right) { // go backward
             pc.printf("out of track, go backward\r\n");
             DriveSingleMotor(MOTOR_M1, 2, DIR_BACKWARD);
             DriveSingleMotor(MOTOR_M2, 2, DIR_BACKWARD);