self driving arduino car with 3 ultrasonic sensors

So im trying to build a self driving car with arduino but my third sensor is always saying 0cm so the car dosent work could somone look at the code and fix it tank you.

#include <AFMotor.h>

#include <AFMotor.h> //import your motor shield library
#define trigPin 12 // define the pins of your sensor
#define echoPin 13
#define trigPin1 8 // define the pins of your sensor
#define echoPin1 9
#define trigPin2 10 // define the pins of your sensor
#define echoPin2 11
AF_DCMotor motor1(1,MOTOR12_64KHZ); // set up motors.
AF_DCMotor motor2(2, MOTOR12_8KHZ);

void setup() {
Serial.begin(9600); // begin serial communitication
Serial.println("Motor test!");
pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
pinMode(trigPin1, OUTPUT);// set the trig pin to output (Send sound waves)
pinMode(echoPin1, INPUT);// set the echo pin to input (recieve sound waves)
pinMode(trigPin2, OUTPUT);// set the trig pin to output (Send sound waves)
pinMode(echoPin2, INPUT);// set the echo pin to input (recieve sound waves)

motor1.setSpeed(150); //set the speed of the motors, between 0-255
motor2.setSpeed (250);
}

void loop() {

long duration, distance, duration1, distance1, duration2, distance2; // start the scan

digitalWrite(trigPin, LOW);
delayMicroseconds(2); // delays are required for a succesful sensor operation.
digitalWrite(trigPin, HIGH);

delayMicroseconds(10); //this delay is required as well!
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;// convert the distance to centimeters.
if (distance < 35)/*if there's an obstacle 25 centimers, ahead, do the following: */ {
Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance From Robot is " );
Serial.print ( distance);
Serial.print ( " CM!");// print out the distance in centimeters.

Serial.println (" The obstacle is declared a threat due to close distance. ");
Serial.println (" Turning !");

;motor1.run(BACKWARD); // Turn as long as there's an obstacle ahead.
motor2.run (FORWARD);

}
else {
Serial.println ("No obstacle detected. going forward");
delay (15);
motor1.run(BACKWARD); //if there's no obstacle ahead, Go Forward!
motor2.run(BACKWARD);
}

digitalWrite(trigPin2, LOW);
delayMicroseconds(2); // delays are required for a succesful sensor operation.
digitalWrite(trigPin2, HIGH);

delayMicroseconds(10); //this delay is required as well!
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distance2 = (duration2/2) / 29.1;// convert the distance to centimeters.
if (distance2 < 30)/*if there's an obstacle 25 centimers, ahead, do the following: */ {
Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance From Robot is " );
Serial.print ( distance2);
Serial.print ( " CM!");// print out the distance in centimeters.

Serial.println (" The obstacle is declared a threat due to close distance. ");
Serial.println (" Turning !");

;motor1.run(BACKWARD); // Turn as long as there's an obstacle ahead.
motor2.run (FORWARD);

}
else {
Serial.println ("No obstacle detected. going forward");
delay (15);
motor1.run(BACKWARD); //if there's no obstacle ahead, Go Forward!
motor2.run(BACKWARD);
}

digitalWrite(trigPin1, LOW);
delayMicroseconds(2); // delays are required for a succesful sensor operation.
digitalWrite(trigPin1, HIGH);

delayMicroseconds(10); //this delay is required as well!
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = (duration2/2) / 29.1;// convert the distance to centimeters.
if (distance1 < 30)/*if there's an obstacle 25 centimers, ahead, do the following: */ {
Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance From Robot is " );
Serial.print ( distance1);
Serial.print ( " CM!");// print out the distance in centimeters.

Serial.println (" The obstacle is declared a threat due to close distance. ");
Serial.println (" Turning !");

;motor1.run(FORWARD); // Turn as long as there's an obstacle ahead.
motor2.run (BACKWARD);

}
else {
Serial.println ("No obstacle detected. going forward");
delay (15);
motor1.run(BACKWARD); //if there's no obstacle ahead, Go Forward!
motor2.run(BACKWARD);
}

}

Please edit your post to add code tags (see the "How to use this forum" post).

Also from a glance you read three same sensors and the same way. Put that in a function, that simplifies your code big time. Then just call that function with the appropriate trig/echo pins as arguments, and have it return the measured distance.

The current code has a loop where I am measuring the distance from the 1st sensor, then from 2nd sensor and then from 3rd sensor. The code is crude code and I agree it can be optimized by using a function as suggested.

While running, the code measures the distance from the 1st and 2nd sensors without any issue. Then, when it measures the distance with the 3rd sensor it always reports 0 cm (zero). I do not understand why this happens since the very same code successfully measures the distance with the 1st and 2nd sensors.

Making a function out of that code will not fix the issue, unless I am missing something here. Do you mind explaining a bit more?

Could be some limitation of the Arduino mega board, a voltage mismatch, etc?

If you use a single function to read your sensors, your code will be shorter, more readable, more maintainable, and it will be simpler to make fewer ranging pulses, so that distant echoes are not mistaken for nearer ones.

Posting the same question in different parts of the forum is a shortcut to a forum timeout, so I deleted the duplicate of this topic.

