Navigation with Arrays

Remember the maneuver function from the last activity? Here are three arrays of values, one for each parameter in the maneuver function. Together, they make up the same forward-left-right-backward-stop sequence we’ve been doing through the chapter.

  //                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};

A sketch can then use this code in one of its functions to execute all the maneuvers:

  // 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]);
  }

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 zeroth 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

  • Enter, save, and upload ManeuverSequence to the Arduino.
  • Verify that the BOE Shield-Bot executes the forward-left-right-backward motions and 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]);
  }
}  
 
void loop()                                  // Main loop auto-repeats
{                                            // Empty, nothing needs repeating
}

void maneuver(int speedLeft, int speedRight, int msTime)
{
  // speedLeft, speedRight ranges: Backward  Linear  Stop  Linear   Forward
  //                               -200      -100......0......100       200
  servoLeft.writeMicroseconds(1500 + speedLeft);   // Set Left servo speed
  servoRight.writeMicroseconds(1500 - speedRight); // Set right servo speed
  if(msTime==-1)                                   // if msTime = -1
  {                                  
    servoLeft.detach();                            // Stop servo signals
    servoRight.detach();   
  }
  delay(msTime);                                   // Delay for msTime
}

Did your BOE Shield-Bot perform the familiar forward-left-right-backward-stop sequence of movements?   Are you thoroughly bored with it by now? Do you want to see your BOE Shield-Bot do something else, or to choreograph your own routine?

Your Turn – Add  Maneuvers to the List

Here’s an example of a longer list you can try.  It does the four pivots after the forward-left-right-backward sequence.  In this example, when index is 4, it’ll use the first number of the second line of each array.  When index is 5, it’ll use the second number on the second line of each array, and so on. Notice that each list of comma-separated array elements is contained within braces { }, and it doesn’t matter whether that list is all on one line or spanning multiple lines. 

int speedsLeft[]  = {200,    -200,    200,      -200,      
                       0,     200,   -200,        0,       0};
int speedsRight[] = {200,     200,   -200,      -200,
                     200,       0,      0,      -200,      0};
int times[]       = {2000,    600,    600,      2000,     
                     1000,   1000,   1000,      1000,     -1};
  • Save ManeuverSequence as ManeuverSequenceExpanded.
  • Change the array so it matches the examples with 9 elements above.
  • Run the modified sketch and verify that the pivot sequence gets executed after the forward-left-right-backward sequence.