HC-SR04 Ultrisonic Range Finder Help

Hello,

I'm new to the Arduino Uno and am having an issue with adding a second HC-SR04 to the sketch. Distance 1 reads fine but Distance 2 is out of range. This line: distance2 = (duration2 / 2) * 0.0343;
has a value of 0.00. Seems this should work?

/*
HC-SR04 Basic Demonstration
HC-SR04-Basic-Demo.ino
Demonstrates functions of HC-SR04 Ultrasonic Range Finder
Displays results on Serial Monitor

DroneBot Workshop 2017

*/

// This uses Serial Monitor to display Range Finder distance readings

// Hook up HC-SR04 with Trig to Arduino Pin 10, Echo to Arduino pin 13

#define trigPin1 9
#define trigPin2 10
#define echoPin1 11
#define echoPin2 12

float duration1, distance1, duration2, distance2;

void setup() {
Serial.begin (115200);
pinMode(trigPin1, OUTPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(echoPin2, INPUT);
}

void loop() {

// Write a pulse to the HC-SR04 Trigger Pin

digitalWrite(trigPin1, LOW);
//delayMicroseconds(2);
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
//delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
//delayMicroseconds(10);
digitalWrite(trigPin2, LOW);

// Measure the response from the HC-SR04 Echo Pin

duration1 = pulseIn(echoPin1, HIGH);
delayMicroseconds(2);
duration2 = pulseIn(echoPin2, HIGH);
delayMicroseconds(2);

// Determine distance from duration
// Use 343 metres per second as speed of sound

distance1 = (duration1 / 2) * 0.0343;
delayMicroseconds(2);
distance2 = (duration2 / 2) * 0.0343;
delayMicroseconds(2);
//Serial.print(distance1);
//delay(500);
//Serial.print(distance2);
// delay(500);

// Send results to Serial Monitor

Serial.print(" Distance 1: ");

if (distance1 >= 400 || distance1 <= 2) {
Serial.print("Out of range");
}
else {
Serial.print(distance1);
Serial.print(" cm ");
}

Serial.print(" Distance 2: ");

if (distance2 >= 400 || distance2 <= 2) {
Serial.print("Out of range");
}
else {
Serial.print(distance2);
Serial.print(" cm");
}

Serial.println(" ");
}

You trigger both sensors in quick succession, then read the first with pulseIn() - a blocking function returning only when it's complete, then read the second sensor, whose pulse is by then either in progress or completed already - in either case no pulse will be detected by the second pulseIn() call, the function times out after 1 second, and returns 0.

Thanks you! Fixed the issue.