Robot code for searching an object and charging at it.
Dependencies: HCSR04 Motor mbed
Diff: main.cpp
- Revision:
- 16:02e533e3a91c
- Parent:
- 12:703f75ae2453
- Child:
- 18:7bd638e3926d
--- a/main.cpp Sat May 30 15:44:01 2015 +0000 +++ b/main.cpp Sat May 30 16:05:07 2015 +0000 @@ -80,33 +80,44 @@ Timer t; t.start(); - int detect = 0; + int detect_l = 0; + int detect_o = 0; while(1) { // Sample code to detect and object and charge at it - if (detect_object(range, searchspeed)) { - while (true) { - charge(chargespeed); + + detect_o = detect_object(range, searchspeed) + + if (detect_o == 1) { + + charge(chargespeed); - detect = detect_line(); + while (true) { + + detect_l = detect_line(); // If line is detected from front then reverse - if(detect == 1) { + if(detect_l == 1) { reverse(searchspeed); wait(2); stop(); - detect = 0 + detect_l = 0 break; // If line is detected from back just keep on moving forward - } else if (detect == -1) { + } else if (detect_l == -1) { wait(2); stop(); - detect = 0 + detect_l = 0 break; } } + } else { + moverandom(); + } + + detect_o = 0; stop(); } }