Line Robot - Basic lessons - Exercises - Cross a line
Task: the robot should cross a line and stop after.
Put the robot in front of the line (but not on the line) that is parallel to its reflectance sensor. The robot must cross the line and stop at least 5 cm after the line.
Solution
void RobotLine::loop() {
if (setup())
go(50, 50);
if (line(0)){
delayMs(3000);
stop();
}
}
This is a simple solution. In the first pass, function's first "if" will start all the devices and start both pairs of motors. The robot will be going ahead until transistor 0 detects the line. As the line is parallel to the sensor, in this simple solution we can neglect the rest of the transistors. Now, as the line is detected, the second "if" will evaluate "true" and the 2 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.