Ultrasonic sensor HC-SR04

I've been having problems with getting 3 HC-SR04s to work. My connections are correct and I can't seem to find any problems with the code, yet the serial monitor displays the sensing distance as zero all the time.

This is my code:

const int leftForward = 2;
const int leftBackward = 3;
const int rightForward = 4;
const int rightBackward = 5;

float duration ;
float distance ;

int trigPin1 = 6;   //mid
int echoPin1 = 7;

int trigPin2 = 8;  //left
int echoPin2 = 11;

int trigPin3 = 12;   //right
int echoPin3 = 13;

int a=0;
int c=0;


void setup()
{
  Serial.begin(9600);
  Serial.print("Starting...\n");
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);

  pinMode(leftForward , OUTPUT);
  pinMode(leftBackward , OUTPUT);
  pinMode(rightForward , OUTPUT);
  pinMode(rightBackward , OUTPUT);
 
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);

  pinMode(leftForward , OUTPUT);
  pinMode(leftBackward , OUTPUT);
  pinMode(rightForward , OUTPUT);
  pinMode(rightBackward , OUTPUT);


}
void firstsensor()// This function is for first sensor.
{ 

  float duration1, distance1;
  digitalWrite (trigPin1, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigPin1, LOW);
  duration1 = pulseIn (echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;
 
      Serial.print("1st Sensor: ");
      Serial.print(distance1);  
      Serial.print("cm    ");
      Serial.println()
  
  if (distance1 < 30)  // middle
  
  {  
    digitalWrite(leftForward , LOW);
    digitalWrite(leftBackward , HIGH);
    digitalWrite(rightForward , HIGH);
    digitalWrite(rightBackward , LOW);

    a=1;
 
     }

     else 
 {   
    digitalWrite(leftBackward , LOW);
    digitalWrite(rightForward ,   HIGH);
    digitalWrite(rightBackward , LOW);
    digitalWrite(leftForward , HIGH); 

    }    
}
void secondsensor() // This function is for second sensor.
{ 
    float duration2, distance2;
    digitalWrite (trigPin2, HIGH);
    delayMicroseconds (10);
    digitalWrite (trigPin2, LOW);
    duration2 = pulseIn (echoPin2, HIGH);
    distance2 = (duration2/2) / 29.1;
   
      Serial.print("2nd Sensor: "); 
      Serial.print(distance2);  
      Serial.print("cm    ");
      Serial.println();

    if (distance2 < 20) //right
    {  
      digitalWrite(rightForward , HIGH);
      digitalWrite(rightBackward , LOW); 
      digitalWrite(leftForward , LOW);
      digitalWrite(leftBackward , HIGH);

      c=1;
    }
    
 else 
 {
    digitalWrite(leftForward , HIGH);
    digitalWrite(leftBackward , LOW);
    digitalWrite(rightForward , HIGH);
    digitalWrite(rightBackward , LOW);

    }    
}
void thirdsensor()// This function is for third sensor.
{ 
    float duration3, distance3;
    digitalWrite (trigPin3, HIGH);
    delayMicroseconds (10);
    digitalWrite (trigPin3, LOW);
    duration3 = pulseIn (echoPin3, HIGH);
    distance3 = (duration3/2) / 29.1;

      Serial.print("3rd Sensor: ");   
      Serial.print(distance3);  
      Serial.print("cm");
      Serial.println();

    if (distance3 < 20)   //                         left
    {  
      
    digitalWrite(leftForward , HIGH);
    digitalWrite(leftBackward , LOW);
    digitalWrite(rightForward , LOW);
    digitalWrite(rightBackward , HIGH);  

    }
       
 else
 {
    digitalWrite(leftForward , HIGH);
    digitalWrite(leftBackward , LOW);
    digitalWrite(rightForward , HIGH);
    digitalWrite(rightBackward , LOW);

    }   
}


void loop()
{ 

firstsensor();

secondsensor();
  
thirdsensor();

}

(leftForward, rightForward and so on are for the motors but that's not the main problem.)

I've been having problems with getting 3 HC-SR04s to work.

Have you tried just getting one to work and without all the motor stuff, just strip it down to the single sensor.

If that will not work post the code of that along with a schematic of the wiring.

If you are successful then add two more sensors.

Writing a separate function for each sensor is a bit turgid, just use one function and pass in the pin numbers you want to use and return the distance reading you get.

Actually, they worked fine when I tested them a while ago. The problems came when I combined them with the motors and pixy cmucam5 (camera).

What is the time interval between “1st Sensor” and “2nd Sensor” appearing on the serial console? If it is about 1 second, then pulseIn hits the default timeout because it does not detect the pulse.

Alysia:
Actually, they worked fine when I tested them a while ago. The problems came when I combined them with the motors and pixy cmucam5 (camera).

Ok just add one component at a time, so does it fall over with Motors? What if you replace the Motors with LEDs?

Then add the camera, does it stop working then?

Will the code you posted actually work?

6v6gt:
What is the time interval between “1st Sensor” and “2nd Sensor” appearing on the serial console? If it is about 1 second, then pulseIn hits the default timeout because it does not detect the pulse.

I inserted a 2 seconds delay and it works fine when testing the ultrasonic sensors alone. But the moment I combine them with a pixy cmucam5, the distance goes back to zero.
In the midst of trying to fix this problem, I discovered an odd problem whereby the ultrasonic sensors start working when I place my fingers under the Arduino mega where the pins are connected. Not sure if that's a circuit problem or not.

I discovered an odd problem whereby the ultrasonic sensors start working when I place my fingers under the Arduino mega where the pins are connected. Not sure if that's a circuit problem or not.

That is a sign of a floating input. That is an input not being pulled in one direction or the other.

But the moment I combine them with a pixy cmucam5, the distance goes back to zero.

That sounds like a conflict between the use of timers, as if the timer configuration for the camera is stopping the duration from working. Does the documentation say anything about this? Can you post a link to the camera.

Or it could be as simple as the camera using one of the same pins as your sensors.