Datasheet
Chapter 4 • BOE Shield-Bot Navigation
138 • Robotics with the BOE Shield-Bot
least once. Each time through the loop, go(maneuvers[index]) passes the character
at
maneuvers[index] to the go function. The ++ in index++ adds one to the index variable
for the next time through the loop—recall that this is the post increment operator. This
continues
while(maneuvers[index] != 's') which means “while the value fetched from
the
maneuvers array is not equal to 's' .”
Now, let’s look at the
go function. It receives each character passed to its c parameter, and
evaluates it on a case-by-case basis using a
switch/case statement. For each of the five
letters in the
maneuvers character array, there is a corresponding case statement in
the
switch(c) block that will be executed if that character is received by go.
If the
go function call passes the f character to the c parameter, the code in case f is
executed—the familiar full-speed-forward. If it passes
b, the full-speed backward code gets
executed. The
break in each case exits the switch block and the sketch moves on to the
next command, which is
delay(200). So, each call to go results in a 200 ms maneuver.
Without that break at the end of each case, the sketch would continue through to the code
for the next case, resulting in un-requested maneuvers.
void go(char c) // go function
{
switch(c) // Switch to based on c
{
case 'f': // c contains 'f'
servoLeft.writeMicroseconds(1700); // Full speed forward
servoRight.writeMicroseconds(1300);
break;
case 'b': // c contains 'b'
servoLeft.writeMicroseconds(1300); // Full speed backward
servoRight.writeMicroseconds(1700);
break;
case 'l': // c contains 'l'
servoLeft.writeMicroseconds(1300); // Rotate left in place
servoRight.writeMicroseconds(1300);
break;
case 'r': // c contains 'r'
servoLeft.writeMicroseconds(1700); // Rotate right in place
servoRight.writeMicroseconds(1700);
break;
case 's': // c contains 's'
servoLeft.writeMicroseconds(1500); // Stop
servoRight.writeMicroseconds(1500);
break;
}
delay(200); // Execute for 0.2 s
Your Turn – Add Array Elements and Cases
Try adding a case for half-speed forward. Use the character 'h', and remember that the
linear speed range for the servos is from 1400 to 1600 microsecond pulses.