Moving Backward, Rotating, and Pivoting

All it takes to get other motions out of your BOE Shield-Bot are different combinations of us parameters in your servoLeft and servoRight writeMicroseconds calls.  For example, these two calls will make your BOE Shield-Bot go backwards:

 // Full speed backwards
 servoLeft.writeMicroseconds(1300);   // Left wheel clockwise
 servoRight.writeMicroseconds(1700);  // Right wheel counterclockwise

These two calls will make your BOE Shield-Bot rotate in place to make a left turn:

 // Turn left in place
 servoLeft.writeMicroseconds(1300);   // Left wheel clockwise
 servoRight.writeMicroseconds(1300);  // Right wheel clockwise

These two calls will make your BOE Shield-Bot rotate in place for a right turn:

 // Turn right in place
 servoLeft.writeMicroseconds(1700);   // Left wheel counterclockwise
 servoRight.write Microseconds(1700); // Right wheel counterclockwise

Let’s combine all these commands into a single sketch that makes the BOE Shield-Bot move forward, turn left, turn right, then move backward.

Example Sketch: ForwardLeftRightBackward

  • Enter, save, and upload ForwardLeftRightBackward.
  • Verify that the BOE Shield-Bot makes the forward-left-right-backward motions.
// Robotics with the BOE Shield - ForwardLeftRightBackward
// Move forward, left, right, then backward for testing and tuning.

#include <Servo.h>                           // Include servo library

Servo servoLeft;                             // Declare left and right servos
Servo servoRight;
 
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(2000);                               // ...for 2 seconds

  // Turn left in place
  servoLeft.writeMicroseconds(1300);         // Left wheel clockwise
  servoRight.writeMicroseconds(1300);        // Right wheel clockwise
  delay(600);                                // ...for 0.6 seconds

  // Turn right in place
  servoLeft.writeMicroseconds(1700);         // Left wheel counterclockwise
  servoRight.writeMicroseconds(1700);        // Right wheel counterclockwise
  delay(600);                                // ...for 0.6 seconds

  // Full speed backward
  servoLeft.writeMicroseconds(1300);         // Left wheel clockwise
  servoRight.writeMicroseconds(1700);        // Right wheel counterclockwise
  delay(2000);                               // ...for 2 seconds

  servoLeft.detach();                        // Stop sending servo signals
  servoRight.detach();
}  

void loop()                                  // Main loop auto-repeats
{                                            // Empty, nothing needs repeating
}

TIP — To enter this sketch quickly, use the Arduino software’s Edit menu tools (Copy and Paste) to make four copies of the four lines that make up a maneuver (comment, servoLeft.writeMicroseconds, servoRight.writeMicroseconds, and delay).  Then, modify each one with individual values

Your Turn – Pivoting

You can make the BOE Shield-Bot turn by pivoting around one wheel.  The trick is to keep one wheel still while the other rotates.  Here are the four routines for forward and backward pivot turns:

 // Pivot forward-left
 servoLeft.writeMicroseconds(1500);   // Left wheel stop
 servoRight.writeMicroseconds(1300);  // Right wheel clockwise

 // Pivot forward-right
 servoLeft.writeMicroseconds(1700);   // Left wheel counterclockwise
 servoRight.writeMicroseconds(1500);  // Right wheel stop

 // Pivot backward-left
 servoLeft.writeMicroseconds(1500);   // Left wheel stop
 servoRight.writeMicroseconds(1700);  // Right wheel counterclockwise

 // Pivot backward-right
 servoLeft.writeMicroseconds(1300);     // Left wheel clockwise
 servoRight.writeMicroseconds(1500);    // Right wheel stop
  • Save ForwardLeftRightBackward as PivotTests.
  • Change the us parameter in each writeMicroseconds call so the sketch will perform all four pivot maneuvers: forward, left, right, backward.
  • Run the modified sketch and verify that the different pivot actions work. 
  • Try experimenting with the delay calls for each maneuver so that each one runs long enough to execute a 90° turn.