Datasheet
Chapter 5 • Tactile Navigation with Whiskers
156 • Robotics with the BOE Shield-Bot
pinMode(2, OUTPUT); // Right LED indicator -> output
To make the whisker input states control the LEDs, insert these two if...else statements
between the
Serial.println(wRight) and delay(50) commands:
if(wLeft == 0) // If left whisker contact
{
digitalWrite(8, HIGH); // Left LED on
}
else // If no left whisker contact
{
digitalWrite(8, LOW); // Left LED off
}
if(wRight == 0) // If right whisker contact
{
digitalWrite(2, HIGH); // Right LED on
}
else // If no right whisker contact
{
digitalWrite(2, LOW); // Right LED off
}
Recall that if...else statements execute blocks of code based on conditions. Here,
if
wLeft stores a zero, it executes the digitalWrite(8, HIGH) call. If wLeft instead stores
a 1, it executes the
digitalWrite(8, LOW) call. The result? The left LED turns on when
the left whisker is pressed or off when it’s not pressed. The second
if…else statement does
the same job with
wRight and the right LED circuit.
Set the BOE Shield’s power switch to position 1.
Reconnect the Arduino’s programming cable.
Create, save, and run TestWhiskersWithLeds on your Arduino.
Test the sketch by gently pressing each whisker against its 3-pin header post in
the breadboard. The red LEDs on the side of the breadboard where you pressed
the whisker should emit light to indicate that the whisker has made contact.
If both LEDs light up and just stay on no matter what, your power switch is
probably in position 0. Switch it to position 1 and try again.
/*
* Robotics with the BOE Shield - TestWhiskersWithLeds
* Display left and right whisker states in Serial Monitor.
* 1 indicates no contact; 0 indicates contact.
* Display whisker states with LEDs. LED on indicates contact;
* off indicates none.
*/