Datasheet
Chapter 4 • BOE Shield-Bot Navigation
134 • Robotics with the BOE Shield-Bot
for(int index = 0; index < elementCount; index++)
{
maneuver(speedsLeft[index], speedsRight[index], times[index]);
}
Each time through the loop, index increases by 1. So, with each maneuver call, the next
element in each array gets passed as a parameter to the
maneuver function. The first time
through the loop,
index is 0, so the maneuver call’s parameters become the zero
th
element
from each array, like this:
maneuver(speedsLeft[0], speedsRight[0], times[0]).
The actual values that get passed to the
maneuver function look like this: maneuver(200,
200, 2000)
. The second time through the loop, index is 1, so the function looks like this:
maneuver(speedsLeft[1], speedsRight[1], times[1])
, which
becomes
maneuver(-200, 200, 2000).
Example Sketch – ManeuverSequence
Create, save, and run the ManeuverSequence sketch.
Verify that the robot makes the forward-left-right-backward motions, then stops.
// Robotics with the BOE Shield - ManeuverSequence
// Move forward, left, right, then backward with an array and the
// maneuver function.
#include <Servo.h> // Include servo library
// Forward left right backward stop
// index 0 1 2 3 4
int speedsLeft[] = {200, -200, 200, -200, 0};
int speedsRight[] = {200, 200, -200, -200, 0};
int times[] = {2000, 600, 600, 2000, -1};
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
// Determine number of elements in sequence list.
int elementCount = sizeof(times) / sizeof(int);
// Fetch successive elements from each sequence list and feed them
// to maneuver function.
for(int index = 0; index < elementCount; index++)
{
maneuver(speedsLeft[index], speedsRight[index], times[index]);
}