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.