Datasheet
Chapter 5 • Tactile Navigation with Whiskers
172 • Robotics with the BOE Shield-Bot
turnRight(400); // Turn right about 60 degrees
}
else if(wRight == 0) // If only right whisker contact
{
pause(500); // Pause motion for 0.5 seconds
backward(1000); // Back up 1 second
turnLeft(400); // Turn left about 60 degrees
}
else // Otherwise, no whisker contact
{
forward(20); // Forward 1/50 of a second
}
}
Project Solutions
1. The key to solving this problem is to write a statement that makes a beep with
the required parameters. As soon as the beep starts, call the
pause function to
keep the BOE Shield-Bot still while it beeps. Make sure not to add any
pause calls
to the
else statement’s code block. It needs to repeatedly go forward for 20 ms,
without any pauses.
// RoamingWithWhiskers Chapter 5 Project 1
// Go forward. Back up and turn if whiskers indicate BOE Shield bot
// bumped into something.
#include <Servo.h> // Include servo library
Servo servoLeft; // Declare left and right servos
Servo servoRight;
void setup() // Built-in initialization block
{
pinMode(7, INPUT); // Set right whisker pin to input
pinMode(5, INPUT); // Set left whisker pin to input
tone(4, 3000, 1000); // Play tone for 1 second
delay(1000); // Delay to finish tone
servoLeft.attach(13); // Attach left signal to pin 13
servoRight.attach(12); // Attach right signal to pin 12
}
void loop() // Main loop auto-repeats
{
byte wLeft = digitalRead(5); // Copy right result to wLeft
byte wRight = digitalRead(7); // Copy left result to wRight
if((wLeft == 0) && (wRight == 0))// If both whiskers contact
{