Itty Bitty Buggy: Example 3: Line Avoidance (mDesigner)
Introduction
Itty Bitty Buggy is capable of tracing lines, but what about avoiding them? This example will program Itty Bitty Buggy to avoid a line!
Overview
How can we make it so Itty Bitty Bug avoids a line instead of tracing it?
First, how can we detect a line? We can use the Color Detector to do so. But which block?
The Color Detector - Detect Color block of course! It is capable of detecting colors, which means it can be used to detect a colored line.
Great, so, we are able to detect a line, what next? We need to avoid it. How can we avoid it? We can turn away from the line. But which way?
Since Itty Bitty Buggy has two sets of Color Detectors, one on each side, labeled A (left side) and B (right side). (As labeled on the underside of Buggy Base.)
We can detect which side we are approaching a line, based on which Color Detector senses the line. With this information, we can turn away from the line.
If we detect the line on the A Color Detector (left side), we need to turn right in order to avoid it.
To turn right, we can set the motors speeds to steer right.
If we detect the line on the B Color Detector (right side), we need to turn left in order to avoid it.
To turn left, we can set the motors speeds to steer left.
If we do not detect a line on both the A and B Color Detectors, we are fine with going forward.
To go forward, we can set the motors speeds to move forward. We have the the motors at a slow speed going forward as it gives Itty Bitty Buggy a chance to detect the line. Moving forwarded too fast can cause Itty Bitty Buggy to simply skip over the line without avoiding it.
We can create a flowchart of the logic for this example.
Assembling the Code
As we want the logic to constantly check. We add a Forever loop block around it. In general, it isn't good to loop without a delay as it can cause issues. Therefore, we add a slight delay time of 0.01 seconds. Then we top it off with the When Green Flag Click block to indicate the start of the program. The complete example program is as follows: