4wd car with 3 sensors

I am trying to control the 4wd car with 3 ping sensors, I was able to integrate and the three sensors and read the information out of them, but when I put the commands to move the car, the first sensor always read 0 while the others read the real distance. Can you tell me why this is happening?
the code is below

#include <AFMotor.h> 
AF_DCMotor motor1(1, MOTOR12_64KHZ); 
AF_DCMotor motor2(2, MOTOR12_64KHZ); 
AF_DCMotor motor3(3, MOTOR12_1KHZ); 
AF_DCMotor motor4(4, MOTOR12_1KHZ); 
int trigPin []={8,10,12};
int echoPin []={9,11,13};
int i, j;
char *names[]={"Right ", "Front ", "Left "};

void setup() {
  Serial.begin (9600);
  motor1.setSpeed(200);         // Set the speed for the motors (255 is the maximum)      
  motor2.setSpeed(200); 
  motor3.setSpeed(200); 
  motor4.setSpeed(200);
}
void forward(){      // This function moves the wheels forward 
    motor1.run(FORWARD); 
  motor2.run(FORWARD); 
  motor3.run(FORWARD); 
  motor4.run(FORWARD);
  
} 
void backward() {      // This function moves the wheels backward 
    motor1.run(BACKWARD); 
  motor2.run(BACKWARD); 
  motor3.run(BACKWARD); 
  motor4.run(BACKWARD); 
} 
void haltMotors()   // This function stops each motor (It is better to stop the motors before changing direction.) 
{ 
  motor1.run(RELEASE); 
  motor2.run(RELEASE); 
  motor3.run(RELEASE); 
  motor4.run(RELEASE); 
} 
void turnRight(){    // This function turns the robot right.   
  motor1.run(FORWARD); 
  motor2.run(BACKWARD); 
  motor3.run(BACKWARD); 
  motor4.run(FORWARD); 
} 
void turnLeft(){
  motor1.run(BACKWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(BACKWARD);
}

unsigned long ping(int j) {
  int duration;
  pinMode (trigPin[j], OUTPUT);
  pinMode (echoPin[j], INPUT);
  digitalWrite(trigPin[j], LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin[j], HIGH);
  duration = pulseIn(echoPin[j], HIGH);
  return (duration/2) / 29.1;
}

int smallest_value (unsigned long array[3]){ // function to find the smallest value int the array
  int min_value, n;
  
  min_value=array[0];
  for (n=1; n<3; n++){
    if(array[n]<min_value){
      min_value=array[n];
      
    }
  }
  return min_value;
}
int pos (unsigned long array[3]){ // function to find position of the smallest value in the array
  int min_value, poS, n;
  poS=0;
  min_value=array[0];
  for (n=1; n<3; n++){
    if(array[n]<min_value){
      min_value=array[n];
      poS=n;
    }
  }
  return poS;
}

void loop(){
  int l, minimum, Position;
  unsigned long distance[3];
  for (i=0, l=0; i<3, l<3; i++, l++){
  distance[l]=ping(i);
  Serial.print (names[i]);
  Serial.print(distance[l]);
  Serial.print("cm");
  delay(1000);
  Serial.println();
  }
  minimum=smallest_value(distance);
  Position=pos(distance);
  Serial.print (minimum);
  Serial.println();
  Serial.print (Position);
  Serial.println();
  delay (1000);
  if (minimum>10){
    forward();}
    else if (minimum<10 && Position==0){ //if the smallest value is smaller than 10 and the position is 0 (right) 
      turnLeft();}
      else if (minimum<10 && Position==2){
        turnRight();}
        else if(minimum <10 && Position==1){
          haltMotors();}
}

Thank you

Some debug prints of the values returned by the ranging calls would be useful

there you go Right 0cm Front 36cm Left 67cm 0 0

the 0 0 at the end is the smallest value and the positon

I don't really get the point of the two indices in the for loop; they've got the same value.

duration = pulseIn(echoPin[j], HIGH);

pulseIn doesn't return an int. That isn't your problem, but clobber the easy ones first.

those two values are the smallest value on the array distance and the position of this smallest value on the same array I use those values to move the robot, if the smallest value is on the position 0 of the array the robot will turn left

I am using the ping sensor that has 4 pins one for trigger and one for echo

I solved part of the problem by putting a if statement before the movement part

 if (minimum!=0){
  if (minimum>10){
    forward();}
    else if (minimum<10 && Position==0){ //if the smallest value is smaller than 10 and the position is 0 (right) 
      turnLeft();}
      else if (minimum<10 && Position==2){
        turnRight();}
        else if(minimum <10 && Position==1){
          haltMotors();}
  }
   
}

but it gave me this type of result
Right 0cm
Front 34cm
Left 68cm
0
0
Right 70cm
Front 35cm
Left 67cm
35
1
Right 0cm
Front 35cm
Left 68cm
0
0
Right 70cm
Front 34cm
Left 53cm
34
1

int smallest_value (unsigned long array[3]){ // function to find the smallest value int the array
  int min_value, n;
  
  min_value=array[0];
  for (n=1; n<3; n++){
    if(array[n]<min_value){
      min_value=array[n];
      
    }
  }
  return min_value;
}

Why does a function that finds the smallest value in an array of unsigned longs store that value in an int, and why does it return an int?

int pos (unsigned long array[3]){ // function to find position of the smallest value in the array
  int min_value, poS, n;
  poS=0;
  min_value=array[0];
  for (n=1; n<3; n++){
    if(array[n]<min_value){
      min_value=array[n];
      poS=n;
    }
  }
  return poS;
}

Another function that stores unsigned longs in ints, and expects that to work. It won’t.

Would this be a simpler way of finding the smallest of three?

// function to find the smallest value int the array
unsigned long smallest_value (unsigned long array[3])
  { 
  return min (array [0], min (array [1], array [2]));
  }

int pos (unsigned long array[3]){ // function to find position of the smallest value in the array
  int min_value, poS, n;
  poS=0;

I'm not keen on variable/function names that differ only in case (pos and poS).

I made the modifications you suggested, but it is still doing the same thing. Anyone know why the sensor alternates the reading between 0 and the real value? That's the real problem with the program.

I made the modifications you suggested

Code?

#include <AFMotor.h> 
AF_DCMotor motor1(1, MOTOR12_64KHZ); 
AF_DCMotor motor2(2, MOTOR12_64KHZ); 
AF_DCMotor motor3(3, MOTOR12_1KHZ); 
AF_DCMotor motor4(4, MOTOR12_1KHZ); 
int trigPin []={8,10,12};
int echoPin []={9,11,13};
int i, j;
char *names[]={"Right ", "Front ", "Left "};

void setup() {
  Serial.begin (9600);
  motor1.setSpeed(200);         // Set the speed for the motors (255 is the maximum)      
  motor2.setSpeed(200); 
  motor3.setSpeed(200); 
  motor4.setSpeed(200);
}
void forward(){      // This function moves the wheels forward 
    motor1.run(FORWARD); 
  motor2.run(FORWARD); 
  motor3.run(FORWARD); 
  motor4.run(FORWARD);
  
} 
void backward() {      // This function moves the wheels backward 
    motor1.run(BACKWARD); 
  motor2.run(BACKWARD); 
  motor3.run(BACKWARD); 
  motor4.run(BACKWARD); 
} 
void haltMotors()   // This function stops each motor (It is better to stop the motors before changing direction.) 
{ 
  motor1.run(RELEASE); 
  motor2.run(RELEASE); 
  motor3.run(RELEASE); 
  motor4.run(RELEASE); 
} 
void turnRight(){    // This function turns the robot right.   
  motor1.run(FORWARD); 
  motor2.run(BACKWARD); 
  motor3.run(BACKWARD); 
  motor4.run(FORWARD); 
} 
void turnLeft(){
  motor1.run(BACKWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(BACKWARD);
}

unsigned long ping(int j) {
  int duration;
  pinMode (trigPin[j], OUTPUT);
  pinMode (echoPin[j], INPUT);
  digitalWrite(trigPin[j], LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin[j], HIGH);
  duration = pulseIn(echoPin[j], HIGH);
  return (duration/2) / 29.1;
}

unsigned long smallest_value (unsigned long array[3]){ // function to find the smallest value int the array
  unsigned long  min_value; 
  int n;
  
  min_value=array[0];
  for (n=1; n<3; n++){
    if(array[n]<min_value){
      min_value=array[n];
      
    }
  }
  return min_value;
}
int pos (unsigned long array[3]){ // function to find position of the smallest value in the array
  int min_value, poS, n;
  poS=0;
  min_value=array[0];
  for (n=1; n<3; n++){
    if(array[n]<min_value){
      min_value=array[n];
      poS=n;
    }
  }
  return poS;
}

void loop(){
  int l, minimum, Position, count;
  unsigned long distance[3];
  for (i=0, l=0; i<3, l<3; i++, l++){
  distance[l]=ping(i);
  Serial.print (names[i]);
  Serial.print(distance[l]);
  Serial.print("cm");
  delay(1000);
  Serial.println();
  }
  minimum=smallest_value(distance);
  Position=pos(distance);
  Serial.print (minimum);
  Serial.println();
  Serial.print (Position);
  Serial.println();
  delay (1000);
  if (minimum!=0){
  if (minimum>10){
    forward();}
    else if (minimum<10 && Position==0){ //if the smallest value is smaller than 10 and the position is 0 (right) 
      turnLeft();}
      else if (minimum<10 && Position==2){
       turnRight();}
        else if(minimum <10 && Position==1){
          haltMotors();}
  }
  else {
    count=0;}
   
}
for (i=0, l=0; i<3, l<3; i++, l++)

Why?

Why?

More to the point. Why don't you read the documentation for the for loop, and see what is allowed. And what isn't.

I used the for loop to read the values on the function ping() and store them on the array distance

I used the for loop to read the values on the function ping() and store them on the array distance

So, how many loop variables do you need? Considering that both of them have the same value every time through loop.

yeah it makes sense, I don’t need the 2 variables. I changed but I’m still getting the same problem.

#include <AFMotor.h> 
AF_DCMotor motor1(1, MOTOR12_64KHZ); 
AF_DCMotor motor2(2, MOTOR12_64KHZ); 
AF_DCMotor motor3(3, MOTOR12_1KHZ); 
AF_DCMotor motor4(4, MOTOR12_1KHZ); 
int trigPin []={8,10,12};
int echoPin []={9,11,13};
int i, j;
char *names[]={"Right ", "Front ", "Left "};

void setup() {
  Serial.begin (9600);
  motor1.setSpeed(200);         // Set the speed for the motors (255 is the maximum)      
  motor2.setSpeed(200); 
  motor3.setSpeed(200); 
  motor4.setSpeed(200);
}
void forward(){      // This function moves the wheels forward 
    motor1.run(FORWARD); 
  motor2.run(FORWARD); 
  motor3.run(FORWARD); 
  motor4.run(FORWARD);
  
} 
void backward() {      // This function moves the wheels backward 
    motor1.run(BACKWARD); 
  motor2.run(BACKWARD); 
  motor3.run(BACKWARD); 
  motor4.run(BACKWARD); 
} 
void haltMotors()   // This function stops each motor (It is better to stop the motors before changing direction.) 
{ 
  motor1.run(RELEASE); 
  motor2.run(RELEASE); 
  motor3.run(RELEASE); 
  motor4.run(RELEASE); 
} 
void turnRight(){    // This function turns the robot right.   
  motor1.run(FORWARD); 
  motor2.run(BACKWARD); 
  motor3.run(BACKWARD); 
  motor4.run(FORWARD); 
} 
void turnLeft(){
  motor1.run(BACKWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(BACKWARD);
}

unsigned long ping(int j) {
  int duration;
  pinMode (trigPin[j], OUTPUT);
  pinMode (echoPin[j], INPUT);
  digitalWrite(trigPin[j], LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin[j], HIGH);
  duration = pulseIn(echoPin[j], HIGH);
  return (duration/2) / 29.1;
}

unsigned long smallest_value (unsigned long array[3]){ // function to find the smallest value int the array
  unsigned long min_value;
  int n;
  
  min_value=array[0];
  for (n=1; n<3; n++){
    if(array[n]<min_value){
      min_value=array[n];
      
    }
  }
  return min_value;
}
int pos (unsigned long array[3]){ // function to find position of the smallest value in the array
  int min_value, poS, n;
  poS=0;
  min_value=array[0];
  for (n=1; n<3; n++){
    if(array[n]<min_value){
      min_value=array[n];
      poS=n;
    }
  }
  return poS;
}

void loop(){
  int l, minimum, Position, count;
  unsigned long distance[3];
  for (i=0, l=0; i<3, l<3; i++, l++){
  distance[l]=ping(i);
  Serial.print (names[i]);
  Serial.print(distance[l]);
  Serial.print("cm");
  delay(1000);
  Serial.println();
  }
  minimum=smallest_value(distance);
  Position=pos(distance);
  Serial.print ("Smallest value ");
  Serial.print (minimum); 
  Serial.println();
  Serial.print ("position of smallest ");
  Serial.print (Position);
  Serial.println();
  delay (1000);
 if (minimum!=0){
  if (minimum>10){
    forward();}
    else if (minimum<10 && Position==0){ //if the smallest value is smaller than 10 and the position is 0 (right) 
      turnLeft();}
      else if (minimum<10 && Position==2){
        turnRight();}
        else if(minimum <10 && Position==1){
          haltMotors();}
  }
  else {
    count=0;}
   
}

the output that I get is
Right 55cm
Front 57cm
Left 75cm
Smallest value 55
position of smallest 0
Right 0cm
Front 49cm
Left 74cm
Smallest value 0
position of smallest 0
Right 54cm
Front 46cm
Left 73cm
Smallest value 46
position of smallest 1
Right 0cm
Front 44cm
Left 73cm
Smallest value 0
position of smallest 0

sorry I put the older code in there.

#include <AFMotor.h> 
AF_DCMotor motor1(1, MOTOR12_64KHZ); 
AF_DCMotor motor2(2, MOTOR12_64KHZ); 
AF_DCMotor motor3(3, MOTOR12_1KHZ); 
AF_DCMotor motor4(4, MOTOR12_1KHZ); 
int trigPin []={8,10,12};
int echoPin []={9,11,13};
int i, j;
char *names[]={"Right ", "Front ", "Left "};

void setup() {
  Serial.begin (9600);
  motor1.setSpeed(200);         // Set the speed for the motors (255 is the maximum)      
  motor2.setSpeed(200); 
  motor3.setSpeed(200); 
  motor4.setSpeed(200);
}
void forward(){      // This function moves the wheels forward 
    motor1.run(FORWARD); 
  motor2.run(FORWARD); 
  motor3.run(FORWARD); 
  motor4.run(FORWARD);
  
} 
void backward() {      // This function moves the wheels backward 
    motor1.run(BACKWARD); 
  motor2.run(BACKWARD); 
  motor3.run(BACKWARD); 
  motor4.run(BACKWARD); 
} 
void haltMotors()   // This function stops each motor (It is better to stop the motors before changing direction.) 
{ 
  motor1.run(RELEASE); 
  motor2.run(RELEASE); 
  motor3.run(RELEASE); 
  motor4.run(RELEASE); 
} 
void turnRight(){    // This function turns the robot right.   
  motor1.run(FORWARD); 
  motor2.run(BACKWARD); 
  motor3.run(BACKWARD); 
  motor4.run(FORWARD); 
} 
void turnLeft(){
  motor1.run(BACKWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(BACKWARD);
}

unsigned long ping(int j) {
  int duration;
  pinMode (trigPin[j], OUTPUT);
  pinMode (echoPin[j], INPUT);
  digitalWrite(trigPin[j], LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin[j], HIGH);
  duration = pulseIn(echoPin[j], HIGH);
  return (duration/2) / 29.1;
}

unsigned long smallest_value (unsigned long array[3]){ // function to find the smallest value int the array
  unsigned long min_value;
  int n;
  
  min_value=array[0];
  for (n=1; n<3; n++){
    if(array[n]<min_value){
      min_value=array[n];
      
    }
  }
  return min_value;
}
int pos (unsigned long array[3]){ // function to find position of the smallest value in the array
  int min_value, poS, n;
  poS=0;
  min_value=array[0];
  for (n=1; n<3; n++){
    if(array[n]<min_value){
      min_value=array[n];
      poS=n;
    }
  }
  return poS;
}

void loop(){
  int minimum, Position, count;
  unsigned long distance[3];
  for (i=0; i<3; i++){
  distance[i]=ping(i);
  Serial.print (names[i]);
  Serial.print(distance[i]);
  Serial.print("cm");
  delay(1000);
  Serial.println();
  }
  minimum=smallest_value(distance);
  Position=pos(distance);
  Serial.print ("Smallest value ");
  Serial.print (minimum); 
  Serial.println();
  Serial.print ("position of smallest ");
  Serial.print (Position);
  Serial.println();
  delay (1000);
 if (minimum!=0){
  if (minimum>10){
    forward();}
    else if (minimum<10 && Position==0){ //if the smallest value is smaller than 10 and the position is 0 (right) 
      turnLeft();}
      else if (minimum<10 && Position==2){
        turnRight();}
        else if(minimum <10 && Position==1){
          haltMotors();}
  }
  else {
    count=0;}
   
}

Right 55cm
Front 69cm
Left 77cm
Smallest value 55
position of smallest 0
Right 0cm
Front 56cm
Left 76cm
Smallest value 0
position of smallest 0
Right 55cm
Front 52cm
Left 76cm
Smallest value 52
position of smallest 1
Right 0cm
Front 48cm
Left 76cm
Smallest value 0
position of smallest 0

Instead of making two passes through the array, to find the minimum value and the location of the minimum value, you can just find the index to the smallest position, and then read the value in that position.

It would be worth putting some more Serial.print() statements in. What value is passed to the ping() function?

The ping() function is a little strange, too. It is defined as returning an unsigned long, when, in reality, the value being returned is an int that is valued as a result of a float calculation. Consistency is a good thing.

for (i=0, l=0; i<3, l<3; i++, l++)

Don’t you find the variable name “l” confusing? Doesn’t it look like “1” (one)?

Can you spot the difference between:

a = l;   // assign lower-case L
a = 1;   // assign the number one

I’ve already suggested you don’t use names that differ only in case:

int pos (unsigned long array[3]){ // function to find position of the smallest value in the array
  int min_value, poS, n;
  poS=0;

poS and pos.

Careful choice of data names make code easier to understand and modify.