Datasheet

Robot Control with Distance Detection • Chapter 8
Robotics with the BOE Shield-Bot 257
Example Sketch: FollowingShieldBot
The FollowingShieldBot sketch repeats the proportional control loops just discussed at a
rate of about 25 times per second. So, all the proportional control calculations and servo
speed updates happen 25 times each second. The result is a BOE Shield-Bot that will follow
your hand, a book, or another robot.
Create, save, and run the sketch FollowingShieldBot.
Point the BOE Shield-Bot at an 8 ½ x 11” sheet of paper held in front of it as
though it’s a wall-obstacle. The BOE Shield-Bot should maintain a fixed distance
between itself and the sheet of paper. (It will dance around a little because of the
sensor noise mentioned earlier.)
Rotate the sheet of paper slightly; the BOE Shield-Bot should rotate with it.
Try using the sheet of paper to lead the BOE Shield-Bot around. The BOE Shield-
Bot should follow it.
Move the sheet of paper too close to the BOE Shield-Bot, and it should back up,
away from the paper.
/*
* Robotics with the BOE Shield - FollowingShieldBot
* Use proportional control to maintain a fixed distance between
* BOE Shield-Bot and object in front of it.
*/
#include <Servo.h> // Include servo library
Servo servoLeft; // Declare left and right servos
Servo servoRight;
const int setpoint = 2; // Target distances
const int kpl = -50; // Proportional control constants
const int kpr = -50;
void setup() // Built-in initialization block
{
pinMode(10, INPUT); pinMode(9, OUTPUT); // Left IR LED & Receiver
pinMode(3, INPUT); pinMode(2, OUTPUT); // Right IR LED & Receiver
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
{
int irLeft = irDistance(9, 10); // Measure left distance
int irRight = irDistance(2, 3); // Measure right distance