Help with line follower and obstacle avoiding robot

Hello, i am trying to make a line follower 4x4 robot using Arduino Uno, L298N motor driver and two IR modules, and it works fine. then i wanted to add an ultrasonic sensor for obstacle avoidance. And here comes my problem, the ultrasonic sensor works well when tested alone, however when i add the ultrasonic code to the line follower code, the robot does not follow the line anymore and does not avoid obstacles either.
Would you please help me. Thank you.
Here's the code.

#include <NewPing.h>

    int motorA_E1 = 9;  
    int motorA_E2 = 10; 
    int motorA_speed = 5;
    int motorB_E3 = 11; 
    int motorB_E4 = 12; 
    int motorB_speed= 6;
    int left_sensor_pin = 2;
    int right_sensor_pin = 3;
    int trigPin = A0;
    int echoPin = A1;
    int max_distance =100;
    
    NewPing sonar(trigPin, echoPin, max_distance);
    
void setup() {
  
  pinMode(motorA_E1, OUTPUT); 
  pinMode(motorA_E2, OUTPUT);
  pinMode(motorB_E3, OUTPUT);
  pinMode(motorB_E4, OUTPUT);
  pinMode(motorA_speed, OUTPUT);
  pinMode(motorB_speed, OUTPUT);
  
  pinMode(left_sensor_pin, INPUT);
  pinMode(right_sensor_pin, INPUT);

  Serial.begin(9600);
}

void loop() {
    
   boolean left_sensor_state=digitalRead(left_sensor_pin);
   boolean right_sensor_state=digitalRead(right_sensor_pin);
   
   delay(70);
   int distance =sonar.ping_cm();
   Serial.print("distance= ");
   Serial.println(distance);
   if (distance == 0)
   {
   distance = 30;
   Serial.print("distance= ");
   Serial.println(distance);
   }
   if(distance<10)
   {
    off();
     
    Serial.println("stop,, obstacle");
   }
   
  if (left_sensor_state==0 and right_sensor_state==0)
     {
    analogWrite (motorA_speed,100);
    analogWrite (motorB_speed,100);
    forward();
     Serial.println("forward"); 
     }
   else if (left_sensor_state==1 and right_sensor_state==0)
     {
     analogWrite (motorA_speed,150);
     analogWrite (motorB_speed,150);
     turnLeft();
     Serial.println("turn left"); 
     }

      else if (left_sensor_state==0 and right_sensor_state==1)
    {
    analogWrite (motorA_speed,150);
    analogWrite (motorB_speed,150);  
     turnRight();
    Serial.println("turn right");
     }
   else if (left_sensor_state==1 and right_sensor_state==1 )
     { 
     analogWrite (motorA_speed,0);
     analogWrite (motorB_speed,0); 
     off();
     Serial.println("stop bc of double straps");
     
}
 
}
void forward(){
    digitalWrite(motorA_E1,HIGH);  
    digitalWrite (motorA_E2,LOW);
    digitalWrite(motorB_E3,LOW);               
    digitalWrite (motorB_E4,HIGH);
   
     
}
void off() {
   digitalWrite(motorA_E1,LOW);  
    digitalWrite (motorA_E2,LOW);
    digitalWrite(motorB_E3,LOW);               
    digitalWrite (motorB_E4,LOW);
   
     
}
void turnRight(){
  digitalWrite(motorA_E1,HIGH);  
    digitalWrite (motorA_E2,LOW);
    digitalWrite(motorB_E3,HIGH);               
    digitalWrite (motorB_E4,LOW);
   
   
}
void turnLeft() {
   digitalWrite(motorA_E1,LOW);  
    digitalWrite (motorA_E2,HIGH);
    digitalWrite(motorB_E3,LOW);               
    digitalWrite (motorB_E4,HIGH);
     
}

And which ultrasonic sensor are you using and did you test it separately before adding it to your code?
Paul

i am using the HC-SR04? yes i tested it multiples times.

i changed the code to this, and now the robot stops when it faces an obstacle,
but it still does not follow the line.

