Circuit with two Ultrasonic Sensors.

Right now individually each of the ultrasonic sensors work, but when i try to run code with both of them, it is unable to do it.
Am I writing it wrong?

const int pingPin = 13;
const int inPin = 12;
const int pingPink = 11;
const int inPink = 10;

void setup() {

Serial.begin(9600);
}

void loop()
{

float duration, inches;
float velocity;
float durationk, inchesk;

pinMode(pingPin, OUTPUT);
pinMode(pingPink, OUTPUT);
digitalWrite(pingPin, LOW);
digitalWrite(pingPink, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
digitalWrite(pingPink, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
digitalWrite(pingPink, LOW);

pinMode(inPin, INPUT);
pinMode(inPink, INPUT);
duration = pulseIn(inPin, HIGH);
durationk = pulseIn(inPink, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
inchesk = microsecondsToInches(durationk);

int time = 0;
boolean timerOn = false;
boolean timerStart = true;

if(timerOn) {
time = time + 10;
}
if(timerStart && inches < 9) {
timerOn = true;
timerStart = false;
}
if(inchesk < 9) {
timerOn = false;
velocity = 6 / time / 12 * 1000;
Serial.print(velocity);
Serial.println(" feet/second");
}

Serial.println(inches);
Serial.println(inchesk);
Serial.println(time);

delay(10);
}

float microsecondsToInches(float microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
return microseconds / 73.746 / 2;
}

long microsecondsToMillimeters(long microseconds)
{
// The speed of sound is 340.29 m/s or 29.3867 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 / 2.93867 / 2;
}

Thanks!

Probably best to alternate between the two rather then to try to ping both simultaneously:

  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);
  pinMode(inPin, INPUT);
  duration = pulseIn(inPin, HIGH);

  pinMode(pingPink, OUTPUT);
  digitalWrite(pingPink, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPink, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPink, LOW);
  pinMode(inPink, INPUT);
  durationk = pulseIn(inPink, HIGH);

I tried that as well. I get an accurate reading from whichever is pinged first, and the second one reads constantly at 0.

You cannot ping them both simultaneously as the beams will interfere.

Also, with the following code, the pulse output from the second sonar will be coming
out while the first pulsein() is still reading, I think, so there is no pulse for the 2nd
pulsein() to read.

duration = pulseIn(inPin, HIGH);
durationk = pulseIn(inPink, HIGH);

If that were the problem then John's suggestion should work, because I would be isolating the two pings, no?
None of you would happen to know of any examples of circuits/code implementing two ultrasonic sensors that I could model my project off of would you?

If that were the problem then John's suggestion should work, because I would be isolating the two pings, no?
None of you would happen to know of any examples of circuits/code implementing two ultrasonic sensors that I could model my project off of would you?

John was giving you the standard code that 100s of people have probably used. If your system still
doesn't work, then you probaby have "2" problems, first was the code problem already mentioned,
second is some electrical/hookup/hardware problem.

Can i ask here??

I want to use two ultrasonic sensor
the first code uses vibrating motor as an alarm
while the second sensor uses a two LED and a buzzer/speaker a an alarm too..

this two codes works fine when i tested it with my sensor
but when i combined it in the loop and modifying the codes and pin designations it does not work fine..
SO i am looking for a help or tutorial on how could i make this two code be in one
thanks for those who would help..

First

const int trigger=5;
const int echo=6;
const int motorPin=7;
long int duration, distanceInches, distanceCm;
int LimitCm = 100;

void setup()
{
pinMode(motorPin, OUTPUT);
}
void loop()
{
pinMode (trigger, OUTPUT);
pinMode (echo, INPUT);
digitalWrite(trigger, LOW);
delayMicroseconds (2);
digitalWrite(trigger,HIGH);
delayMicroseconds(5);
digitalWrite(trigger,LOW);

pinMode(echo, INPUT);
duration = pulseIn(echo,HIGH);

distanceInches=microsecondsToInches(duration);
distanceCm=microsecondsToCentimeters(duration);

checkLimit();
delay (100);
}
void checkLimit()
{
if(distanceCm < LimitCm)
{
digitalWrite(motorPin,HIGH);
}
else
{
digitalWrite(motorPin,LOW);
}
}
long microsecondsToInches (long microseconds)
{
return microseconds /74/2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds /29/2;
}

Second

const int trigger=5;
const int echo=6;
const int LedPin=7;
const int speaker=8;
const int LedAlert=4;
long int duration, distanceInches, distanceCm;
int LimitCm = 100;

void setup()
{
pinMode(LedPin, OUTPUT);
pinMode(LedAlert, OUTPUT);
pinMode(speaker, OUTPUT);
}
void loop()
{
digitalWrite(LedPin, HIGH);
pinMode (trigger, OUTPUT);
pinMode (echo, INPUT);
digitalWrite(trigger, LOW);
delayMicroseconds (2);
digitalWrite(trigger,HIGH);
delayMicroseconds(5);
digitalWrite(trigger,LOW);

pinMode(echo, INPUT);
duration = pulseIn(echo,HIGH);

distanceInches=microsecondsToInches(duration);
distanceCm=microsecondsToCentimeters(duration);

checkLimit();
delay (100);
}
void checkLimit()
{
if(distanceCm < LimitCm)
{
digitalWrite(LedAlert,HIGH);
delayMicroseconds(2);
digitalWrite(speaker,HIGH);
delayMicroseconds(5);

}
else
{
digitalWrite(LedAlert,LOW);
delayMicroseconds(2);
digitalWrite(speaker,LOW);
delayMicroseconds(5);

}
}
long microsecondsToInches (long microseconds)
{
return microseconds /74/2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds /29/2;
}