1

I want to attach two ultrasonic sensors to an obstacle to avoiding the car.

  1. One on the front of the car
  2. Second on the servo motor on the car

It is an obstacle avoidance Project. I am getting the problem with the code.

   **First, the code of the front sensor gets execute while the second is off. And the second sensor gets on while the first one is off, And again the second gets off and first gets on. And the loop continues. I want to run both the sensors parallel to each other i.e both the sensors should work along.** 

Here is my code:

#include <Servo.h> //Servo motor library
#include <NewPing.h> //Ultrasonic sensor function library

const int LeftForward = 7;// L298N control pins
const int LeftBackward = 6;
const int RightForward = 9;
const int RightBackward = 8;
int ENA = 10;
int ENB = 5;
#define trig_pin1 A1 //sensor pins - analog input 1
#define echo_pin1 A2 //analog input 2
#define trig_pin2 A3 //sensor pins - analog input 1
#define echo_pin2 A4
#define maximum_distance 200

boolean goesForward = false;
int distance = 100;
long duration;
NewPing sonar1(trig_pin1, echo_pin1,  maximum_distance); //sensor function
Servo servo_motor; //servo name

void setup(){
  pinMode(RightForward, OUTPUT);
  pinMode(LeftForward, OUTPUT);
  pinMode(LeftBackward, OUTPUT);
  pinMode(RightBackward, OUTPUT);
  pinMode(trig_pin2, OUTPUT); 
  pinMode(echo_pin2, INPUT);

  analogWrite(ENA, 255);    //set speed to maximum
  analogWrite(ENB, 255);
  servo_motor.attach(11); //servo pin
  servo_motor.write(115);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop(){
  digitalWrite(trig_pin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trig_pin2, HIGH);
  delayMicroseconds(2);
  duration = pulseIn(echo_pin2, HIGH);
  distance = duration/58.2;
  delay(10);
  if(distance >19)
         {digitalWrite(RightForward, HIGH);
          digitalWrite(RightBackward, LOW);
          digitalWrite(LeftForward, HIGH);                                
          digitalWrite(LeftBackward, LOW);                                                       
          }
  if (distance < 18)
  {
    digitalWrite(RightForward, LOW);  //Stop                
    digitalWrite(RightBackward, LOW);
    digitalWrite(LeftForward, LOW);                                
    digitalWrite(LeftBackward, LOW);
    delay(500);
    digitalWrite(RightForward, LOW);      //movebackword         
    digitalWrite(RightBackward, HIGH);
    digitalWrite(LeftForward, LOW);                                
    digitalWrite(LeftBackward, HIGH);
    delay(500);
    digitalWrite(RightForward, LOW);  //Stop                
    digitalWrite(RightBackward, LOW);
    digitalWrite(LeftForward, LOW);                                
    digitalWrite(LeftBackward, LOW);  
    delay(100);  
    digitalWrite(RightForward, HIGH);       
    digitalWrite(RightBackward, LOW);   
    digitalWrite(LeftForward, LOW);                                 
    digitalWrite(LeftBackward, LOW);  
    delay(500);
  }





  int distanceRight = 0;
  int distanceLeft = 0;

  delay(50);
  if (distance <= 40){
    moveStop(); // obstacle probably on the route forward, so stop
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);
    if (distance >= distanceLeft){
      turnRight(); // calculate in which direction the obstacle is more far
      moveStop();
    }
    else{
      turnLeft();
      moveStop();
    }
  }
  else{
    moveForward(); 
  }
    distance = readPing();
}

int lookRight(){  
  servo_motor.write(50);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
}

int lookLeft(){
  servo_motor.write(170);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
  delay(100);
}

int readPing(){
delay(70);
int cm = sonar1.ping_cm();
if (cm==0){
    cm=250;
  }
  return cm;
}

void moveStop(){
  digitalWrite(RightForward, LOW);
  digitalWrite(LeftForward, LOW);
  digitalWrite(RightBackward, LOW);
  digitalWrite(LeftBackward, LOW);
}

void moveForward(){
  if(!goesForward){
    goesForward=true;
    digitalWrite(LeftForward, HIGH);
    digitalWrite(RightForward, HIGH);
    digitalWrite(LeftBackward, LOW);
    digitalWrite(RightBackward, LOW); 
  }
}

void moveBackward(){
  goesForward=false;
  digitalWrite(LeftBackward, HIGH);
  digitalWrite(RightBackward, HIGH);
  digitalWrite(LeftForward, LOW);
  digitalWrite(RightForward, LOW);
}

void turnRight(){
  digitalWrite(LeftForward, HIGH);
  digitalWrite(RightBackward, HIGH);
  digitalWrite(LeftBackward, LOW);
  digitalWrite(RightForward, LOW);
  delay(500);
  digitalWrite(LeftForward, HIGH);
  digitalWrite(RightForward, HIGH);
  digitalWrite(LeftBackward, LOW);
  digitalWrite(RightBackward, LOW);
}

void turnLeft(){
  digitalWrite(LeftBackward, HIGH);
  digitalWrite(RightForward, HIGH);
  digitalWrite(LeftForward, LOW);
  digitalWrite(RightBackward, LOW);
  delay(500);
  digitalWrite(LeftForward, HIGH);
  digitalWrite(RightForward, HIGH);
  digitalWrite(LeftBackward, LOW);
  digitalWrite(RightBackward, LOW);
}

what wrong am I doing that is not making both sensors work along.

2
  • remove all motor code ... leave only the ultrasonic sensor code ... print distance to serial monitor ... when you get that working, add the motor code in small amounts
    – jsotola
    Nov 2 '20 at 15:09
  • 1
    @okay, let me try Nov 2 '20 at 15:23
0

You need to change this part of the code

digitalWrite(trig_pin2, LOW);

delayMicroseconds(2);

digitalWrite(trig_pin2, HIGH);

delayMicroseconds(2);

to this

digitalWrite(trig_pin2, LOW);

delayMicroseconds(2);

digitalWrite(trig_pin2, HIGH);

delayMicroseconds(10);

because you will need to wait for 10 Microseconds to get the output. After making the above changes your code should work fine you can also have a look at this site they have interfaced multiple ultrasonic sensors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.