Ultrasonic sensor Person counter

Hello all,

I am currently trying to make a People counter for entering through a door. It is working but I'm trying to alter the code so that when somebody stops in front of the sensor it stops counting until they move away. I know that it is possible to change the delay but I'd like it to be as accurate as possible.

Trying to describe what I'm trying to achieve as simple as possible:

Person enters room
-Sensor Adds 1-
-Sensor waits until they're out the way-

Another person enters room
-Sensor Adds 1-
-Sensor waits until they're out the way-
etc.

This is the code I am currently using:

 #include <LiquidCrystal.h>
 #define trigPin 13
 #define echoPin 8
 // initialize the library with the numbers of the interface pins
 LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 int counter = 0;
 int currentState = 0;
 int previousState = 0;
 
 void setup() {
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 lcd.begin(16, 2);
 lcd.setCursor(0, 0);
 lcd.print("People Counter!");
 }
 
 void loop() {
 long duration, distance;
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 distance = (duration/2) / 29.1;
 
 if (distance <= 100){
 currentState = 1;
 }
 else {
 currentState = 0;
 }
 
 delay(200);
 if(currentState != previousState){
 if(currentState == 1){
 counter = counter + 1;
 lcd.print(counter);
 lcd.setCursor(0,1);
 delay(1000);
 }
 }
 }

I've got the 16 pin LCD screen and Ping Ultrasonic sensors which come with the Arduino starter kit.

Any advice would be much appreciated.

-Sensor waits until they're out the way-

Right there is your problem. The sensor can not possibly do that.

The Arduino could ignore the sensor for a while, but that would not make sense.

The Arduino could do something when the distance drops below some threshold, and something else when the distance rises above the threshold.

There is no reason to delay() AT ALL in your code.

The NewPing library is a better way of reading ultrasonic sensors. It does not contain any blocking code.

You never re-value previousState, so it's hardly a useful "variable".

Thanks for the reply,

I have previously tried using the 'While' function to add 1 when the sensor is closer than 100cm with no luck.
Do you think this is something which could be possible to incorporate into the code or would it still not be possible?

gingerheaddan:

 delay(200);

if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
lcd.print(counter);
lcd.setCursor(0,1);
delay(1000);
}
}
}

Hi there!

I like your approach for determining if the person is still there, however there is a small logic error that will not allow the code to function as you would like. Something like the following code may help:

 delay(200);
 if(currentState == 1 && previousState == 0)
 {
   counter = counter + 1;
   lcd.setCursor(0,1);
   lcd.print(counter);
   delay(1000);
 }
 }
previousState = currentState;
 }

//Also put "previousState = 0;" in your setup function

Essentially, I added a statement so that the variable "previousState" is updated each time the sensor is read. I also switched the order of the two lcd commands so that the cursor will be set to (0,1) before it writes the number, just in case you want the "People Counter!" to stay on the first line.

Try replacing your segment of code with the one I wrote and see what happens!

Thank you!
I've spent so long trying to alter different parts of the code but this seems to have worked :slight_smile:

No problem. When you have a programming error, it almost always helps to walk away for a few minutes, and come back with fresh eyes. Nevertheless, I'm glad to have helped you. Enjoy the rest of your day!