using 2 ping))) sensors to count cars coming and going

Hello, Im working on a project where I want to count cars coming in and out of a parking area.

I have modified the example ping code and used the ping distance as a way to count up or down, the problem i am running into is that when I run them at the same time the counter only counts down

here is my code so far.

/* Ping))) Sensor
  
   This sketch reads a PING))) ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse 
   to return.  The length of the returning pulse is proportional to 
   the distance of the object from the sensor.
     
   The circuit:
	* +V connection of the PING))) attached to +5V
	* GND connection of the PING))) attached to ground
	* SIG connection of the PING))) attached to digital pin 7

   http://www.arduino.cc/en/Tutorial/Ping
   
   created 3 Nov 2008
   by David A. Mellis
   modified 30 Aug 2011
   by Tom Igoe
 
   This example code is in the public domain.

 */

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;
const int pingPin2 = 8;
int count = 6;

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, inches, cm;
  long duration2, inches2, 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);
  
  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(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  
  pinMode(pingPin2, INPUT);
  duration2 = pulseIn(pingPin2, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  inches2 = microsecondsToInches(duration2);
  cm = microsecondsToCentimeters(duration2);
  
  if(inches < 20)
  { 
    count++;
  }
  
  else(inches2 < 20);
  {
  count--;
  }
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  Serial.print("Count");
  Serial.println(count);
  Serial.print(inches2);
  Serial.print("in, ");
  Serial.print(cm2);
  Serial.print("cm");
  Serial.println();
  Serial.print("Count");
  Serial.println(count);
  delay(500);
}

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

Hi btw

It looks like you ping one sensor, ping the second sensor, then listen for the echo on the first sensor, then listen for the echo on the second sensor.

What happens if you change the order: ping one, listen one, ping two, listen two?

All the best

Ray

It looks like you ping one sensor, ping the second sensor, then listen for the echo on the first sensor, then listen for the echo on the second sensor.

The sensors can't discriminate the source of an echo, so the method is probably flawed.

could the sensors transmit different frequencies?

Nope.

I have two HC-SR04 sensors working close together on the same frequency. Using the NewPing library, admittedly, but I think the principles apply.

Same frequency is not a problem so long as you allow enough time for the echo from one ping to arrive (if it is going to) before starting the next ping. In the NewPing documentation, there is guidance on the timing on this.

In your application, I had inferred (maybe wrongly) that there was an in lane to the car park and an out lane, and your sensors were positioned to bounce of cars going through each lane. In that case, crossover echos may not be a problem, and if you sequence the pings correctly, won't be a problem.

So, as a start, how about switching the order of your code around as per my earlier post, putting a few seconds delay for debugging purposes between the two blocks of code (sensor 1 ping / echo and sensor 2 ping / echo) and then testing to see if you are detecting cars and if the counter arithmetic is OK?

Cheers

Ray

Hi btw

I've looked at the code you posted again.

The counters are in this part ...

  if(inches < 20)
  { 
    count++;
  }
  
  else(inches2 < 20);
  {
  count--;
  }

First thing I spot in this is the ";" after "(inches2 < 20)". I think this makes the code after it, the curly brackets with "count--", a statement on its own outside the if / else. In which case, maybe it is being executed every time round the loop, which is why the counter is only counting down.

Was the following what you meant it to be?

  if (inches < 20)
  { 
     count++;
  }
  else if (inches2 < 20)
  {
    count--;
  }

If this works, then there is another thing to think about. Your main loop repeats after a 500ms delay. So roughly twice a second you increase or decrease the counters depending on whether you get echoes. But won't a car take longer to pass the sensor than half a second? I think you code will keep counting so long as the car is there. Or have I misunderstood?

Regards

Ray