Line Robot - Basic lessons - Signal sharp bending
This exercise will help You using ML-R 8x8 bicolor display, CAN Bus,UART, 4 switches.
Task: signal sharp bend.
Follow the line with 4 transistors. Each time the robot detects a sharp curve to the left or right, display "S" for 500 ms. Otherwise, display "L". The line-following code is here:
void RobotLine::loop() {
if (line(8))
go(-90, 90);
else if (line(5))
go(-30, 90);
else if (line(3))
go(90, -60);
else if (line(0))
go(90, -90);
else
go(60, 60);
}
Solution
void RobotLine::loop() {
bool inBend = false;
if (line(8))
go(-90, 90), inBend = true;
else if (line(5))
go(-30, 90);
else if (line(3))
go(90, -60);
else if (line(0))
go(90, -90), inBend = true;
else
go(60, 60);
if (inBend)
sign(83); // S
else
sign(76); // L
}
- A boolean variable "inBend" is declared and its value set to "false".
- A bigger block of "if" commands follows, which implements line-following.
- In the line-following block, when any of the 2 edge transistors detects a line, "bendStart" changes its value to "true". Note that You can put 2 C++ commands in the same line, if You separate them with a comma. So:
go(-90, 90), inBend = true;
is equivalent to:
{
go(-90, 90);
inBend = true;
}
- The next "if" tests if "inBend" is true. If so, a sharp bend has just been detected. We change the display to "S".
- If not, we show "L".
- Note that this program sends constantly new requests to display, but that is OK. The sensor will not load CAN Bus as it sends a new command only when the value chages, for example "S" to "L".
If You have a sensor with less than 9 transistors, You will have to decrease 8 to an appropriate lower value.