Line Robot - Basic lessons - Exercises - Marker at intersection
A very useful exercise for RCJ Rescue Line.
Task: if marker detected, turn.
Follow line. If there is a left marker before the intersection, turn left. The same for right. If none, go straight. As usual, start based on line-following code:
void RobotLine::loop() {
if (line(5))
go(10, 80);
else if (line(3))
go(80, 10);
else
go(60, 60);
}
Solution
void RobotLine::loop() {
if (setup()){
illumination(0, 1);
illumination(1, 1);
}
if (line(5))
go(10, 80);
else if (line(3))
go(80, 10);
else
go(60, 60);
if (line(0) && line(8)){
stop();
delayMs(50);
if (patternColors(0) == 2)
turn(-90);
else if (patternColors(1) == 2){
turn(90);
else{
go(50, 50);
delayMs(500);
}
}
}
- First "if" we know already. Illumination is turned on.
- Second "if" follows line.
- Third "if" contains new elements. First, we stop the robot after both edge transistors found line.
- There is a short delay of 50 ms, to allow the color sensors to read colors well.
- We check left color sensor, assuming that it is number 0. If not, swap 0 and 1, so in this step sensor 1 should be read. If green is detected, turn 90° left.
- The same for the right sensor.
- If no sensor found a green marker, continue straight ahead cross the intersection.
You will have to adjust all the parameters. The ones here are just examples.