Can't understand what im doin wrong

I'm building a bot that has limited path-finding capabilities but when the distance is greater than 6cm it still wants to turn.

// Motor Driver
 // ENB
 int enB = 3;
 int in3 = 4;
 int in4 = 5;

 //ENA
 int enA = 11;
 int in1 = 10;
 int in2 = 9;

// Ultrasonic Rangefinder
 int ePin = 6;
 int tPin =7;

 float distance;

//pathfinding
 int safe = 0;
 int turn = 0;
void setup() {
 // put your setup code here, to run once:

 //Motor Driver Pins
   //ENB
 pinMode(in1, OUTPUT);
 pinMode(in2, OUTPUT);
 pinMode(enB, OUTPUT);

   //ENA
 pinMode(in3, OUTPUT);
 pinMode(in4, OUTPUT);
 pinMode(enA, OUTPUT);

   //Ultrasonic Rangefinder
 Serial.begin(9600);
 pinMode(ePin, INPUT);
 pinMode(tPin, OUTPUT);
}

void ping() {
 float duration;
 digitalWrite(tPin, LOW); 
 delayMicroseconds(2);

 digitalWrite(tPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(tPin, LOW);
 
 duration = pulseIn(ePin, HIGH);
 distance = (duration / 2) * 0.0344;
 
 if (distance >= 400 || distance <= 2){
   Serial.print("Distance = ");
   Serial.println("Out of range");
 }
 else {
   Serial.print("Distance = ");
   Serial.print(distance);
   Serial.println(" cm");
 }
 delay(500);
}

void pathfind() {
 if (distance >= 6 || distance <= 2) {
   analogWrite(enA, 250);
   analogWrite(enB, 250);
   digitalWrite(in1, LOW);
   digitalWrite(in2, HIGH);
   digitalWrite(in3, HIGH);
   digitalWrite(in4, LOW);
 } else {
   digitalWrite(in1, HIGH);
   digitalWrite(in2, LOW);
   digitalWrite(in3, HIGH);
   digitalWrite(in4, LOW);
 }
}

void loop() {
 // put your main code here, to run repeatedly:
 ping();
 pathfind();

}

What do you see when you print the value of distance before you test it ?

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

...R

UKHeliBob:
What do you see when you print the value of distance before you test it ?

Serial monitor reads 50cm and it still wants to turn.

Which piece of your program makes your 'bot turn?

...R

Why do you piss away half a second every time you call ping()?

The names in1, in2, in3, and in4 might make sense from the point of view of the motor controller. They don't mean squat from the point of view of the Arduino. Use names that make sense from the Arduino's point of view, so that WE can tell that you are setting the pins appropriately.

check your logic
if (distance >= 6 || distance <= 2) {

if greater than 6 execute 1st part of if , so 50 should execute.
else portion only if distance 2 - 6

not sure which way you wired your motors