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.
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?
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!
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!