Chapter 5 Solutions

Question Solutions

  1. A normally open, momentary, single-pole, single-throw tactile switch.
  2. Zero (0) volts, resulting in binary zero (0) returned by digitalRead.
    digitalRead(8) == 0 when whisker is pressed. 
    digitalRead(8) == 1 when whisker is not pressed.
  3. digitalRead(7)== 1 means the right whisker is not pressed. 
    digitalRead(7)== 0 means the right whisker is pressed. 
    digitalRead(5)== 1 means the left whisker is not pressed.
    digitalRead(5)== 0 means the left whisker is pressed.
  4. This chapter used if, if…else, and if…else if…else statements to evaluate whisker conditions and call navigation functions.
  5. If one condition turns out to be true, the code might need to evaluate another condition with a nested if statement. 

Exerise Solutions

  1. Since digitalRead returns 1 or 0, your code can multiply digitalRead(5) by 2 and store the result in the whiskers variable.  It can then add the result of digitalRead(7) to the whiskers variable and the result will be 3 for no whiskers.
// Robotics with the BOE Shield Chapter 5, Exercise 1
// Value from 0 to 3 indicates whisker states:
// 0 = both, 1 = left, 2 = right, 3 = neither.

void setup()                       // Built-in initialization block
{
  tone(4, 3000, 1000);             // Play tone for 1 second
  delay(1000);                     // Delay to finish tone

  pinMode(7, INPUT);               // Set right whisker pin to input
  pinMode(5, INPUT);               // Set left whisker pin to input  

  Serial.begin(9600);              // Set data rate to 9600 bps
}  
 
void loop()                        // Main loop auto-repeats
{                                            
  byte whiskers = 2 * digitalRead(5);
  whiskers += digitalRead(7);

  Serial.println(whiskers);        // Display wLeft

  delay(50);                       // Pause for 50 ms
}            
  1. In the if((wLeft == 0)  && (wRight == 0)) block, remove the backward and turnLeft function and replace them with calls to servoLeft.detach and servoRight.detach.
void loop()                             // Main loop auto-repeats
{
  byte wLeft = digitalRead(5);          // Copy right result to wLeft  
  byte wRight = digitalRead(7);         // Copy left result to wRight

  if((wLeft == 0) && (wRight == 0))     // If both whiskers contact
  {
    pause(500);                         // Pause motion for 0.5 seconds
    backward(1000);                     // Back up 1 second
    turnLeft(800);                      // Turn left about 120 degrees
  }
  else if(wLeft == 0)                   // If only left whisker contact
  {
    pause(500);                         // Pause motion for 0.5 seconds
    backward(1000);                     // Back up 1 second
    turnRight(400);                     // Turn right about 60 degrees
  }
  else if(wRight == 0)                  // If only right whisker contact
  {
    pause(500);                         // Pause motion for 0.5 seconds
    backward(1000);                     // Back up 1 second
    turnLeft(400);                      // Turn left about 60 degrees
  }
  else                                  // Otherwise, no whisker contact
  {
    forward(20);                        // Forward 1/50 of a second
  }
}
  1.  Solution:
void pause(int time)                    // Pause drive wheels
{
  servoLeft.writeMicroseconds(1500);    // Left wheel stay still
  servoRight.writeMicroseconds(1500);   // Right wheel stay still
  delay(time);                          // Maneuver for time ms
}
  1. Make sure not to call this pause in the else condition because the forward function is only supposed to go forward for 20 ms before checking the whiskers again.
void loop()                             // Main loop auto-repeats
{
  byte wLeft = digitalRead(5);          // Copy right result to wLeft  
  byte wRight = digitalRead(7);         // Copy left result to wRight

  if((wLeft == 0) && (wRight == 0))     // If both whiskers contact
  {
    pause(500);                         // Pause motion for 0.5 seconds
    backward(1000);                     // Back up 1 second
    turnLeft(800);                      // Turn left about 120 degrees
  }
  else if(wLeft == 0)                   // If only left whisker contact
  {
    pause(500);                         // Pause motion for 0.5 seconds
    backward(1000);                     // Back up 1 second
    turnRight(400);                     // Turn right about 60 degrees
  }
  else if(wRight == 0)                  // If only right whisker contact
  {
    pause(500);                         // Pause motion for 0.5 seconds
    backward(1000);                     // Back up 1 second
    turnLeft(400);                      // Turn left about 60 degrees
  }
  else                                  // Otherwise, no whisker contact
  {
    forward(20);                        // Forward 1/50 of a second
  }
}

