Robot code for searching an object and charging at it.

Dependencies:   HCSR04 Motor mbed

Revision:
10:cec68ef272cd
Parent:
8:43a650f9d4af
Parent:
9:7770a84228c0
Child:
12:703f75ae2453
--- a/main.cpp	Sat May 23 17:08:45 2015 +0000
+++ b/main.cpp	Sat May 23 17:48:01 2015 +0000
@@ -79,21 +79,33 @@
 
     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);
+        // Sample code to detect and object and charge at it
+        if (detect_object(range, searchspeed)) {
+            while (true) {
+                charge(chargespeed);
+
+                detect = detect_line();
+                // If line is detected from front then reverse
+                if(detect == 1) {
+                    reverse(searchspeed);
+                    wait(2);
+                    stop();
+                    break;
+                    // If line is detected from back just keep on moving forward
+                } else if (detect == -1) {
+                    wait(2);
+                    stop();
+                    break;
+                }
+            }
+        } else {
+            moverandom();
+        }
         stop();
-          
-    } else {
-        moverandom();  
-  
-    }
-    
-    stop();
     }
 }