2 ping sensors 1 arduino

Hey guys, I’m trying to get two ping sensors to input inches and cm.
I got it to work with one ping sensor, but I’m having trouble letting 2 as an input for (long duration, inches, cm).

I want both ping sensors to act as one entity. So if something is infront of one or/and both of them, it says A once. When something is not infront of both, it says B once.

Here’s what I got so far…

int pingPinA = 4;
int pingPinB = 5;
int serInLen = 25;
char serInString[25];
int pId =0;
int result;

boolean unique = true;

void setup() 
{ 
  Serial.begin(9600); 
  Serial.println("#S|SPXL|[]#");        // start EXCEL
    pId= atoi(serInString);  
             // convert result to integer 
} 
 
void loop() 
{ 
   long durationA, inches, cm;
   long durationB, inches, cm;


char buffer[5];
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
  pinMode(pingPinA, OUTPUT);
  pinMode(pingPinB, OUTPUT);
  digitalWrite(pingPinA, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPinA, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPinA, LOW);
  delayMicroseconds(10); 
  digitalWrite(pingPinB, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPinB, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPinB, 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(pingPinA, INPUT);
  durationA = pulseIn(pingPinA, HIGH);
  pinMode(pingPinB, INPUT);
  durationB = pulseIn(pingPinB, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(durationA, durationB);
  cm = microsecondsToCentimeters(durationA, durationB);
  if(cm>=1,1 && inches<=5,1 &&unique)
  {
  Serial.print("#S|SENDK|[");
     Serial.print(itoa((pId), buffer, 10));
     Serial.print("&");
     Serial.print("a");
     Serial.println("]#");
  Serial.println("#S|DLDATA|[]#");
  unique=false;
  
} 
else if (inches>5 &&!unique)
 {
  Serial.print("#S|SENDK|[");
     Serial.print(itoa((pId), buffer, 10));
     Serial.print("&");
     Serial.print("b");
     Serial.println("]#");
  Serial.println("#S|DLDATA|[]#");
  unique=true;
  
} 
  
  delay(100);
}

  


//read a string from the serial and store it in an array
//you must supply the array variable - will return if timeOut ms passes before the sting is read so you should
//check the contents of the char array before making any assumptions.

long microsecondsToInches(long 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.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

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;
}


void readSerialString (char *strArray,long timeOut) 
{
   long startTime=millis();
   int i;

   while (!Serial.available()) {
      if (millis()-startTime >= timeOut) {
         return;
      }
   }
   while (Serial.available() && i < serInLen) {
      strArray[i] = Serial.read();
      i++;
   }
}

Ok, I’m having trouble with the PulseIn.
It’s not wanting to have 2 ping sensors. It will only work for one of them, so I’m testing out the timeout portion of the PulseIn and now it’s showing the ping sensor is detecting something if there on the actual sensor, but not on gobetwino.

Any clue on what to do?

Here’s my code so far now…

int pingPinA = 4;
int pingPinB = 5;
int serInLen = 25;
char serInString[25];
int pId =0;
int result;

boolean unique = true;

void setup() 
{ 
  Serial.begin(9600); 
  Serial.println("#S|SPXL|[]#");        // start EXCEL
    pId= atoi(serInString);  
             // convert result to integer 
} 
 
void loop() 
{ 
   long durationA, inchesA, cmA;
   long durationB, inchesB, cmB;


char buffer[5];

   pinMode(pingPinA, OUTPUT);
  digitalWrite(pingPinA, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPinA, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPinA, LOW);
  
    pinMode(pingPinB, OUTPUT);
  digitalWrite(pingPinB, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPinB, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPinB, LOW);


  pinMode(pingPinA, INPUT);
  durationA = pulseIn(pingPinA, HIGH, 1000);
  
  pinMode(pingPinB, INPUT);
  durationB = pulseIn(pingPinB, HIGH, 2000);

  // convert the time into a distance
  inchesA = microsecondsToInches(durationA);
  cmA = microsecondsToCentimeters(durationA);
  
  inchesB = microsecondsToInches(durationB);
  cmB = microsecondsToCentimeters(durationB);
  
  if(cmA>=1 && inchesA<=5 &&unique )
  {
  Serial.print("#S|SENDK|[");
     Serial.print(itoa((pId), buffer, 10));
     Serial.print("&");
     Serial.print("a");
     Serial.println("]#");
  Serial.println("#S|DLDATA|[]#");
  unique=false;
  
} 

else if (inchesA>5 &&!unique)
 {
  Serial.print("#S|SENDK|[");
     Serial.print(itoa((pId), buffer, 10));
     Serial.print("&");
     Serial.print("b");
     Serial.println("]#");
  Serial.println("#S|DLDATA|[]#");
  unique=true;
  
} 

if(cmB>=1 && inchesB<=5 &&unique )
  {
  Serial.print("#S|SENDK|[");
     Serial.print(itoa((pId), buffer, 10));
     Serial.print("&");
     Serial.print("a");
     Serial.println("]#");
  Serial.println("#S|DLDATA|[]#");
  unique=false;
  
} 
else if (inchesB>5 &&!unique)
 {
  Serial.print("#S|SENDK|[");
     Serial.print(itoa((pId), buffer, 10));
     Serial.print("&");
     Serial.print("b");
     Serial.println("]#");
  Serial.println("#S|DLDATA|[]#");
  unique=true;
  
} 
 delay(100);
}


long microsecondsToInches(long microseconds)
{

  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{

  return microseconds / 29 / 2;
}


void readSerialString (char *strArray,long timeOut) 
{
   long startTime=millis();
   int i;

   while (!Serial.available()) {
      if (millis()-startTime >= timeOut) {
         return;
      }
   }
   while (Serial.available() && i < serInLen) {
      strArray[i] = Serial.read();
      i++;
   }
}

Sorry for triple posting , but I think someone might have a problem similar as me. I sort of found a solution by adding something that can randomize when to look at a certain PingIn.

If there is a better way, please do post. If not here, here you go guys…

int pingPinA = 4;
int pingPinB = 5;
int serInLen = 25;
char serInString[25];
int pId =0;
int result;
boolean sensorState = false;

boolean unique = true;

void setup() 
{ 
  Serial.begin(9600); 
  Serial.println("#S|SPXL|[]#");        // start EXCEL
    pId= atoi(serInString);  
             // convert result to integer 
} 
 
void loop() 
{ 
   long durationA, inchesA, cmA;
   long durationB, inchesB, cmB;


char buffer[5];

   pinMode(pingPinA, OUTPUT);
  digitalWrite(pingPinA, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPinA, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPinA, LOW);
  
    pinMode(pingPinB, OUTPUT);
  digitalWrite(pingPinB, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPinB, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPinB, LOW);


 
  
  //pinMode(pingPinB, INPUT);
  //durationB = pulseIn(pingPinB, HIGH, 2000);

  
   if (readPingInA()){
   pinMode(pingPinA, INPUT);
 durationA = pulseIn(pingPinA, HIGH, 1000);
  inchesA = microsecondsToInches(durationA);
  cmA = microsecondsToCentimeters(durationA);
 }
 //if sensor reading does not indicate a breach, AND the breach is not unique (or rather, was unique) - print B
 else if(!readPingInA()){
  pinMode(pingPinB, INPUT);
  durationB = pulseIn(pingPinB, HIGH, 1500);
  inchesB = microsecondsToInches(durationB);
  cmB = microsecondsToCentimeters(durationB);
 }
  
  
  
  
  
  
  if(cmA>=1 && inchesA<=5 &&unique )
  {
  Serial.print("#S|SENDK|[");
     Serial.print(itoa((pId), buffer, 10));
     Serial.print("&");
     Serial.print("a");
     Serial.println("]#");
  Serial.println("#S|DLDATA|[]#");
  unique=false;
  
} 

else if (inchesA>5 &&!unique)
 {
  Serial.print("#S|SENDK|[");
     Serial.print(itoa((pId), buffer, 10));
     Serial.print("&");
     Serial.print("b");
     Serial.println("]#");
  Serial.println("#S|DLDATA|[]#");
  unique=true;
   //pinMode(pingPinB, INPUT);
  //durationB = pulseIn(pingPinB, HIGH, 2000);
  
} 


 

if(cmB>=1 && inchesB<=5 &&unique )
  {
  Serial.print("#S|SENDK|[");
     Serial.print(itoa((pId), buffer, 10));
     Serial.print("&");
     Serial.print("a");
     Serial.println("]#");
  Serial.println("#S|DLDATA|[]#");
  unique=false;
  
} 
else if (inchesB>5 &&!unique)
 {
  Serial.print("#S|SENDK|[");
     Serial.print(itoa((pId), buffer, 10));
     Serial.print("&");
     Serial.print("b");
     Serial.println("]#");
  Serial.println("#S|DLDATA|[]#");
  unique=true;
  

}
 delay(100);
}


long microsecondsToInches(long microseconds)
{

  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{

  return microseconds / 29 / 2;
}


void readSerialString (char *strArray,long timeOut) 
{
   long startTime=millis();
   int i;

   while (!Serial.available()) {
      if (millis()-startTime >= timeOut) {
         return;
      }
   }
   while (Serial.available() && i < serInLen) {
      strArray[i] = Serial.read();
      i++;
   }
}


boolean readPingInA(){
 millis()%1000 == 0 ? sensorState=!sensorState : sensorState; //simulated randomness
 return sensorState;
}

There was a thread about 9 months ago that looked at this. Basically you have to abandon pulseIn() and look at one pin then the other, while maintaining a loop count and recording the loop count when each of the pins returns, so you can time the echo from both simultaneously.