People counter question

Dear all,

I would like you to ask you for a support on my project. It seems that I am missing something and could't understand what is it. Basically it is supposed to be a people counter using ultrasound sensors (or another type). If the sensors are being triggered in the direction 1---->2 That will sense people coming in and 2---->1 people leaving. The code that I wrote bellow kind of works but is easily getting confused of the direction and swaps leaving with incoming. Any suggestions? Thanks a lot in advance!

  digitalWrite(trigPin2, LOW);  
  delayMicroseconds(2); 
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin2, LOW);
  duration = pulseIn(echoPin2, HIGH);
  distance = (duration/2) / 29.1;
  
  digitalWrite(trigPin1, LOW);  
  delayMicroseconds(2); 
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin1, LOW);
  duration2 = pulseIn(echoPin1, HIGH);
  distance2 = (duration2/2) / 29.1;
  
  
  


  //ULTRASONIC DEFINITIONS
  ////////////////////////////////////////////////////////////////////////////////////////
  
  if (distance2 < 50)
  {
   mili1=millis(); 
  }
  
 if (distance < 50 )

 {
   mili2=millis();
 }

  
  
  if (distance2 < 50)
  
  {
   
    if (mili2 < mili1)
    {
      
      count++;
      Serial.println("People in the room:");
      Serial.print(count);
      Serial.println("");
       if (count < 0) count =0;
      
  
    }
    
    writeToSQL();
      delay(2000);
  }
  
 if (distance < 50)
  
  {

  if (mili2 > mili1)
    {
      
      count--;
     Serial.println("People in the room:");
     Serial.print(count);
     Serial.println("");
     if (count < 0) count =0;
     
   }
     writeToSQL();
      delay(2000);
    
  } 
  

  mili1=0;
  mili2=0;
  
  
  

}

You really need to document your use of millis(). It's kind of bizarre, and there are no comments in the code. Actually, I can't figure out why millis is mixed up in calculations with the distance variables. Also your time stamps are not always set, because they are in "if" statements. It's all very confusing.

If it works but the direction is reversed, why not just invert the meaning of the outputs? So that in is out and out is in, right?

Dear aarg it works but it confuses directions sometimes (instead of +1 it counts -1). I have added comments to the code bellow and was wondering how to save the value of millis() when a sensor was triggered? If you have time could you take a look and suggest what can be changed? Thanks!

  if (distance2 < 50)
  {
   mili1=millis();  //if the first sensor is triggered set timestamp 1 to current time
  }
  
 if (distance < 50 )

 {
   mili2=millis(); //if the first sensor is triggered set timestamp 1 to current time
 }

  
  //If sensor 1 triggered and time stamp 1 is smaller than timestamp 2 --> count + 1
  if (distance2 < 50)
  
  {
   
    if (mili1 < mili2)  
    {
       count++;
         }
  }
  //  //If sensor 2 triggered and time stamp 2 is smaller than timestamp 1 --> count - 1
 if (distance < 50)
  
  {

  if (mili2 < mili1)
    {   
      count--;
   }
  } 
  
 //reset timestamps
  mili1=0;
  mili2=0;

My guess on "sometimes getting confused" has to do with placement of the sensors (making sure it sees things correctly) as well as when people walk, their arms and legs tend to swing back and forth - you may be getting false readings from arms swinging back for example when someone is walking towards the sensor). I think the only way to get decent counts is to have a narrow doorway for example where only one person can go through at a time, but then you also have to work the issue of someone goes out, someone comes in and steps out of the path of the sensor and the sensor then sees the person that just went out again and counts that twice. Probably why most of the automatic lights etc. I see for places respond to motion in the room rather than trying to keep track of the count in/out.

Hi Mikey,

That was the first thing to check - if sensors are working correctly and are at appropriate distance from each other and there is enough reaction time. They are both reacting fine that leads me it is an issue with the code above (me not written it properly).

Rather than use ultrasonics have you considered an infrared beam being broken?

Yes, this would require both a transmitter and receiver although they could be on the same physical board with a mirror that the beam bounces off. This would be far more reliable than ultrasonics I would have thought. It’s certainly what they use in factories to count items.

I still don’t get the ‘direction’ requirement though. How can any solution track the direction of a person, given that people may pass each other as they beam is broken? Think of a barrier-controlled car park: each car must pass through a single point of entry or exit to count the number of cars (and parking bays empty) in the car park.

What sort of doorway are you talking about? Big conference room? Your bedroom?

Here is how to change a sign:

var = (-var);

Problem solved.

Ralph_S_Bacon: Rather than use ultrasonics have you considered an infrared beam being broken?

Yes, this would require both a transmitter and receiver although they could be on the same physical board with a mirror that the beam bounces off. This would be far more reliable than ultrasonics I would have thought. It's certainly what they use in factories to count items.

I still don't get the 'direction' requirement though. How can any solution track the direction of a person, given that people may pass each other as they beam is broken? Think of a barrier-controlled car park: each car must pass through a single point of entry or exit to count the number of cars (and parking bays empty) in the car park.

What sort of doorway are you talking about? Big conference room? Your bedroom?

Dear Ralph,

I am more focusing on getting the code working while assuming that the walkway is narrow enough to let only 1 person at a time. The direction is required to add and subtract to the total number of people in the room so it is known how many people are attending the room. I know that infrared beam sensors are better for the purposes but I have these to hand and they are fast enough for the moment.

Okay, so can you mark the thread as "solved"? You said it worked, except for the sign of the direction, and I gave you the solution for that.

Alternatively, you could swap sensor inputs. Perhaps you already have them hooked up backwards. Or just swap the actual sensor positions.