Datasheet
Chapter 5 • Tactile Navigation with Whiskers
154 • Robotics with the BOE Shield-Bot
Next, Serial.print displays the value of wLeft to the Serial Monitor,
and
Serial.println displays the value of wRight and a carriage return.
Serial.print(wLeft); // Display left whisker state
Serial.println(wRight); // Display right whisker state
Before the next repetition of the loop function, there’s a delay(50). This slows down the
number of messages the Serial Monitor receives each second. Although it’s probably not
needed, we leave it in to prevent possible computer buffer overruns (too much data to store)
for older hardware and certain operating systems.
Your Turn – Nesting Function Calls
Your sketch doesn’t actually need to use variables to store the values from digitalRead.
Instead, the (1 or 0) value that
digitalRead returns can be used directly by nesting the
function call inside
Serial.print and sending its return value straight to the Serial
Monitor. In that case, your
loop function would look like this:
void loop() // Main loop auto-repeats
{
Serial.print(digitalRead(5)); // Display wLeft
Serial.println(digitalRead(7)); // Display wRight
delay(50); // Pause for 50 ms
}
Replace the loop function with the one above, load the sketch, and test the
whiskers to verify that it functions the same.
Activity 2: Field-Test the Whiskers
What if you have to test the whiskers at some later time away from a computer? In that case,
the Serial Monitor won’t be available, so what can you do? One solution would be to use LED
circuits to display the whisker states. All it takes is a simple sketch that turns an LED on
when a whisker is pressed or off when it’s not pressed.
Parts List:
(2) resistors, 220 Ω (red-red-brown)
(2) LEDs, red
Build the LED Whisker Testing Circuits
Unplug the BOE Shield-Bot’s battery pack and USB cables.
Add the circuit shown below.