Datasheet
Chapter 4 • BOE Shield-Bot Navigation
136 • Robotics with the BOE Shield-Bot
Character Arrays and switch-case
The last example in this activity is a sketch for performing maneuvers using a list of
characters in an array. Each character represents a certain maneuver, with a 200 ms run
time. Since the run time is fixed, it’s not as flexible as the last approach, but it sure makes it
simple to build a quick sequence of maneuvers.
f = forward b = backward l = left r = right s = stop
Character arrays do not use lists of comma-separated elements. Instead, they use a
continuous string of characters. Here is an example of the same-old forward-left-right-
backward-stop sequence in a character array:
char maneuvers[] = "fffffffffflllrrrbbbbbbbbbbs";
The character array string has 10 f characters. Since each character represents 200 ms of
run time, that takes the BOE Shield-Bot forward for 2 seconds. Next, three
l characters
make 600 ms of left turn. Three
r characters make a right turn, followed by ten
b
characters to go backward, and then an s character for stop completes the sequence.
Example Sketch: ControlWithCharacters
Create, save, and run ControlWithCharacters on the Arduino.
Verify that the BOE Shield-Bot executes the forward-left-right-backward motions
and then stops.
// Robotics with the BOE Shield – ControlWithCharacters
// Move forward, left, right, then backward for testing and tuning.
#include <Servo.h> // Include servo library
char maneuvers[] = "fffffffffflllrrrbbbbbbbbbbs";
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 P13
servoRight.attach(12); // Attach right signal to P12
// Parse maneuvers and feed each successive character to the go function.
int index = 0;
do
{
go(maneuvers[index]);