Datasheet

Chapter 4 • BOE Shield-Bot Navigation
106Robotics with the BOE Shield-Bot
As with all motion sketches, the first action setup takes is making the piezospeaker beep.
The
tone function call transmits a signal to digital pin 4 that makes the piezospeaker play a
3 kHz tone that lasts for 1 second. Since the
tone function works in the background while
the code moves on,
delay(1000) prevents the BOE Shield-Bot from moving until the tone is
done playing.
void setup() // Built-in initialization
{
tone(4, 3000, 1000); // Play tone for 1 second
delay(1000); // Delay to finish tone
Next, the servoLeft object instance gets attached to digital pin 13 and
the
servoRight instance gets attached to pin 12. This makes calls
to
servoLeft.writeMicroseconds affect the servo control signals sent on pin 13.
Likewise,
servoRight.writeMicroseconds calls will affect the signals sent on pin 12.
servoLeft.attach(13); // Attach left signal to pin 13
servoRight.attach(12); // Attach right signal to pin 12
Remember that we need the BOE Shield-Bot’s left and right wheels to turn in opposite
directions to drive forward. The function call
servoLeft.writeMicroseconds(1700)
makes the left servo turn full speed counterclockwise, and the function
call
servoRight.writeMicroseconds(1300) makes the right wheel turn full speed
clockwise. The result is forward motion. The
delay(3000) function call keeps the servos
running at that speed for three full seconds. After the delay, s
ervoLeft.detach and
s
ervoRight.detach discontinue the servo signals, which bring the robot to a stop.
// Full speed forward
servoLeft.writeMicroseconds(1700); // Left wheel counterclockwise
servoRight.writeMicroseconds(1300); // Right wheel clockwise
delay(3000); // ...for 3 seconds
servoLeft.detach(); // Stop sending servo signals
servoRight.detach();
}
After the setup function runs out of code, the sketch automatically advances to the loop
function, which repeats itself indefinitely. In this case, we are leaving it empty because the
sketch is done, so it repeats nothing, over and over again, indefinitely.
void loop() // Main loop auto-repeats
{
}