Soccer Robot C - Basic lessons - Exercises - Cross a line
Task: the robot should cross a line and stop after.
Put the robot in front of white line (but not on the line). The robot must cross the line and stop at least 5 cm after the line.
Solution
void RobotSoccer::loop() {
if (setup())
go(50, 0);
if (line(0, 0)){
delayMs(3000);
end();
}
}
This is a simple solution. In the first pass, function's first "if" will start all the devices and start the motors. The robot will be going ahead until transistor 0 of the sensor 0 (the front one) detects the bright line. Now, as the line is detected, the second "if" will evaluate "true" and the 3 commands will be executed. The first one will pause the program for 3 sec. However, as the motors were spinning already, the robot will continue going ahead, despite the program being paused. It is assumed that after 3 seconds it will be far enough, so we will stop the motors here, because "end()" automatically stops all the motors and all the sensors.