Pages: [1]   Go Down
Author Topic: Two ping as a door counter  (Read 698 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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 smiley
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24298
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
if(ping(1)!=7 && ping(2)==7){
A little optimistic to expect exact ranges, especially at such short distances.
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 90
I’m no expert, but I play one on the Internet…
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
unsigned long distanceA,distanceB

Try this:
Code:
//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
« Last Edit: June 12, 2011, 11:49:05 pm by willnue » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 90
I’m no expert, but I play one on the Internet…
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-sad
Logged

Pages: [1]   Go Up
Jump to: