Datasheet
Chapter 5 • Tactile Navigation with Whiskers
160 • Robotics with the BOE Shield-Bot
== 0)). Translated to English, it reads “if the wLeft variable AND the wRight variable both
equal zero.” If both variables are zero, the two calls in the
if statement’s code block get
executed:
backward(1000) and turnLeft(800).
if((wLeft == 0) && (wRight == 0)) // If both whiskers contact
{
backward(1000); // Back up 1 second
turnLeft(800); // Turn left about 120 degrees
}
In the if…else if…else statement, the sketch skips code blocks with conditions that are
not true, and keeps checking until it either finds a condition that’s true or runs out of
conditions. When the sketch finds a true statement, it executes whatever is in its code block,
then it skips to the end of the
if…else if…else statement without checking any more
conditions, and moves on to whatever else comes next in the sketch.
So, if both whiskers are not pressed, that first
if statement is not true and its code block is
skipped. The sketch will check the first
else if statement. So, maybe the left whisker is
pressed and the calls in this statement’s code block will run. After backing up for one second
and turning right for 0.4 seconds, the sketch skips the rest of the conditions and moves on to
whatever comes after that last
else statement.
else if(wLeft == 0) // If only left whisker contact
{
backward(1000); // Back up 1 second
turnRight(400); // Turn right about 60 degrees
}
If it’s the right whisker that detects an obstacle, the first two code blocks will be skipped, and
the
if(wRight == 0) block will run.
else if(wRight == 0) // If only right whisker contact
{
backward(1000); // Back up 1 second
turnLeft(400); // Turn left about 60 degrees
}
An else condition functions as a catch-all for when none of the statements preceding it were
true. It’s not required, but in this case, it’s useful for when no whiskers are pressed. If that’s
the case, it allows the BOE Shield-Bot to roll forward for 20 ms. Why so little time before the
loop repeats? The small forward time before rechecking allows the BOE Shield-Bot to
respond quickly to changes in the whisker sensors as it rolls forward.
else // Otherwise, no whisker contact
{