Purpose:

These sketches are getting long so I am putting them on separate pages . The following is the sample code for using a conditional statement with the sonic ranger.

Step 1:

Copy and past the code into your own sketch and save it appropriately.

/*
Sonic Logic 7/10/2020 -- !! (public domain)

A first exploration of a conditional process and sketch. The objective
is to drive the ranger at a wall and come to rest a particular distance away.
This sketch combines elements of the Motor Control Lab with the Sonic I lab.
Organization and clear comments will be critical to the ultimate success.

The pins that control the motors and the sonic ranger can be found in the
appropriate earlier labs. 

7/7/20
Bruce Emerson

 */

 // math.h is for pulseIn() function for the SR04 ranger.
#include <math.h>

// Motor Control Variables
// initialize variables that will used repetitively during execution
int rightMotor = 5;           // the PWM pin for the right motor
int leftMotor = 6;           // the PWM pin for the left motor
int rightForward = 7;        // forward-reverse right motor
int leftForward = 8;         // forward-reverse left motor
int maxspeed = 250;         // set max motor speed for safety,  255 is absolute max

// motor speeds -- from Motor Control II lab
int motorSlow = 50;
int motorMed = 100;
int motorFast = 150;

// adjustments to balance the motors -- different for every bot!
// I have found that the amount I have to slow one motor down relative
// to the other doesn't seem to depend much on speed. That's why I only 
// have one for forward and one for reverse.
int forwardOffset = -2;      // correction to motor going forward
int reverseOffset = -3;      // correction to motor going in reverse


// settings for turning
int turnSpeed = 50;          // set speed for making turns
int turn90Time = 1820;       // turn time for 90 degrees
int turn180Time = 3640;      // turn time to turn 180 degrees

// Control variables for the SR04 - from Sonic I lab
int trigPin = 3;
int echoPin = 4;

float distance;           // holds the calculated distance
// I can adjust speedSound if the ranger seems to be consistently off
float speedSound = 343.0; // standard speed of sound in m/s
long duration;            // long to provide extra precision for flight time
float safeDistance = 25.0;  // stop distance from wall in cm

void setup() {
  Serial.begin (9600);      // during debug will need this to verify
// declare motor control pins to be outputs:
  pinMode(rightMotor, OUTPUT);
  pinMode(leftMotor, OUTPUT);
  pinMode(rightForward, OUTPUT);
  pinMode(leftForward, OUTPUT);

// declare pins for SR04
  pinMode(trigPin, OUTPUT); // we use the trigPin to talk TO the robot
  pinMode(echoPin, INPUT);  // we use the echoPin to get info FROM the robot
  Serial.print("You have set the stop distance from wall to  ");
  Serial.print(safeDistance);
  Serial.println(" cm.");
  delay(5000); // this allows you 5 s after you start the Qbot to set it down

}

void loop() {
// Check for distance 
  Serial.println("Checking for what is in front of robot");
  Serial.println();
  digitalWrite(trigPin, LOW); // make sure it's off
  delayMicroseconds(10);
  digitalWrite(trigPin, HIGH); // turn the trigger on
  delayMicroseconds(10);       // wait 10 microseconds
  digitalWrite(trigPin, LOW);  // turn the trigger off, this sends the pulse
  
  duration = pulseIn(echoPin, HIGH); 
  distance = (duration / 2) * (speedSound / 10000);

  Serial.print("There is an object in front of me at a distance of ");
  Serial.print(distance);
  Serial.println(" cm");

  if (distance <= safeDistance) {  // are we at target distance to stop?
    Serial.println("Time to Stop!"); 
    // stop motors
    analogWrite(rightMotor, 0);
    analogWrite(leftMotor, 0);
    Serial.println("I'm stopped."); 
    Serial.println();
    delay(5000);
    } else {
    Serial.println("Coast is clear, (re)starting motors");
    // motors on
    analogWrite(rightMotor, motorMed);
    analogWrite(leftMotor, motorMed);
    delay(200);
    } 
 
// loop back to top and measure distance again and decide what to do.
}
      

Step 2:

Start the serial monitor so you can see what the robot thinks is happening. Then turn on the robot while it's still connected to your computer. If you hold it up off the ground it won't try to drive away. Using a book or a piece of paper in front of the robot verify that it will stop the motors if the paper is at the 'safeDistance' or closer. If you move the paper away the motors will restart and the robot will drive forward.

Step 3:

If everything appears to work correctly in Step 2 then unplug the robot (with the power to the robot turned off for safety. Turn the robot on and put it down on the floor a meter or so from a wall. It should drive towards the wall until it gets to the safeDistance and then stop and not move again. If you pick the robot up and move it further from the wall it will restart and do it again. If this all seems solid return to the main lab.