the problem is thath the third sensor is giving a reading of 0 cm its not a prople of echos somting with the code is wrong and i dont know wath , the hardware is all go ans tested if somone could take the time a writhe the code in a better way so this robot would function

If it's really the same code, it's most likely a hardware (wiring) problem.

i understand thath but i checked all my wiring tested with mutiple, utrasonic sensors and still not working the voltage for the sensor was testes using difrent pins for the trig and the echo so i relly dont know

Swap out sensors for starters - make sure the sensors themselves are fine.If you swap the second and third position sensors and it's still the third position reading zero, it's in the wiring. If it's changed to the second position, it's the sensor.
Or write a piece of code that reads one and only one sensor, to make sure the sensors at least work independently.
Putting code in functions also helps making sure there are no sneaky typos and that it's really the same code for all sensors.
If either the trig or the echo signal doesn't come through, the pulseIn() will timeout and return zero.

i switched the sensors and still the 3 position sensor dosent work so its a code problem i checked thath the cod is good i dont see any typos it might be somting with how the sensor in finds waths the distance so he delay from the trig to the echo might be wrong if you could check it out. tank you

I take it you still didn't clean up your code placing the sensor reading code in a function, shared by all?
Then switch pin definitions of the second and third sensor.
Read that third sensor with the known good code of the second, and the second known good sensor with the suspect code of the third.

My money is still on the wiring.

i suitched them up as you said but still not working reading a value of 0cm. Do you want me to write the sketch for you

wath is a website that i can show my wiring so you can check it out

lucu007:
i suitched them up as you said but still not working reading a value of 0cm.

Which sensor is giving the errant reading? The same physical sensor? Or the third that's being read?

The third sensor that's being read is giving the error so its a code problem not a wiring problem.

can someone write this code in a function i really dont know how.

You still didn't put your sensor reading code in a separate function, did you?

That simple action will guarantee you that your code is correct for all three of them. Plus all the other advantages as pointed out before.

This code should do the same as yours, it's just a bit shorter and a bit more readable and maintainable. If this reads two sensors correctly and the third not, it's not a software issue.

#include <AFMotor.h> //import your motor shield library
uint8_t trigPin[] = {12, 8, 10};
uint8_t echoPin[] = {13, 9, 11};
AF_DCMotor motor1(1, MOTOR12_64KHZ); // set up motors.
AF_DCMotor motor2(2, MOTOR12_8KHZ);

void setup() {
  Serial.begin(9600); // begin serial communitication
  Serial.println("Motor test!");
  for (uint8_t i = 0; i < 3; i++) {
    pinMode(trigPin[i], OUTPUT);// set the trig pin to output (Send sound waves)
    pinMode(echoPin[i], INPUT);// set the echo pin to input (recieve sound waves)
  }
  motor1.setSpeed(150); //set the speed of the motors, between 0-255
  motor2.setSpeed (250);
}

void loop() {

  float distance = readSensor(0);
  if (distance < 35) { /*if there's an obstacle <35 centimers, ahead, do the following: */
    makeTurn(distance, 0);
  }
  else {
    moveForward();
  }

  distance = readSensor(2);
  if (distance < 30) { /*if there's an obstacle <30 centimers, ahead, do the following: */
    makeTurn(distance, 2);
  }
  else {
    moveForward();
  }

  distance = readSensor(1);
  if (distance < 30) { /*if there's an obstacle <30 centimers, ahead, do the following: */
    makeTurn(distance, 1);
  }
  else {
    moveForward();
  }
}

float readSensor(uint8_t sensor) {
  digitalWrite(trigPin[sensor], LOW);
  delayMicroseconds(2); // delays are required for a succesful sensor operation.
  digitalWrite(trigPin[sensor], HIGH);
  delayMicroseconds(10); //this delay is required as well!
  digitalWrite(trigPin[sensor], LOW);
  int duration = pulseIn(echoPin[sensor], HIGH, 30000);
  return ((float)duration / 2) / 29.1; // convert the distance to centimeters.
}

void makeTurn(float distance, uint8_t sensor) {
  Serial.print("Close Obstacle detected by sensor ");
  Serial.print(sensor);
  Serial.println("!" );
  Serial.println("Obstacle Details:");
  Serial.print("Distance From Robot is " );
  Serial.print(distance);
  Serial.print(" CM!");// print out the distance in centimeters.
  Serial.println(" The obstacle is declared a threat due to close distance. ");
  Serial.println(" Turning !");
  motor1.run(FORWARD); // Turn as long as there's an obstacle ahead.
  motor2.run(BACKWARD);
}

void moveForward() {
  Serial.println ("No obstacle detected. going forward");
  delay (15);
  motor1.run(BACKWARD); //if there's no obstacle ahead, Go Forward!
  motor2.run(BACKWARD);
}

now the (sensor 2) so the sensor 3 is saying 0.05cm so it not 0 anymore

Ah! That's a result.

One error I fixed is that you used a wrong datatype for the distance: integer instead of float. So things get rounded and it looks like you get a time out instead of very small reading.

Add a delay between the readings. 20-50 ms should be enough to let the echoes die out. Just start with a delay(20) after reading the second sensor. Increase that a bit if needed. That delay is short enough it shouldn't affect the operation of the robot.

can you sow me wer to put the delay because i dont tink i put it in the write place