Robot code for searching an object and charging at it.

Dependencies:   HCSR04 Motor mbed

Revision:
9:7770a84228c0
Parent:
6:af897173cb75
Child:
10:cec68ef272cd
--- a/main.cpp	Sat May 23 16:09:43 2015 +0000
+++ b/main.cpp	Sat May 23 17:09:28 2015 +0000
@@ -79,14 +79,29 @@
 
     Timer t;
     t.start();
+    
+    int detect = 0;
 
     //while(1) {
     // Sample code to detect and object and charge at it
     if (detect_object(range, searchspeed)) {
-         
-        charge(chargespeed);
-        wait(3);
-        stop();
+        while (true) {
+            charge(chargespeed);
+            
+            detect = detect_line();
+            // If line is detected from front then reverse
+            if(detect == 1) {
+                reverse(chargespeed);
+                wait(2);
+                stop();
+                break;
+            // If line is detected from back just keep on moving forward
+            } else if (detect == -1) {
+                wait(2);
+                stop();
+                break;
+            }
+        }
           
     } else {