Line following and Obstacle Avoiding robot in a continuous loop

Hi there, I am currently making a line following and obstacle avoiding robot, here is the code that I am using:

/*------ Arduino Line Follower and Obstacle Avoider Code----- */
/*-------Ultrasonic Sensor Stuff---------*/
#define trigPin 11
#define echoPin 12
/*-------definning Inputs------*/
#define LS 9      // left sensor
#define RS 10     // right sensor
int vspeed = 100;
int turndelay = 1000;
long duration;
int distance;
int safetyDistance;
/*-------definning Outputs------*/
#define LM1 3       // left motor
#define LM2 4       // left motor
#define RM1 8       // right motor
#define RM2 7       // right motor
#define MtrspeedA 5 
#define MtrspeedB 6

void setup()
{
  pinMode(LS, INPUT);
  pinMode(RS, INPUT);
  pinMode(LM1, OUTPUT);
  pinMode(LM2, OUTPUT);
  pinMode(RM1, OUTPUT);
  pinMode(RM2, OUTPUT);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input

}

void loop()
{
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

  // Calculating the distance
  distance= duration*0.034/2;
  safetyDistance = distance;
  if (safetyDistance <= 13.5){

    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, LOW);
    digitalWrite(RM2, HIGH);
    analogWrite(MtrspeedA, vspeed);
    analogWrite(MtrspeedB, vspeed);
    delay(turndelay);
    digitalWrite(LM1, LOW);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, LOW);
    digitalWrite(RM2, HIGH);
    analogWrite(MtrspeedA, vspeed);
    analogWrite(MtrspeedB, vspeed);
    delay(turndelay);
    digitalWrite(LM1, LOW);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, HIGH);
    analogWrite(MtrspeedA, vspeed);
    analogWrite(MtrspeedB, vspeed);
    delay(turndelay);
    digitalWrite(LM1, LOW);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, LOW);
    digitalWrite(RM2, HIGH);
    analogWrite(MtrspeedA, vspeed);
    analogWrite(MtrspeedB, vspeed);
    delay(turndelay);
    digitalWrite(LM1, LOW);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, HIGH);
    analogWrite(MtrspeedA, vspeed);
    analogWrite(MtrspeedB, vspeed);
    delay(turndelay);
    digitalWrite(LM1, LOW);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, LOW);
    digitalWrite(RM2, HIGH);
    analogWrite(MtrspeedA, vspeed);
    analogWrite(MtrspeedB, vspeed);
    delay(turndelay);
     
  }
  else{

    digitalWrite(LM1, LOW);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, LOW);
    digitalWrite(RM2, HIGH);
    analogWrite(MtrspeedA, vspeed);
    analogWrite(MtrspeedB, vspeed);
      
  }

  
  if(!(digitalRead(LS)) && !(digitalRead(RS)))     // Move Forward
  {
    digitalWrite(LM1, LOW);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, LOW);
    digitalWrite(RM2, HIGH);
    analogWrite(MtrspeedA, vspeed);
    analogWrite(MtrspeedB, vspeed);
  }
  
  if(digitalRead(LS) && !(digitalRead(RS)))     // Turn right
  {
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, LOW);
    digitalWrite(RM2, HIGH);
    analogWrite(MtrspeedA, vspeed);
    analogWrite(MtrspeedB, vspeed);
  }
  
  if(!(digitalRead(LS)) && digitalRead(RS))     // turn left
  {
    digitalWrite(LM1, LOW);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, HIGH);
    analogWrite(MtrspeedA, vspeed);
    analogWrite(MtrspeedB, vspeed);
  }
  
  if(digitalRead(LS) && digitalRead(RS))     // stop
  {
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, HIGH);
    analogWrite(MtrspeedA, 0);
    analogWrite(MtrspeedB, 0);
  }
}

The problem that I am facing is that the robot is no longer following the line but instead all it is doing is moving, right, then forward, then left, then forward, and then reaching the line, but instead of following the line, it repeats the whole process again, unable to follow any line.

Please Help, thank you

Do you have a copy that does follow a line? Without any obstacle avoiding logic.

I see you are using PulseIn for obstacle detection. I wonder what that does to the line following logic.

I prefer to read sensors once at the top of the loop and then act on them. You are repeatedly re-reading them throughout the loop. I would worry about how that might break the line following logic if the sensor returns a different value part way through the loop.

yes I do have one without any obstacle avoidance logic:

/*------ Arduino Line Follower Code----- */
/*-------definning Inputs------*/
#define LS 9      // left sensor
#define RS 10     // right sensor
int vspeed = 100;
/*-------definning Outputs------*/
#define LM1 3       // left motor
#define LM2 4       // left motor
#define RM1 8       // right motor
#define RM2 7       // right motor
#define MtrspeedA 5 
#define MtrspeedB 6

void setup()
{
  pinMode(LS, INPUT);
  pinMode(RS, INPUT);
  pinMode(LM1, OUTPUT);
  pinMode(LM2, OUTPUT);
  pinMode(RM1, OUTPUT);
  pinMode(RM2, OUTPUT);


}

void loop()
{
  if(!(digitalRead(LS)) && !(digitalRead(RS)))     // Move Forward
  {
    digitalWrite(LM1, LOW);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, LOW);
    digitalWrite(RM2, HIGH);
    analogWrite(MtrspeedA, vspeed);
    analogWrite(MtrspeedB, vspeed);
  }
  
  if(digitalRead(LS) && !(digitalRead(RS)))     // Turn right
  {
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, LOW);
    digitalWrite(RM2, HIGH);
    analogWrite(MtrspeedA, vspeed);
    analogWrite(MtrspeedB, vspeed);
  }
  
  if(!(digitalRead(LS)) && digitalRead(RS))     // turn left
  {
    digitalWrite(LM1, LOW);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, HIGH);
    analogWrite(MtrspeedA, vspeed);
    analogWrite(MtrspeedB, vspeed);
  }
  
  if(digitalRead(LS) && digitalRead(RS))     // stop
  {
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, HIGH);
    analogWrite(MtrspeedA, 0);
    analogWrite(MtrspeedB, 0);
  }
}

Instead of messing about with distance and safetyDistance why don't you just use duration directly? Just change the value you're comparing against.

Steve

Slipstick I do not know what you mean by saying

int safetyDistance;

if (safetyDistance <= 13.5){

safetyDistance is an integer. It’s a waste of time comparing it to a float. It looks like you are using centimeters as your distance unit. Is 0.5cm important to you? Then use mm or use a float to store the distance values.

What is the robot supposed to do? You have given us the code and the description of what it actually does (thankyou, that is rare for first-timers here) but what do you want it to do? If it’s on the line and ‘sees’ an obstacle, should it stop? Should it proceed slowly and hope that the line turns before the wall?

If it’s not on the line, what is it supposed to do? Is it searching for the line while avoiding obstacles?