#include <NewPing.h>

    int motorA_E1 = 9;  
    int motorA_E2 = 10; 
    int motorA_speed = 5;
    int motorB_E3 = 11; 
    int motorB_E4 = 12; 
    int motorB_speed= 6;
    int left_sensor_pin = 2;
    int right_sensor_pin = 3;
    int trigPin = A0;
    int echoPin = A1;
    int max_distance =100;
    
    NewPing sonar(trigPin, echoPin, max_distance);
    
void setup() {
  
  pinMode(motorA_E1, OUTPUT); 
  pinMode(motorA_E2, OUTPUT);
  pinMode(motorB_E3, OUTPUT);
  pinMode(motorB_E4, OUTPUT);
  pinMode(motorA_speed, OUTPUT);
  pinMode(motorB_speed, OUTPUT);
  
  pinMode(left_sensor_pin, INPUT);
  pinMode(right_sensor_pin, INPUT);

  Serial.begin(9600);
}

void loop() {
    
   boolean left_sensor_state=digitalRead(left_sensor_pin);
   boolean right_sensor_state=digitalRead(right_sensor_pin);
   
   delay(70);
   int distance =sonar.ping_cm();
   Serial.print("distance= ");
   Serial.println(distance);
   if (distance == 0)
   {
   distance = 30;
   Serial.print("distance= ");
   Serial.println(distance);
   }
    
   
  if (left_sensor_state==0 and right_sensor_state==0 and distance>10)
     {
    analogWrite (motorA_speed,100);
    analogWrite (motorB_speed,100);
    forward();
     Serial.println("forward"); 
     }
   else if (left_sensor_state==1 and right_sensor_state==0 and distance >10)
     {
     analogWrite (motorA_speed,150);
     analogWrite (motorB_speed,150);
     turnLeft();
     Serial.println("turn left"); 
     }

      else if (left_sensor_state==0 and right_sensor_state==1 and distance>10)
    {
    analogWrite (motorA_speed,150);
    analogWrite (motorB_speed,150);  
     turnRight();
    Serial.println("turn right");
     }
   else if (left_sensor_state==1 and right_sensor_state==1 and distance>10 )
     { 
     analogWrite (motorA_speed,0);
     analogWrite (motorB_speed,0); 
     off();
     Serial.println("stop bc of double straps");
     }
    else{ 
      off();
       
    }
 
}
void forward(){
    digitalWrite(motorA_E1,HIGH);  
    digitalWrite (motorA_E2,LOW);
    digitalWrite(motorB_E3,LOW);               
    digitalWrite (motorB_E4,HIGH);
   
     
}
void off() {
   digitalWrite(motorA_E1,LOW);  
    digitalWrite (motorA_E2,LOW);
    digitalWrite(motorB_E3,LOW);               
    digitalWrite (motorB_E4,LOW);
   
     
}
void turnRight(){
  digitalWrite(motorA_E1,HIGH);  
    digitalWrite (motorA_E2,LOW);
    digitalWrite(motorB_E3,HIGH);               
    digitalWrite (motorB_E4,LOW);
   
   
}
void turnLeft() {
   digitalWrite(motorA_E1,LOW);  
    digitalWrite (motorA_E2,HIGH);
    digitalWrite(motorB_E3,LOW);               
    digitalWrite (motorB_E4,HIGH);
     
}

Then you already know the sensor has a built-in delay, up to a second, while waiting for the echo or the 1 second time-out. Have you shortened the time-out?
Paul

i specifically used the NewPing library because it doesn't lag for a full second while waiting for the echo. but other than this i don't know to to shorten the delay.

Ok, how often do you ask the NewPing library for pinging?
Paul

After reading your reply, i did shorten the pinging time from 70ms to 30ms, it worked and followed the line in a way that was not so good, but after several essays without changing anything, it stopped following the line again and instead went always forward.

Thank you again.

Hello leeyoun

To make a perfect line follower with 3 IR modules.That will really help to control the robot .I advice you to see this article :
https://gootrio.com/realiser-un-robot-suiveur-de-ligne-avec-une-carte-arduino/

Hello friend,to create your line follower robot yo should have 3 IR module and 4 DC motor,driver L298N and 4 wheels and for the program I suggest this article for you
https://gootrio.com/realiser-un-robot-suiveur-de-ligne-avec-une-carte-arduino-uno/

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.