Condtion for my counting project

hey there,
I have a project to count the people who enter the room and who get out of the room,
my code is reading the signal very good but the thing is that I need condition when the first sensor is TRUE the second one should be off, or when the first sensor is TRUE the program should wait till the other sensor also store the signal and then it should add one,
In this way, I can count people who get IN and OUT on the one way.


  
  if (distance_in < calibrate_in && distance_in > 0) { // If closer than wall/calibrated object (person is present) && throw out zero readings

      
    if (prev_inblocked == false) {
     
      
      count++; // Increase count by one
      Serial.print("Count: ");
      Serial.println(count);
      delay (100);
      }
      
    prev_inblocked = true;
  } else {
    prev_inblocked = false;
  }
  if (distance_out < calibrate_out && distance_out > 0) {

    
    if (prev_outblocked == false) {
      count--; // Decrease count by one
      Serial.print("Count: ");
      Serial.println(count);
      delay (100);
    }
    prev_outblocked = true;
  } else {
    prev_outblocked = false;
  }

You’ve got four possible sensor states to contend with…

  • Both sensors open
  • In sensor closed
  • Out sensor closed
  • Both sensors closed
    *below

Based on the transition from any state to another, you have to determine what activity is taking place.

Using a simple state-machine acting upon the current and preceding states, you can figure out what’s happening.

I assume that if someone is halfway between the sensors, they are in the ‘middle’, otherwise you may have confusion between two people entering the zone simultaneously.

*That may be a good argument for an additional sensor in-between the two existing…
That would raise your number of ‘states’ to eight, but also give you more awareness of what’s happening.

1 Like

for now, the code work if there is two way, in different sides, the only thing I want is to have a condition that when the first sensor detects, the sensor should wait till the other sensor detect too

Sounds similar to quadrature encoding.

1 Like

can you explain your sensors, are there separate enter/exit paths each with a distance sensor, or is there a single enter/exit path? don't understand your need to make one dependent on the other.

wouldn't a distance sensor normally report some farthest distance (or timeout), so when someine enter/exits, the distance report changes value from which you can determine a direction and then increment a count when the distance becomes farthest or timeout

(what happens if a person turns around)?

1 Like

it's one path, for IN and OUT, basically, I have to ultrasonic, which read signal in (cm) I put both of the sensors beside together, one sensor for IN and other for OUT, when the first sensor distance go less than 30 cm it adds one+ to count, and when the other sensor also go less than 30 cm it takes on (-1) form the count,
and now when someone enters first it adds one and then when he passed into the other sensor too it takes one,
I can put delay(); when the first sensor detects but I want other ways.

i don't understand why you need 2 "distance" detectors if there is just one path? are the perpendicular to the path or parallel (in line)

a common approach for counting people is to have two optical detectors perpendicular to the path that a person breaks. the detector the becomes active last indicates the direction

1 Like

can you please explain more about your idea,
because first when I thought about count I said that, if it's one path it's and I put one ultrasonic how I would know that each detection is OUT or IN, so I plan to buy two ultrasonic,
but still it works ^_^, but the problem is with the code

maybe i don't understand. are you using an ultrasonic sensor shown below.

where are the located along your enter/exit path and which direction do they face?

1 Like

yes, I use this sensor, they are next to each other,

the picture is for example.

do the face across the path broken by the person enter/exiting?

yes i point them to the path, who enter and who get out,

what direction is "to"?
across the path?

i know you are trying to count the # of people entering and exiting.
you said this already
you said this already
please don't repeat your question .... it wastes time

1 Like

okay,

both ultrasonic set next to each other and I hung them on the wall that allowed them to read the signal

which way do they point? across the path or along (in line) the path

draw picture

1 Like

alright
25574710

so they are pointed across the path

presumably you are repeatedly making a distance measurement with each sensor and you expect one sensor to detect a person (< 30cm) before the other sensor

and presumably, as I suggested, the sensor the detects a person last indicates the direction.

but the ultrasonic sensors have a 15 deg aperture. this means that it senses something 5cm to its side at 10cm from it and 10 cm to the side at 20 cm

      
    10 \        /
        \      /
       5 \    /
          \  /
            o

so wouldn't each sensor detect the person at the same time of they are very close to one another?

it would be better to put the sensors at each end of you enter/exit box so that they are as far apart as possible

in the one direction the sensors should report

AAAAAA
   BBBBBB
-----------> time

and in the oppositer direction

   AAAAAA
BBBBBB
-----------> time
1 Like

I already put the sensor far away from each other and they don't effect on each other,

also, I get the time explanation, but the thing is how I'm gonna do it on the code,
is there any library that I can use, ?

i guess you didn't understand @lastchancename's suggestion for using a state machine?

_ -> A -> AB -> B -> _ IN
_ -> B -> AB -> A -> _ OUT

1 Like

yes i get you, but how to store the time that the first sensor detect and compare it with the other sensor time