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

Put Maneuvers Into Functions

Let’s try putting the forward, turnLeft, turnRight, and backward navigation routines inside functions.  Here’s an example: 

Example Sketch – MovementsWithSimpleFunctions

  • Enter, save, and upload MovementsWithSimpleFunctions.
// Robotics with the BOE Shield - MovementsWithSimpleFunctions
// 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

  forward(2000);                             // Go forward for 2 seconds
  turnLeft(600);                             // Turn left for 0.6 seconds
  turnRight(600);                            // Turn right for 0.6 seconds
  backward(2000);                            // go backward for 2 seconds

  disableServos();                           // Stay still indefinitely
}  
 
void loop()                                  // Main loop auto-repeats
{                                            // Empty, nothing needs repeating
}

void forward(int time)                       // Forward function
{
  servoLeft.writeMicroseconds(1700);         // Left wheel counterclockwise
  servoRight.writeMicroseconds(1300);        // Right wheel clockwise
  delay(time);                               // Maneuver for time ms
}

void turnLeft(int time)                      // Left turn function
{
  servoLeft.writeMicroseconds(1300);         // Left wheel clockwise
  servoRight.writeMicroseconds(1300);        // Right wheel clockwise
  delay(time);                               // Maneuver for time ms
}

void turnRight(int time)                     // Right turn function
{
  servoLeft.writeMicroseconds(1700);         // Left wheel counterclockwise
  servoRight.writeMicroseconds(1700);        // Right wheel counterclockwise
  delay(time);                               // Maneuver for time ms
}

void backward(int time)                      // Backward function
{
  servoLeft.writeMicroseconds(1300);         // Left wheel clockwise
  servoRight.writeMicroseconds(1700);        // Right wheel counterclockwise
  delay(time);                               // Maneuver for time ms
}

void disableServos()                         // Halt servo signals
{                                            
  servoLeft.detach();                        // Stop sending servo signals
  servoRight.detach();
}

You should recognize the pattern of movement your BOE Shield-Bot makes; it is the same one made by the ForwardLeftRightBackward sketch.  This is a second example of the many different ways to structure a sketch that will result in the same movements.  There will be a few more examples before the end of the chapter. 

Your Turn – Move Function Calls into loop

Want to keep performing that set of four maneuvers over and over again?  Just move those four maneuvering function calls from the setup function into the loop function.  Try this:

  • Save the sketch under a new name, like MovementsWithFunctionsInLoop
  • Comment out the disableServos() function call that’s in setup by placing two forward slashes to its left, like this: // disableServos
  • Remove the // Empty… comment from the loop function—it won’t be correct!

Cut the function calls to forward(2000), turnLeft(600), turnRight(600), and backward(2000) out of the setup function and paste them into the loop function.  It should look like this when you’re done:

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

  // disableServos();             // Stay still indefinitely
}  
 
void loop()                     // Main loop auto-repeats
{                                            
  forward(2000);                // Go forward for 2 seconds
  turnLeft(600);                // Turn left for 0.6 seconds
  turnRight(600);               // Turn right for 0.6 seconds
  backward(2000);               // go backward for 2 seconds
  • Upload the modified sketch and verify that it repeats the sequence of four maneuvers indefinitely.

Printer-friendly version
Function Call with Parameters
Prev
Activity 6: Custom Maneuver Function
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress