ayuda programa para conectar 2 sensores ultrasonicos srf04 al arduino

hice este programa que les pegare al ultimo del post. mi problema es que a la hora de correrlo solo me funciona un sensor y el otro no, y cheque que los sensores funcionaran por separado y si lo hacen, pero con este programa que hice nadamas sirve uno, me podrian ayudar en identificar la solucion, o que le puedo modificar a mi programa para que me funcionen los dos sensores ultrasonicos a la vez.
Estoy casi seguro que el problema esta en el programa, ojala y me puedan ayudar,gracia
este es el programa:
int led1=13;
int led2=8;
int pingPin = 7;
int pingPin2 = 6;
int value = 0; // Valor del pulsador
int value2 =0;

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration,duration2, cm,cm2;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

pinMode(pingPin2, OUTPUT);
digitalWrite(pingPin2, LOW);
delayMicroseconds(2);
digitalWrite(pingPin2, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin2, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin2, INPUT);
duration2 = pulseIn(pingPin2, HIGH);

// convert the time into a distance

cm = microsecondsToCentimeters(duration);
cm2 = microsecondsToCentimeters(duration2);
value = duration / 29 / 2;
value2 = duration2 / 29 / 2;

pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);

Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);

Serial.print(cm2);
Serial.print("cm2");
Serial.println();
delay(100);

if
( cm <=20 )

{digitalWrite (led1,HIGH);}
else
{digitalWrite (led2,LOW);}

if
( value2 <= 20)
{digitalWrite (led2,HIGH);
}
else
{digitalWrite (led2,LOW);}

}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

les agradeceria mucho sus respuestas.

_9_de_octubre_prueba_ultrasonidos.ino (2.18 KB)

Hola! conseguiste finalmente hacer que funcionasen los 2 sensores a la vez?? A mi me gustaría hacer que funcionasen 4 SRF04, aun que con 2 ya estaría contento... :slight_smile:

Para usar varios sensores a la vez tienes que usar la libreria SoftwareSerial.
Un ejemplo de como seria en codigo:

#include <SoftwareSerial.h>

// define the digital pins to use as RX and TX for two
// software serial connections
const int RX1 = 8;
const int TX1 = 9;
const int RX2 = 10;
const int TX2 = 11;

// create SoftwareSerial objects
SoftwareSerial SoftSerialOne(RX1,TX1);
SoftwareSerial SoftSerialTwo(RX2,TX2);

void setup(void) {
// setup the software serial pins
pinMode(RX1, INPUT);
pinMode(RX2, INPUT);
pinMode(TX1, OUTPUT);
pinMode(TX2, OUTPUT);
}

void loop(void) {
SoftSerialOne.begin(9600); // begin communication on the first 
// software serial channel
SoftSerialOne.print("Hello World"); // send something
SoftSerialOne.end(); // end communication on the first software
// serial channel
SoftSerialTwo.begin(9600); // begin communication on the second 
// software serial channel
SoftSerialTwo.print("Hello World"); // send something
SoftSerialTwo.end(); // end communication on the second software
// serial channel
}