Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • Robotics with the Board of Education Shield for Arduino

Robotics with the Board of Education Shield for Arduino

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.

Printer-friendly version
Using Array Elements
Prev
Character Arrays and switch-case
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress