Character Arrays and switch-case

The last example in this activity 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 use 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

  • Enter, save, and upload ControlWithCharacters to 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]);
  } while(maneuvers[index++] != 's');}  
 
void loop()                                  // Main loop auto-repeats
{                                            // Empty, nothing needs repeating
}

void go(char c)                              // go function
{
  switch(c)                                  // Switch to code 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 seconds
}
  • Try this array—can you guess what it will make the BOE Shield-Bot do?
char maneuvers[] = "fffffffffflllrrrrrrlllbbbbbbbbbbs";

After the char maneuvers array and the usual initialization, these lines fetch the characters from the array and pass them to the go function (explained later).

  int index = 0;
  do
  {
    go(maneuvers[index]);
  } while(maneuvers[index++] != 's');

First, index is declared and initialized to zero, to be used in a do-while loop.  Similar to a regular while loop, do-while repeatedly executes commands inside its code block while a condition is true, but the while part comes after its block so the block always executes at 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.
case 'h':                                // c contains 'h'
  servoLeft.writeMicroseconds(1550);     // Half speed forward
  servoRight.writeMicroseconds(1450);
  break;
  • Add ten or so h characters to your maneuvers character array.  Keep in mind that they have to be added to the left of the s character for the sketch to get to them.
  • Experiment a little, and add another case statement for a different maneuver, such as pivot-backward-left, then add some characters for the new maneuver to your array string.  Can you see how this is a convenient way to build sequences of maneuvers?