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. 

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, calls to servoRight.writeMicroseconds 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, servoLeft.detach and servoRight.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
{
}

Your Turn – Adjusting Distance

Want to change the distance traveled?  Just change the time in delay(3000).  For example, delay(1500) will make the BOE Shield-Bot go for only half the time, which in turn will make it travel only half as far.  Likewise, delay(6000) will make it go for twice the time, and therefore twice the distance. 

  • Change delay(3000) to delay(1500) and re-upload the sketch. Did the BOE Shield-Bot travel only half the distance?