Help, to count people in home using proximity sensors

im doing my project in arduino, here im struggling to make arduino code to count the people..
my concept is by using proximity sensors,

if one crosses proximity sensor 1 followed by proximity sensor 2, i want to show one person is in home, if the same happens again, i want person count to be incremented,

if one crosses proximity sensor 2 followed by proximity sensor 1, i want person count to be decremented,

i need person count equals 0 in initial state because i use person count in few areas in my project.

my current status is,
now im using dip switch for this function,
i said when dip is OFF there are no one present in home, when dip is ON one or more people are present in home.
please help me in coding...
thanks in advance regards Jay

Start the coding by reading both sensors and printing the readings.

What type of sensor are you going to use?

You need one that can detect two people close together. This leaves out IR burglar alarm types and sonar as they both have wide detectin area. The IR type could possibly be used if there was a baffle arrangement to limit the detection angle.

Your best way would probably be to break an IR beam. The two sensors would need to be fairly close together to detect the first person going through the two sensors before a second person enters.

You may also need to restrict the path so only one person can go in or out at a time.

Weedpharma

I think to get an accurate count you'll have to take care on how you place the sensors, control the people into a line, and code accordingly.

If you assume the sensor is real time and 'dumb', so it's just high for as long as there is an obstruction in it's range, and if we assume it's one person at a time, and they're not waving their arms around in front of the sensor, so the signal is clean.

If we also assume the sensors are close together, and there is overlap in their signal when someone passes, meaning the second sensor goes high before the first sensor drops to low again, then we can use the same principle for code as reading a rotary encoder. Have a look at example 1 on this page:

http://playground.arduino.cc/Main/RotaryEncoders#Example1

But this might not be very reliable if there are false readings because of breaks in the beam.

In this case you could place them further apart so their signal doesn't overlap, and set flags when they go off and compare these flags and reset them when the event is complete and we have our count update.

So I think something like this would work (pseudo-code),

digitalRead sensorA
digitalRead sensorB

if (sensorA == high) {
sensorAFlaggedHigh=true; // we set a boole flag, so it remains high even when person has passed A
}

if (sensorB == high) {
sensorBFlaggedHigh=true; // we set a boole flag, so it remains high even when person has passed B
}

if (sensorB == high && sensorAFlaggedHigh == true) {
peopleInTheHouse++;
sensorAFlaggedHigh=false; // Resetting sensor A's flag - we captured our event.
}

if (sensorA == high && sensorBFlaggedHigh == true) {
peopleInTheHouse--;
sensorBFlaggedHigh=false; //Resetting sensor B's flag - we captured our event.
}

So I think this would work as long as the sensor triggers don't overlap.

Using a camera overhead and OpenCV would be cool for this, and would be able to track multiple people at the same time, and would be pretty hard to confuse (unless it crashes!)