Line Robot - Basic lessons - Color 2/2
In the last lesson we learned how to read and use HSV values. Now a different approach: recorded patterns. We can record what each color looks like (a pattern) and later ask the sensor, when presented with a new color, which of the existing is the closest match. You must first record the patterns. Follow instructions in
this page, paragraph "Record a pattern". Without this setup You cannot go on because there will be no colors in the sensor to compare with.
The programs here will be very similar to last lesson's. After recording, You can try this program to see how to use the sensor in code, assuming that your pattern number for green is 2:
void RobotLine::loop() {
if (setup()){
illumination(0, 1);
illumination(1, 1);
}
if (patternColors(0) == 2 || patternColors(1) == 2)
print("Green\n\r");
else
print("\n\r");
}
During the first run, commands
illumination(sensorNumber, intensity);
turn on both sensors' (0 and 1, first argument) illumination LEDs, intensity 1 (second argument). In the second "if"
if (patternColors(0) == 2)
checks if the sensor 0 recognizes pattern 2 (green) and the same for sensor 1. If so, "Green" is displayed.
Task: stop on green.
Display "?" (code 63). Follow line. On intersection stop. If there is any marker, display "G" (code 71). Use the following line-following code as a basis for Your program:
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);
sign(63); // ?
}
if (line(5))
go(10, 80);
else if (line(3))
go(80, 10);
else
go(60, 60);
if (line(0) && line(8)){
stop();
if (patternColors(0) == 2 || patternColors(1) == 2)
sign(71); // G
end();
}
}
Explanation:
- The program starts, turns both illumination LEDs on, and displays "?" (in order to clear result of the last run).
- Next block is line-following code we explained already.
- The last "if" stops the robot if both edge transistors detect black line.
- After stopping, both color sensors are checked to see if any of them detects pattern 2 (green). If so, "G" is displayed.
- The program ends.