Project Solutions

  1. The key to solving this problem is to write a statement that makes a beep with the required parameters. As soon as the beep starts, call the pause function to keep the BOE Shield-Bot still while it beeps.  Make sure not to add any pause calls to the else statement’s code block.  It needs to repeatedly go forward for 20 ms, without any pauses.
// RoamingWithWhiskers  Chapter 5 Project 1
// Go forward.  Back up and turn if whiskers indicate BOE Shield bot
// bumped into something.

#include <Servo.h>                      // Include servo library
 
Servo servoLeft;                        // Declare left and right servos
Servo servoRight;
 
void setup()                            // Built-in initialization block
{
  pinMode(7, INPUT);                    // Set right whisker pin to input
  pinMode(5, INPUT);                    // Set left whisker pin to input  

  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
}  
 
void loop()                             // Main loop auto-repeats
{
  byte wLeft = digitalRead(5);          // Copy right result to wLeft  
  byte wRight = digitalRead(7);         // Copy left result to wRight

  if((wLeft == 0) && (wRight == 0))     // If both whiskers contact
  {
    tone(4, 4000, 100);                 // Play a 0.1 ms tone
    pause(200);                         // Stop for 0.2 seconds
    tone(4, 4000, 100);                 // Play a 0.1 ms tone
    pause(200);                         // Stop for 0.2 seconds
    backward(1000);                     // Back up 1 second
    turnLeft(800);                      // Turn left about 120 degrees
  }
  else if(wLeft == 0)                   // If only left whisker contact
  {
    tone(4, 4000, 100);                 // Play a 0.1 ms tone
    pause(200);                         // Stop for 0.2 seconds
    backward(1000);                     // Back up 1 second
    turnRight(400);                     // Turn right about 60 degrees
  }
  else if(wRight == 0)                  // If only right whisker contact
  {
    tone(4, 4000, 100);                 // Play a 0.1 ms tone
    pause(200);                         // Stop for 0.2 seconds
    backward(1000);                     // Back up 1 second
    turnLeft(400);                      // Turn left about 60 degrees
  }
  else                                      // Otherwise, no whisker contact
  {
    forward(20);                            // Forward 1/50 of a second
  }
}

void pause(int time)                        // Backward function
{
  servoLeft.writeMicroseconds(1500);        // Left wheel clockwise
  servoRight.writeMicroseconds(1500);       // Right wheel counterclockwise
  delay(time);                              // Maneuver for time ms
}

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
}
  1. Start with the Circle sketch from Chapter 4 Solutions.  Comment the detach calls and move the circle code to the loop function and reduce the delay to 50 ms so that it can check the whiskers for contacts 20 times per second.  Then, add the whisker monitoring code with an if statement that reduces or increases a variable that slows the right wheel when the right whisker is pressed, or speeds up the right wheel if the left whisker is pressed.
// Robotics with the BOE Shield - Chapter 5, project 2 – WhiskerCircle
// BOE Shield-Bot navigates a circle of 1 yard diameter.
// Tightens turn if right whisker pressed, or reduces turn if left whisker
// is pressed.

#include <Servo.h>                    // Include servo library
 
Servo servoLeft;                      // Declare left and right servos
Servo servoRight;

int turn;
 
void setup()                          // Built-in initialization block
{
  pinMode(7, INPUT);                    // Set right whisker pin to input
  pinMode(5, INPUT);                    // Set left whisker pin to input  

  tone(4, 3000, 1000);                // Play tone for 1 second
  delay(1000);                        // Delay to finish tone

  servoLeft.attach(13);               // Attach left signal to Port 13
  servoRight.attach(12);              // Attach right signal to Port 12
 
  turn = 0;

//  servoLeft.detach();                 // Stop sending servo signals
//  servoRight.detach();
}  
 
void loop()                          // Main loop auto-repeats
{                                    // Nothing needs repeating
  int wLeft = digitalRead(5);
  int wRight = digitalRead(7);
 
  if(wLeft == 0)
  {
    turn -= 10;
  }
  else if(wRight == 0)
  {
    turn += 10;
  }
 
  // Arc to the right
  servoLeft.writeMicroseconds(1600);  // Left wheel counterclockwise
  servoRight.writeMicroseconds(1438 + turn); // Right wheel clockwise slower
  delay(50);                       // ...for 25.5 seconds
}