NewPing + AFMotor + If Statement

I am very new to writing code and would be grateful for any help with the following.

I am building an object avoidance car. I have an Adafruit Motor Shield, two motors for my wheels, and an HCSR04 Ultrasonic Sensor. So far I have successfully been able to run the motors and the sensors on separate occasions using the two codes below.

My question is how do I combine the two sketches and write an if statement to stop the motors when the car gets too close to an object. If I could get some guidance just making it stop, I can continue to build the code to stop, backup, and turn.

First Code

#include <AFMotor.h>

AF_DCMotor motor2(2);
AF_DCMotor motor4(4);

void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Dual Motor test!");

// turn on motor
motor2.setSpeed(200);
motor4.setSpeed(200);

motor2.run(RELEASE);
motor4.run(RELEASE);
}

void loop() {
uint8_t i;

motor2.run(FORWARD);
motor4.run(BACKWARD);
for (i=0; i<255; i++)
{
motor2.setSpeed(i);
motor4.setSpeed(i);
delay(10);
}

for (i=255; i!=0; i--)
{
motor2.setSpeed(i);
motor4.setSpeed(i);
delay(10);
}
}

Second Code

#include <NewPing.h>

#define TRIGGER_PIN 5 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 6 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
int cm = sonar.ping_cm(); // Send out the ping, get the results in centimeters.
Serial.print("Ping: ");
Serial.print(cm); // Print the result (0 = outside the set distance range, no ping echo)
Serial.println("cm");

}

My question is how do I combine the two sketches and write an if statement to stop the motors when the car gets too close to an object.

Look at what part of the first sketch makes the motors go.

motor2.setSpeed(200);
  motor4.setSpeed(200);
  motor2.run(FORWARD);
  motor4.run(BACKWARD);

Setting the speed to another value will make it stop.

In the second sketch, you simply read and print the distance. It is easy to add an if statement to make something happen if the distance is greater than, or less than, some value.

Thanks for the quick reply!

OK so it took me a while but I was able to combine the two codes without errors.

One problem I am running into is the ping time printing to the serial monitor is very slow about 7 secs despite reducing the delay from 50 to 29. I am thinking it has something to do with combining the two codes because it was very fast just running the NewPing sketch.

Here is the latest code I have uploaded to the Arduino.

#include <AFMotor.h>
#include <NewPing.h>

#define TRIGGER_PIN  5  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     6  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

AF_DCMotor motor2(2);
AF_DCMotor motor4(4);

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  Serial.println("Object Avoidance Project!");

  // turn on motor
  motor2.setSpeed(200);
  motor4.setSpeed(200);

  motor2.run(RELEASE);
  motor4.run(RELEASE);
  
}

void loop() {
  uint8_t i;
  
  motor2.run(FORWARD);
  motor4.run(BACKWARD);
  for (i=0; i<255; i++) 
  {
    motor2.setSpeed(i);  
    motor4.setSpeed(i);  
    delay(10);
  }

  for (i=255; i!=0; i--) 
  {
    motor2.setSpeed(i);  
    motor4.setSpeed(i);  
    delay(10);
  }
   delay(50);                // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  int cm = sonar.ping_cm(); // Send out the ping, get the results in centimeters.
  Serial.print("Ping: ");
  Serial.print(cm);         // Print the result (0 = outside the set distance range, no ping echo)
  Serial.println("cm");
  }

I am thinking it has something to do with combining the two codes because it was very fast just running the NewPing sketch.

Yes, it does. I don't see the purpose of those two for loops that ramp up and down the speed of the motors. That was fine in the demo sketch, but doesn't really reflect anything you want the real robot to do.

I took the unnecessaries you mentioned out and the pings behaved a little better but I started getting a lot of 0s printing in the serial monitor. So I methodically began removing code relating to the motor. When I removed motor2.setSpeed(200); motor4.setSpeed(200); the pings returned to normal and displayed the correct distances.
Of course I need these remarks to make the motors start up and run. Any ideas why this is happening and perhaps what I could replace them with?

Here is the latest sketch with the motor2.setSpeed(200); motor4.setSpeed(200);

#include <NewPing.h>
#include <AFMotor.h>

#define TRIGGER_PIN  5  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     6  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 100 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

AF_DCMotor motor2(2);
AF_DCMotor motor4(4);

void setup() {
  Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results.
 
 // turn on motor
 motor2.setSpeed(200);
 motor4.setSpeed(200);

 
}

void loop() {
  
  delay(50);                // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  int cm = sonar.ping_cm(); // Send out the ping, get the results in centimeters.
  Serial.print("Ping: ");
  Serial.print(cm);         // Print the result (0 = outside the set distance range, no ping echo)
  Serial.println("cm");
  
 }

#define TRIGGER_PIN 5 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 6 // Arduino pin tied to echo pin on the ultrasonic sensor.

These are PWM pins. Try using some pins that are NOT PWM pins.

Paul, that was exactly the issue!! I moved the sensor over to pins 12 and 13 and it works great!

Now I move on the to if statement. I will be reading up on it this afternoon. Any advice as I move forward?

Any advice as I move forward?

You can't hurt anything by writing code. So, try something. Read the reference page.

Thanks for all your help Paul!

If TX wasn't so far from WA I would buy you lunch!