Go Down

Topic: Two ping as a door counter (Read 1 time) previous topic - next topic

ED73

Hi guys , I am new to arduino and ping

so I need a little help, I want to count the people going in and out of the room, I wanted to do that buy 2 ping sensor. I am able to detect the distance from the 2 ping using this code that I have found here but edited it a little bit
Code: [Select]
int ultraSoundSignalPins[] = {8,7}; // Front Left,Front, Front Right, Rear Ultrasound signal pins
char *pingString[] = {"1st ","2nd "}; // just something to print to indicate direction
int j;
int ledpin = 2;


void setup()
{
  Serial.begin(9600);
 
  pinMode(ledpin = 13, OUTPUT);
}

//Ping function
unsigned long ping(int index)
{
  unsigned long echo;

  pinMode(ultraSoundSignalPins[index], OUTPUT); // Switch signalpin to output
  digitalWrite(ultraSoundSignalPins[index], LOW); // Send low pulse
  delayMicroseconds(2); // Wait for 2 microseconds
  digitalWrite(ultraSoundSignalPins[index], HIGH); // Send high pulse
  delayMicroseconds(5); // Wait for 5 microseconds
  digitalWrite(ultraSoundSignalPins[index], LOW); // Holdoff
  pinMode(ultraSoundSignalPins[index], INPUT); // Switch signalpin to input
  digitalWrite(ultraSoundSignalPins[index], HIGH); // Turn on pullup resistor
  echo = pulseIn(ultraSoundSignalPins[index], HIGH); //Listen for echo
  return (echo / 58.138) ; //convert to CM then to inches
}

void loop()
{
  unsigned long ultrasoundValue;
  for(int i=0; i < 2; i++){
    ultrasoundValue = ping(i);
    Serial.print(pingString[i]);
    Serial.print(ultrasoundValue);
    Serial.print("cm, ");   
    delay(50);
   

 

}

 
 
  Serial.println();
  delay(50);
}


and I was trying to detect the persone coming in or out using this condition assuming my constant distance is 7
Code: [Select]
if(ping(1)!=7 && ping(2)==7){
    j--;
    //digitalWrite(ledpin = 13, LOW);
  }
  if(ping(1)==7 && ping(2)!=7){
    j++;
    //digitalWrite(ledpin = 13, LOW);
  }
  Serial.println(j);


but only one of the 2 condition is entered ( that means that for example only if ping(2) is not equal 7 ) when ping(1) not equal 7 nothing happens

So can any one please help me with my code or even provide me with a better one

Thank you so much :)

AWOL

Quote
if(ping(1)!=7 && ping(2)==7){

A little optimistic to expect exact ranges, especially at such short distances.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

ED73

I monitor the values through the serial port and I notice when it changes from 7  but the problem as stated is when ping(1) changes the j-- isn't executed but when ping(2) changes j++ is executed
Thank you

willnue

#3
Jun 13, 2011, 06:46 am Last Edit: Jun 13, 2011, 06:49 am by willnue Reason: 1
It should be doing what you want, but since it isn't I am thinking the value of ping(2) is not equal to 7 when ping(1) changes. I would suggest trying the following code to give you more insight into what is happening. Also, this code reads the sensor once then compares the output, your code was reading it twice (once per comparison) which could also be what is throwing off your expectations and reading the sensors once and comparing the output twice will be faster than reading it twice...

Note: don't forget to add declarations for distanceA and distanceB to the top of your sketch. ie:
Code: [Select]
unsigned long distanceA,distanceB
Try this:
Code: [Select]

//Read the sensors
distanceA = ping(1)
distanceB = ping(2)

//Display the Readings (debug)
Serial.print("distanceA: ");
Serial.println(distanceA);
Serial.print("distanceB: ");
Serial.println(distanceB);

//Compare the output
if(distanceA != 7 && distanceB == 7){
   j--;
   //digitalWrite(ledpin = 13, LOW);
 }
if(distanceA == 7 && distanceB != 7){
    j++;
    //digitalWrite(ledpin = 13, LOW);
 }
Serial.println(j);


willnue

ED73

I knew where the problem is ... the problem was that I should recall the ping method from index 0 ... thats mean ping(0) and ping(1)
but I figured that the ultrasonic isn't accurate, it didn't return the exact right result ... sometimes it increments the j index and sometimes not ... i donno why :S

willnue

Try using the code snippet I posted making sure to update it for ping(0) and ping(1) and it should show you exactly what the values are for each reading of the sensor.

willnue

ED73

thx willnue
I have already tried it, when the reading changes than 7 the j++ or j-- but the problem now is sometimes the values changes and the j doesn't change :(

Go Up