[SOLVED]HC-SR04 pauses during use.

Hi I built an obstacle avoiding car with 4 DC Gear Motors run by an Arduino Uno through an L293D Motor Shield. The car uses an HC-SR04 Sensor Module mounted on a Servo to detect objects in front of the car. The Uno is powered by a 9 Volt battery and the Motor Shield by 6 AA batteries. The car is supposed to back up when it senses an object in front of it, look left and right and then turn in the direction where the closest object is the farthest away. It does all that, but when it senses an object it stops and then about 5 seconds later continues as supposed to. This is really frustrating and I don’t know why it does this. Here is my code.

#include <AFMotor.h>

#include <Servo.h>

#include <NewPing.h>

#define TRIGGER_PIN 15

#define ECHO_PIN 14

#define MAX_DISTANCE 200

AF_DCMotor motor1(1);

AF_DCMotor motor2(2);

AF_DCMotor motor3(3);

AF_DCMotor motor4(4);

Servo servo;

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

int speed;

int distance;

int dist;

int distR;

int distL;

void setup() {
  
  Serial.begin(9600);
  
  speed = 255;
  
  servo.attach(9);
  
  servo.write(100);
  
  delay(500);
}

void loop() {
  distR = 0;
  
  distL = 0;
  
  distance = predictDist();
  
  if(distance <= 6 && distance > 0){
    runBackward(200);
    dontRun(0);

    distR = lookRight();
    delay(200);
    distL = lookLeft();
    delay(200);
    
  if(distR >= distL){
    turnRight(650);
    dontRun(1);
  } else {
      turnLeft(650);
      dontRun(1);
    }
    
  } else {
    runForward(1);
  }
  if(distance = 0){
    distance = 250;
  }
}

void runForward(int delayTime){
  motor1.setSpeed(speed);
  
  motor1.run(FORWARD);
  
  motor2.setSpeed(speed);
  
  motor2.run(FORWARD);
  
  motor3.setSpeed(speed);
  
  motor3.run(FORWARD);
  
  motor4.setSpeed(speed);
  
  motor4.run(FORWARD);
  
  delay(delayTime);
};

void runBackward(int delayTime){
  motor1.setSpeed(speed);
  
  motor1.run(BACKWARD);
  
  motor2.setSpeed(speed);
  
  motor2.run(BACKWARD);
  
  motor3.setSpeed(speed);
  
  motor3.run(BACKWARD);
  
  motor4.setSpeed(speed);
  
  motor4.run(BACKWARD);
  
  delay(delayTime);
};

void turnLeft(int delayTime){
  motor1.setSpeed(speed);
  
  motor1.run(FORWARD);
  
  motor2.setSpeed(speed);
  
  motor2.run(FORWARD);
  
  motor3.setSpeed(speed);
  
  motor3.run(BACKWARD);
  
  motor4.setSpeed(speed);
  
  motor4.run(BACKWARD);
  
  delay(delayTime);
};

void turnRight(int delayTime){
  motor1.setSpeed(speed);
  
  motor1.run(BACKWARD);
  
  motor2.setSpeed(speed);
  
  motor2.run(BACKWARD);
  
  motor3.setSpeed(speed);
  
  motor3.run(FORWARD);
  
  motor4.setSpeed(speed);
  
  motor4.run(FORWARD);
  
  delay(delayTime);
}

void dontRun(int delayTime){
  motor1.setSpeed(speed);
  
  motor1.run(RELEASE);
  
  motor2.setSpeed(speed);
  
  motor2.run(RELEASE);
  
  motor3.setSpeed(speed);
  
  motor3.run(RELEASE);
  
  motor4.setSpeed(speed);
  
  motor4.run(RELEASE);
  
  delay(delayTime);
};   

int lookRight(){
  servo.write(10);
  delay(500);
  distance = predictDist();
  delay(100);
  servo.write(100);
  return distance;
  delay(100);
}

int lookLeft(){
  servo.write(170);
  delay(500);
  distance = predictDist();
  delay(100);
  servo.write(100);
  return distance;
  delay(100);
}

int predictDist(){
  
  unsigned int uS = sonar.ping();
  
  pinMode(ECHO_PIN,OUTPUT);
  
  digitalWrite(ECHO_PIN,LOW);
  
  pinMode(ECHO_PIN,INPUT);
  
  dist = uS / US_ROUNDTRIP_IN;
  
  return dist;
  
}

Thank you in advance.

I see LOTS of delay() calls in that code. That's probably leading to the problems you see.

But there is no delays between the sensing of an object and backing up, looking around etc.

This line sets distance to zero. Use "==" in comparisons.

if (distance = 0)

That's true, but why is there a delay? Fixing that If statement didn't help it.

This probably isn't the source of your problem, but why are you messing with pinMode in your predictDist code and briefly shorting the echo pin to ground? The NewPing library takes care of pinModes for you.

Also, for better reliability, suggest using NewPing's median method (to help ignore the occasional zero or glitchy reading).

int predictDist(){
  
  unsigned int uS = sonar.ping();
  
  pinMode(ECHO_PIN,OUTPUT);
  digitalWrite(ECHO_PIN,LOW);
  pinMode(ECHO_PIN,INPUT);
  
  dist = uS / US_ROUNDTRIP_IN;
  
  return dist;
  
}

I don't really get the use of those pinModes but I took that part of the code from this forum.

It is best not to use code that you don't understand. You won't learn anything.

And, the posted bit in question doesn't do anything useful.

jremington:
It is best not to use code that you don't understand. You won't learn anything.

I understand most of it.

jremington:
And, the posted bit doesn't do anything useful.

The posted bit of what?

The way I coded it may be a bit weird, but it works fine. I just need to know why theirs a delay.

I just added a short delay before the runBackward call and it's working perfectly now. I guess it couldn't switch from forward to backward so quickly.