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