Datasheet
Tactile Navigation with Whiskers • Chapter 5
Robotics with the BOE Shield-Bot • 167
statement also has an else condition with a block that sets counter to zero if the whisker
values are not opposite from those of the previous contact.
Study the code in the picture carefully.
Imagine that
wLeft = 0 , wRight = 0 and counter == 3, and think about what
this statement would do.
Imagine that
wLeft = 1 , wRight = 0 , wLeftOld = 0 , wRight = 1
and
counter == 3. Try walking through the code again line by line and explain
what happens to each variable at each step.
Your Turn
One of the if statements in EscapingCorners checks to see if counter has reached 4.
Try increasing the value to 5 and 6 and test the effect. Keep in mind that it will
either count to the number of alternate whisker contacts, or maybe one more
than that depending on which side you start.
// Corner Escape
byte wLeft = digitalRead(5);
byte wRight = digitalRead(7);
if(wLeft != wRight)
{
if ((wLeft != wLeftOld) && (wRight != wRightOld))
{
counter++;
wLeftOld = wLeft;
wRightOld = wRight;
if(counter == 4)
{
wLeft = 0;
wRight = 0;
counter = 0;
}
}
else
{
counter = 0;
}
}