COde for IR bidirectional counting using Arduino Mega

hello everone, i am trying to build a project on bradboard. objective is to count person entering and leaving a room using IR sensor and Arduino Mega. Can anyone help me with the code?

What code?

this is what i am using. which apparently has some problem as it counts only something is constantly hold infront of the sensor.

#include<LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);
#define in A0
#define out A1

int count=0;
void IN()
{
count++;
lcd.clear();
lcd.print(“Person In Room:”);
lcd.setCursor(0,2);
lcd.print(count);
delay(1000);
}
void OUT()
{
count–;
lcd.clear();
lcd.print(“Person In Room:”);
lcd.setCursor(0,2);
lcd.print(count);
delay(1000);
}
void setup()
{
lcd.begin(16,2);
lcd.clear();
lcd.print(“Visitor Counter”);
lcd.setCursor(0,2);
delay(2000);
pinMode(in, INPUT);
pinMode(out, INPUT);

lcd.clear();
lcd.print(“Person In Room:”);
lcd.setCursor(0,2);
lcd.print(count);
}
void loop()
{

if(digitalRead(in))
IN();
if(digitalRead(out))
OUT();

if(count<=0)
{

lcd.clear();
lcd.print(“Nobody In Room”);
lcd.setCursor(0,1);

delay(2000);
}
}

  if(digitalRead(in))
  IN();
  if(digitalRead(out))
  OUT();

It seems you have two break beam sensors.

They will count up resp. down very fast when reading HIGH (is that beam made or beam broken?). You don't do state checking (you have to count on CHANGE of the signal). Furthermore this implies there's one route for in, and another one for out.

thank you. actually what i was trying to do is to up count when in is break first, and down count when out is break fast. i am new in this and couldnt do any better than this. Can you please help ?

Then on top of state checking you need to keep track of the order in which they were broken, so only count when you detect the second.

And you have to account for breaking one but not the other within a certain time limit (they changed their mind and didn't continue) or you start counting the wrong way.

Still you will count wrong if people are too short (under the beam), too tall (you count two legs), or pass next to each other (you count only one).