Datasheet

BOE Shield-Bot Navigation • Chapter 4
Robotics with the BOE Shield-Bot 105
void setup() // Built-in initialization block
{
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
// 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();
}
void loop() // Main loop auto-repeats
{ // Empty, nothing needs repeating
}
How ForwardThreeSeconds Works
First, the Servo library has to be included so that your sketch can access its functions:
#include <Servo.h> // Include servo library
Next, an instance of Servo must be declared and uniquely named for each wheel:
Servo servoLeft; // Declare left & right servos
Servo servoRight;
Instance of an Object: An object is a block of pre-written code that can be copied and re-used
multiple times in a single sketch. Each copy, called an object instance, can be configured
differently. For example, the two Servo declarations create two instances of the object’s code,
named servoLeft and servoRight. Then, functions within each instance can be called and
configured individually. So, servoLeft.attach(13) configures the servoLeft object instance
to send its servo control signals to pin 13. Likewise,
servoRight.attach(12) tells
the
servoRight object instance to send its signals to pin 12.
A sketch automatically starts in its setup function. It runs the code in there once before
moving on to the
loop function, which automatically keeps repeating. Since we only want
the BOE Shield-Bot to go forward and stop once, all the code can be placed in the
setup
function. This leaves the
loop function empty, but that’s okay.