Datasheet

Chapter 5 Tactile Navigation with Whiskers
162Robotics with the BOE Shield-Bot
Remember to set the digital pins to outputs in the setup function so they can
actually supply current to the LEDS:
pinMode(8, OUTPUT); // Left LED indicator -> output
pinMode(2, OUTPUT); // Right LED indicator -> output
Activity 4: Artificial Intelligence for Escaping Corners
You may have noticed that with the last sketch, the BOE Shield-Bot tends to get stuck in
corners. As it enters a corner, its left whisker contacts the wall on the left, so it backs up and
turns right. When the BOE Shield-Bot moves forward again, its right whisker contacts the
wall on the right, so it backs up and turns left. Then it contacts the left wall again, and then
the right wall again, and so on, until somebody rescues it from its predicament.
Programming to Escape Corners
RoamingWithWhiskers can be expanded to detect this problem and act upon it. The trick is
to count the number of times that alternate whiskers make contact with objects. To do this,
the sketch has to remember what state each whisker was in during the previous contact.
Then, it has to compare those states to the current whisker contact states. If they are
opposite, then add 1 to a counter. If the counter goes over a threshold that you (the
programmer) have determined, then it’s time to do a U-turn and escape the corner, and also
reset the counter.
This next sketch relies on the fact that you can nest
if statements, one inside another. The
sketch checks for one condition, and if that condition is true, it checks for another condition
within the first
if statement’s code block. We’ll use this technique to detect consecutive
alternate whisker contacts in the next sketch.
Example Sketch: EscapingCorners
This sketch will cause your BOE Shield-Bot to execute a reverse and U-turn to escape a
corner at either the fourth or fifth alternate whisker press, depending on which one was
pressed first.
With the power switch in Position 1, create, save, and run EscapingCorners.
Test this sketch pressing alternate whiskers as the BOE Shield-Bot roams. It
should execute its reverse and U-turn maneuver after either the fourth or fifth
consecutive, alternate whisker contact.
/*
* Robotics with the BOE Shield - EscapingCorners
* Count number of alternate whisker contacts; if it exceeds 4, get out
* of the corner.
*/