Arduino obstacle avoiding robot problem

Dear guys,

I have problem which i cant figure out.

I’m using Arduino Uno with Arduino motor shield L293D, 2 motors And 3 sonic sensors.

Here is my base project which is working fine :

#include <AFMotor.h> 
#define trigPin 12 
#define echoPin 13
AF_DCMotor motor1(1, MOTOR12_8KHZ);
AF_DCMotor motor2(2, MOTOR12_8KHZ);

void setup() {
  Serial.begin(9600); 
  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  motor1.setSpeed(200); 
  motor2.setSpeed (180);
}

void loop() {

  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1; 
  
  
  if (distance < 25) {

    
    motor1.run(BACKWARD);  
    motor2.run (BACKWARD);
  }
    
 
 
  else if (distance < 40
  ){
   
    motor1.run(BACKWARD); 
    motor2.run(FORWARD);
  
}
  else{
   
    delayMicroseconds   (15);
    motor1.run(FORWARD); 
    motor2.run(FORWARD);
    }
}

And here is my code which is not working. Actually if i try to test motor just with motor1.run(FORWARD);
motor2.run(FORWARD);

Only one of those spins. Everything is well connected, because with my “base” project both motors spins well.

I already tried everything i could but i had no luck.

#include <AFMotor.h> 
#define trigPin1 3
#define echoPin1 2
#define trigPin2 4
#define echoPin2 5
#define trigPin3 6
#define echoPin3 7

AF_DCMotor motor1(1, MOTOR12_8KHZ);
AF_DCMotor motor2(2, MOTOR12_8KHZ);
long duration1, duration2, duration3, distance1, distance2, distance3, RightSensor, FrontSensor, LeftSensor;
void setup()
{
  Serial.begin (9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
  motor1.setSpeed (200);
  motor2.setSpeed (200);
}


void loop() {


  SonarSensor(trigPin1, echoPin1);
  FrontSensor = distance1;
  SonarSensor(trigPin2, echoPin2);
  RightSensor = distance2;
  SonarSensor(trigPin3, echoPin3);
  LeftSensor = distance3;

  Serial.print(" Front ");
  Serial.print(FrontSensor);
  Serial.print(" Right ");
  Serial.print(RightSensor);
  Serial.print(" Left ");
  Serial.println(LeftSensor);


}
 void SonarSensor(int trigPin, int echoPin)
{
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH);
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  digitalWrite(trigPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin3, LOW);
  duration3 = pulseIn(echoPin3, HIGH);
  distance1 = (duration1 / 2) / 29.1;
  distance2 = (duration2 / 2) / 29.1;
  distance3 = (duration3 / 2) / 29.1;
 // if (distance < 25) {

    
 //   motor1.run(BACKWARD);  
 //   motor2.run (BACKWARD);
  }
    
 
 
//  else if (distance < 40
//  ){
   
//    motor1.run(BACKWARD); 
//    motor2.run(FORWARD);
  
//}
//  else{
   
//    delayMicroseconds   (15);
 //   motor1.run(FORWARD); 
 //   motor2.run(FORWARD);
 //   }
//}

(deleted)

edit

I understand now. Thanks a lot.