An auto car with 3 IR sensors.

Dependencies:   Ping

Revision:
7:4edd049209a6
Parent:
6:f34538eea724
Child:
9:ce1dd89ff5e8
--- a/main.cpp	Fri Jun 29 13:12:48 2018 +0000
+++ b/main.cpp	Sat Jun 30 01:45:13 2018 +0000
@@ -1,6 +1,10 @@
 #include "mbed.h"
 
+// trigger button and triggered LED
 DigitalOut led1(LED1);
+DigitalIn pb(PC_13);
+int lastButtonState = 0;
+bool ledState = false;
 
 Serial pc(SERIAL_TX, SERIAL_RX);
 
@@ -43,6 +47,25 @@
     int threshold = 500;
 
     while (true) {
+        int reading1 = pb.read();
+        
+        if(reading1 != lastButtonState) {
+            wait_ms(20);
+            
+            int reading2 = pb.read();
+            
+            if(reading2 == reading1) {
+                lastButtonState = reading2;
+            }
+            
+            if(lastButtonState == 1) {
+                ledState = !ledState;
+            }
+        }
+        
+        led1.write(ledState);
+        
+        if(ledState) {
         // not on track: > 500
         // on track (black): < 500
         left = leftIR.read_u16() < threshold ? true : false;
@@ -50,6 +73,7 @@
         right = rightIR.read_u16() < threshold ? true : false;
 
         driveMotor(left, middle, right);
+        }
     }
 }