Chapter 4 Solutions

Question Solutions

  1. Left wheel counterclockwise, right wheel clockwise.
  2. The right wheel is turning clockwise (forward), and the left wheel is not moving.
servoLeft.writeMicroseconds(1500); 
servoRight.writeMicroseconds(1300);
  1. Slow down the right wheel to correct a veer to the left, and slow down the left wheel to correct a veer to the right.  Slow down a wheel by changing its servo’s writeMicroseconds us parameter, using values closer to 1500.   Start at the appropriate end of the linear speed control range (1400–1600), gradually move towards 1500 in increments of 10, and go back in smaller increments if you overshoot.
  2. Given the data below, it should take about 3727 milliseconds to travel 36 inches:

BOE Shield-Bot speed = 11 in/s       

BOE Shield-Bot distance = 36 in/s

time = (BOE Shield-Bot distance / BOE Shield-Bot speed) * (1000 ms / s)

       = (36 / 11 ) * (1000)

       = 3.727272…s * 1000 ms/s

       ≈ 3727 ms

  1. Without that 20 ms (1/50th of a second) delay between each repetition of the loop, it would ramp from 0 to 100 so quickly that it would seem like the BOE Shield-Bot just stepped instantly into full speed.  The ramping would not be apparent.
  2. An array.
  3. for loops and do-while loops were examples from this chapter
  4. switch/case.
  5. do{...}loop while(condition)

Exercise Solutions

  1. Solution:
servoLeft.writeMicroseconds(1300);
servoRight.writeMicroseconds(1700);
delay(2500);
  1. Solution:
// 30/180 = 1/6, so use 1200/6 = 200
servoLeft.writeMicroseconds(1700);
servoRight.writeMicroseconds(1700);
delay(200);

// alternate approach
servoLeft.writeMicroseconds(1700);
servoRight.writeMicroseconds(1700);
delay(1200 * 30 / 180);

// 45/180 = 1/4, so use 1200/4 = 300
servoLeft.writeMicroseconds(1700);
servoRight.writeMicroseconds(1700);
delay(300);

// 60/180 = 1/3, so use 1200/3 = 400
servoLeft.writeMicroseconds(1700);
servoRight.writeMicroseconds(1700);
delay(400);
  1. Solution:
// forward 1 second
servoLeft.writeMicroseconds(1700);
servoRight.writeMicroseconds(1700);
delay(1000);

// ramp into pivot
for(int speed = 0; speed <= 100; speed+=2)
{
  servoLeft.writeMicroseconds(1500);
  servoRight.writeMicroseconds(1500+speed);
  delay(20);
};

// ramp out of pivot
for(int speed = 100; speed >= 0; speed-=2)
{
  servoLeft.writeMicroseconds(1500);
  servoRight.writeMicroseconds(1500+speed);
  delay(20);
}

// forward again
servoLeft.writeMicroseconds(1700);
servoRight.writeMicroseconds(1700);
delay(1000);

Project Solutions

  1. Solution (though the table looks a little different than the one you may have printed out.)

Table completed that shows the relationship between servo control pulse combinations and BOE Shield-Bot behavior

  1. The circle can be implemented by veering right continuously.  Trial and error, and a yard or meter stick, will help you arrive at the right us parameters for writeMicroseconds(us) and the right ms parameter for delay(ms).  Below is a solution that worked for a particular pair of servos and set of batteries.  Your values may vary considerably from what’s in the Circle sketch.

    For the triangle, First calculate the required travel time in ms for a 1 meter or 1 yard straight line, as in Question 4, and fine-tune for your BOE Shield-Bot and particular surface.  The BOE Shield-Bot must travel 1 meter/yard forward, and then make a 120° turn, repeated three times for the three sides of the triangle. You may have to adjust the delay call in the Turn Left 120° routine to get a precise 120° turn.

Circle sketch:

// Robotics with the BOE Shield - Chapter 4, project 2 - Circle
// BOE Shield-Bot navigates a circle of  1 yard diameter.

#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

  // Arc to the right
  servoLeft.writeMicroseconds(1600);  // Left wheel counterclockwise
  servoRight.writeMicroseconds(1438); // Right wheel clockwise slower
  delay(25500);                       // ...for 25.5 seconds
 
  servoLeft.detach();                 // Stop sending servo signals
  servoRight.detach();
}  
 
void loop()                          // Main loop auto-repeats
{                                    // Nothing needs repeating
}

Triangle sketch:

// Robotics with the BOE Shield - Chapter 4, project 2 - Triangle
// BOE Shield-Bot navigates a triangle with 1 yard sides and 120
// degree angles.  Go straight 1 yard, turn 120 degrees, repeat 3 times

#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

  for(int index = 1; index <= 3; index++)
  {
    // Full speed forward
    servoLeft.writeMicroseconds(1700);  // Left wheel counterclockwise
    servoRight.writeMicroseconds(1300); // Right wheel clockwise slower
    delay(5500);                        // ...for 5.5 seconds
    
    // Turn left 120 degrees
    servoLeft.writeMicroseconds(1300);  // Left wheel counterclockwise
    servoRight.writeMicroseconds(1300); // Right wheel clockwise slower
    delay(700);   
  }
  servoLeft.detach();                   // Stop sending servo signals
  servoRight.detach();
}  
 
void loop()                             // Main loop auto-repeats
{                                       // Nothing needs repeating
}