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

Activity 6: Custom Maneuver Function

The last sketch, MovementsWithSimpleFunctions, was kind of long and clunky. And, the four functions it uses to drive the robot are almost the same. The TestManeuverFunction sketch takes advantage of those function’s similarities and streamlines the code.

TestManeuverFunction has a single function for motion named maneuver that accepts three parameters: speedLeft, speedRight, and msTime:

void maneuver(int speedLeft, int speedRight, int msTime)

The rules for speedLeft and speedRight, listed below, are easy to remember. Best of all, with this maneuver function you don’t have to think about clockwise and counterclockwise rotation anymore.

  • positive values for moving the robot forward
  • negative values for moving the robot backward
  • 200 for full speed forward
  • –200 for full speed backward
  • 0 for stop
  • 100 to –100 range for linear speed control

The rules for msTime are:

  • Positive values for the number of ms to execute the maneuver
  • –1 to disable the servo signal

Here is what calls to this function will look like for the familiar forward-backward-left-right-stop sequence:

maneuver(200, 200, 2000);           // Forward 2 seconds
maneuver(-200, 200, 600);           // Left 0.6 seconds
maneuver(200, -200, 600);           // Right 0.6 seconds
maneuver(-200, -200, 2000);         // Backward 2 seconds
maneuver(0, 0, -1);                 // Disable servos

Example Sketch – TestManeuverFunction

  • Enter, save, and upload TestManeuverFunction.
  • Verify that it completes the forward, left, right, backward, stop sequence.
// Robotics with the BOE Shield - TestManeuverFunction
// Move forward, left, right, then backward with maneuver function.

#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
 
  maneuver(200, 200, 2000);                  // Forward 2 seconds
  maneuver(-200, 200, 600);                  // Left 0.6 seconds
  maneuver(200, -200, 600);                  // Right 0.6 seconds
  maneuver(-200, -200, 2000);                // Backward 2 seconds
  maneuver(0, 0, -1);                        // Disable servos
}  
 
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
}

Your Turn – Customize Speed and Duration Control

With the maneuver function, 0 is stop, 100 to –100 is the speed control range, and 200 and –200 are overkill to keep the servos running as fast as they possibly can.

The TestManeuverFunction sketch makes it easy to define custom  maneuvers quickly. Just pass new parameters for each wheel rotation and maneuver duration to each call of the maneuver function.  For example, let’s make the left wheel move at half speed while the right wheel moves at full speed to draw an arc for 3 seconds.  Here is what that function call would look like:

  maneuver(50, 100, 3000);

Here is another example that keeps the left wheel still and moves the right wheel forward for a left pivot turn:  

  maneuver(0, 200, 1200);
  • Try adding both of the example maneuver calls to your sketch.
  • Try adding the other three wheel-pivot turns to the sequence: forward-right, backward-right, and backward-left.

Printer-friendly version
Put Maneuvers Into Functions
Prev
Activity 7: Maneuver Sequences with Arrays
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress