help with two ultrasonic sensors

I need help! I do not know much about the Arduino theme.
I’m working with two ultrasonic sensors to measure distances.

With a sensor everything is fine, but I connect the second sensor does not read it and it shows me 0 cm on the screen

What I can do?
I do not know what I’ll be doing wrong! I would appreciate the help of someone!!! :slight_smile:
This is the code:

include <Ultrasonic.h>
// Sensor 1, trigger pin 11 y echo pin 10.
const int trigPin1 = 11;
const int echoPin1 = 10;

// Sensor 2, trigger pin 9 y echo pin 8.
const int trigPin2 = 9;
const int echoPin2 = 8;

long duration1, duration2,distancia1, distancia2;
int ledRojo = 3;
int ledVerde = 4;

void setup () {

pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(ledRojo, OUTPUT);
pinMode(ledVerde, OUTPUT);
Serial.begin (9600);
}

void loop() {
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distancia1= duration1*0.034/2;

//Sensor2
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distancia2= duration2*0.034/2;

if (distancia2 < 50 && distancia1 < 50){
digitalWrite(ledVerde, LOW);
digitalWrite(ledRojo, HIGH);
Serial.print(distancia1);
Serial.println(" cm");
Serial.println(" “);
Serial.print(distancia2);
Serial.println(” cm");
}
else {
digitalWrite(ledVerde, HIGH);
digitalWrite(ledRojo, LOW);
Serial.print(distancia1);
Serial.println(" cm");
Serial.println(" “);
Serial.print(distancia2);
Serial.println(” cm");

}
delay(500);

}

I’ve also used a same type of code and got same error
can any one help

#define trigPin1 13
#define echoPin1 12
#define trigPin2 10
#define echoPin2 11
int counter = 0;
int currentState = 0;
int previousState = 0;
void setup() {
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
}

void loop() {
long duration1, distance1;
long duration2, distance2;
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = (duration1/2) / 29.1;
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distance2 = (duration2/2) / 29.1;
if (distance1 <= 10 && distance2 <= 10){
currentState = 1;
}
else {
currentState = 0;
}
delay(100);
if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;

Serial.println(counter);
}
}
}

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn’t show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you’re using the Arduino Web Editor you will not have access to this useful tool but it’s still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

Does putting a short delay between reading one sensor and the next one help? Maybe its getting a false reading from echos of the first one?