Need a way to detect if a person is walking into the room

I need a way to detect if a person is walking into the room or exiting it by placing 2 ultrasonic sensors in a straight line (well spaced) therefore I need a way to detect if sensor1 is detected High before sensor2 Any help would be appreciated.

Save the value of millis() when a sensor becomes activated, and compare.

Always use unsigned long variables with millis() and micros().

Sorry but I didn't understand what you meant here

unsigned long entry_time=millis();

If you don't know what "unsigned long" means, then you need to review variable types in C/C++. It is very important that you understand the different types, and use them correctly. Otherwise your programs will not work as expected.

will do
and I'm also kinda stuck here (I'm really new to programming)

  int dis = sonar.ping_cm();
  if (dis <= 50){
    unsigned long entry_time = millis();
    if (entery_time >)
  }

kindly guide

could you assist

Something like that?

int dist_wall=50;
void setup(){
//the usual setup
}
void loop(){
int dis1= sonar1.ping_cm();
int dis2= sonar2.ping_cm();
unsigned long entry_time=millis();

Serial.print("At t=");
Serial.print(entry_time);
if(dis1<dist_wall){
Serial.println("detected on 1");
}else{
Serial.println("not detected on 1");
}
//same for sonar2
}

Kinda works if the distance between the two sonars is not on the mm scale (dont know the acquisition time for sonar_cm())

Don't forget to add an error margin for the distance I don't know how accurate ur sonar is but better be cautious.
Best of luck

Read about millis() in the handy on-line Arduino Reference guide:

If you subtract the entry times for two sensors, you get the time it took for the person to pass between them. Compare the times to determine the direction of travel.

However, it is extremely important that you sense when the sensor becomes triggered, not if it is triggered. Study the Arduino State Change Detection example to learn how to do that.

hi appreciate the response but I am looking for the direction of motion here

@jremington how would we declare the entry time for the second sensor?

The same approach works with both sensors.

When the sensor becomes activated, save the value of millis(), using one unsigned long variable per sensor.

@jremington hey then would this be right

void loop() {
  int dis = sonar.ping_cm();
  int diss = sonar2.ping_cm();
  if (dis <= 50){
    if (diss <= 50){
      unsigned long exit_time = millis();
    }
    unsigned long entry_time = millis();
    if (entery_time < exit_time){
      //detected coming in
    }
        if (entery_time < exit_time){
      //detected going out
    }
  }
}

?

No, it won't work due to this :

if (dis<=50){
if(diss<=50){
//your code
}
}

That implies that if your first sonar does not detect someone you will not save the time for the second sonar in case of another direction and you will always detect someone entering but never going out (even if he is).
Nevermind exit_time will not even be defined in a going out case

Be very careful with spelling and capitalization. "entry" and "entery" are different variables.

Why not use quadrature logic?  When sensor1 is blocked check sensor2.  if sensor2 is blocked isExiting else isEntering.

why do you need 2? wouldn't sequential measurements indicate whether a person is moving close or farther, into or out of the room?

so what do you mean as the last line was confusing

didn't notice :joy: ,else everything is fine??

try dis=48 and diss=54
Then go through your code with those values you should see the first issue.
Also try dis<=50 after diss<=50 you will see the second one