Linetracker and obstacle detection in the same code not working - arduino uno

Hi everybody,
I would like to receive some help for my code for my arduino uno project, because it isn't working properly. Which involves 3 linetracker sensors, one ultrasonic sensor (HC-SRO4), two DC motors and a L298N Motor driver board. It is supposed to follow a black line and stop when an object is detected. The linetracking code by itself runs properly, however, when I add the obstacle detection code to it, the arduino fails to follow the black line, but the robot does stop when it detects an object. I hope someone can help me.

 #include <Servo.h> //servo library

Servo myservo; // create servo object to control servo

#define LT1 digitalRead(10)// linetracker 1
#define LT2 digitalRead(4)// linetracker 2
#define LT3 digitalRead(2)// linetracker 3


int M1 = 6; // right motor forward
int M2 = 7;// right motor back
int M3 = 8;// left motor back
int M4 = 9;// left motor forward
int ENA = 5; // motor left
int ENB = 11;// motor right
int ABS = 115;// speed
int middle_distance = 0; 
int stop_distance = 12;
int Echo = A4; // ultrasonic sensor
int Trig = A5; // ultrasonic sensor

void forward(){
  analogWrite(ENA, ABS);
  analogWrite(ENB, ABS);
  digitalWrite(M1, HIGH);
  digitalWrite(M2, LOW);
  digitalWrite(M3, LOW);
  digitalWrite(M4, HIGH);

}

void back(){
  analogWrite(ENA, ABS);
  analogWrite(ENB, ABS);
  digitalWrite(M1, LOW);
  digitalWrite(M2, HIGH);
  digitalWrite(M3, HIGH);
  digitalWrite(M4, LOW);

}

void left(){
  analogWrite(ENA, ABS);
  analogWrite(ENB, ABS);
  digitalWrite(M1, HIGH);
  digitalWrite(M2, LOW);
  digitalWrite(M3, LOW);
  digitalWrite(M4, LOW); 
  
}

void extremeleft(){
  analogWrite(ENA, ABS);
  analogWrite(ENB, ABS);
  digitalWrite(M1, HIGH);
  digitalWrite(M2, LOW);
  digitalWrite(M3, HIGH);
  digitalWrite(M4, LOW); 
  
}


void right(){
  analogWrite(ENA, ABS);
  analogWrite(ENB, ABS);
  digitalWrite(M1, LOW);
  digitalWrite(M2, LOW);
  digitalWrite(M3, LOW);
  digitalWrite(M4, HIGH);
} 
void extremeright(){
  analogWrite(ENA, ABS);
  analogWrite(ENB, ABS);
  digitalWrite(M1, LOW);
  digitalWrite(M2, HIGH);
  digitalWrite(M3, LOW);
  digitalWrite(M4, HIGH);
} 

void stop(){
   digitalWrite(ENA, LOW);
   digitalWrite(ENB, LOW);
  
} 

int Distance_test()   
{
  digitalWrite(Trig, LOW);   
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);  
  delayMicroseconds(20);
  digitalWrite(Trig, LOW);   
  float Fdistance = pulseIn(Echo, HIGH);  
  Fdistance= Fdistance/58.2;       
  return (int)Fdistance;   
}







void setup(){
  myservo.attach(3);
  Serial.begin(9600);
  pinMode(Echo, INPUT);    
  pinMode(Trig, OUTPUT);  
  pinMode(M1,OUTPUT);
  pinMode(M2,OUTPUT);
  pinMode(M3,OUTPUT);
  pinMode(M4,OUTPUT);
  pinMode(ENA,OUTPUT);
  pinMode(ENB,OUTPUT);
  
} 

void loop() {

  
  myservo.write(90);
  delay(500);
  middle_distance = Distance_test();

  
if (middle_distance<=stop_distance)
{
  stop();
  delay(500);
  
}
else if(LT1 == 0 && LT2 == 1 && LT3 == 0){
    forward();
  }
  else if(LT1 == 1 && LT2 == 0 && LT3 == 0) { 
    extremeleft();
    while(LT1);                             
  }   
  else if (LT1 == 1 && LT2 == 1 && LT3 == 0){
    left();
    while (LT1 && LT2);
  }
  else if(LT3) {
    extremeright();
    while(LT3);  
  }
  else if (LT1 == 0 && LT2 == 1 && LT3 == 1){
    right();
    while(LT2 && LT3);
  }
}

olivierstad:
#define LT1 digitalRead(10)// linetracker 1
#define LT2 digitalRead(4)// linetracker 2
#define LT3 digitalRead(2)// linetracker 3

You need to set pinMode for these pins too (with pullup).

You need to set pinMode for these pins too (with pullup).

Pins default to being inputs so pinMode() is not strictly required, but pullup resistors (either internal or external) or pulldown resistors (external) are usually a good idea.

However, using pinMode(), even if not required, makes it obvious what the intended use of the pin is as does a meaningful name for the pin.

What happens if you get rid of the delay() on line 122?

I suspect the delay() is to prevent unnecessary obstacle checking but it is also affecting the line checking.

Use millis() to limit the obstacle checking without limiting the line checking - something like this

if (millis() - prevObstacleCheck >= 500) {
    prevObstacleCheck += 500;
    middleDistance = distanceCheck();
}

...R

thank you for your answer, however it gives an error that states 'prevDistance_test' was not declared in this scope. Do you know what i could do this fix this.
btw i called it prevDistance_test because that is what i called it Distance_test before.

olivierstad:
thank you for your answer, however it gives an error that states 'prevDistance_test' was not declared in this scope. Do you know what i could do this fix this.
btw i called it prevDistance_test because that is what i called it Distance_test before.

Please post the program that gave that error and also post the actual error message.

In the code in your Original Post there is no variable called prevDistance_test

...R