How to create a VISITOR counter using E18D80NK X2

Hi I really need help how to create code for this type of project I want to change the IR module with 2PCS of E18-D80NK

I wanted to DISPLAY on screen HOW MANY PERSON is in the room and DECREASE the number if the person goes out.

I have this code I dont know what is wrong.

#include<LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);

int ir_right_pin = 6;
int ir_left_pin = 7;

int ir_right_state = 0;
int ir_left_state  = 0;

int ir_right_state_last = -1;
int ir_left_state_last  = -1;

int in_counter = 0;
int out_counter = 0;

bool bWalkIn = false;
bool bWalkOut = false;
unsigned long tm;

void IN()
{
    count++;
    lcd.clear();
    lcd.print("Person In Room:");
    lcd.setCursor(0,1);
    lcd.print(count);
    delay(1000);
}

void OUT()
{
  count--;
    lcd.clear();
    lcd.print("Person In Room:");
    lcd.setCursor(0,1);
    lcd.print(count);
    delay(1000);
}

void setup()
{
  lcd.begin(16,2);
  lcd.print("Visitor Counter");
  delay(2000);
  pinMode(in, INPUT);
  pinMode(out, INPUT);
  pinMode(relay, OUTPUT);
  lcd.clear();
  lcd.print("Person In Room:");
  lcd.setCursor(0,1);
  lcd.print(count);
}

void loop()
{  
  
  if(digitalRead(in))
  IN();
  if(digitalRead(out))
  OUT();
  
  if(count<=0)
  {
    lcd.clear();
    digitalWrite(relay, LOW);
    lcd.clear();
    lcd.print("Nobody In Room");
    lcd.setCursor(0,1);
    lcd.print("Light Is Off");
    delay(200);
  }
  
  else
    digitalWrite(relay, HIGH);
  
}

!(http://

)

Try a site search on 'room counter'. It's been done many times.

Well for a start it won’t compile.

You need to declare variables count and relay:

int count;
int relay=2;

Then instead of reading pins in and out you need to use the pins you declared ir_right_pin and ir_left_pin.

So:

pinMode(ir_right_pin, INPUT);
pinMode(ir_left_pin, INPUT);

And:

if(digitalRead(ir_right_pin))
IN();
if(digitalRead(ir_left_pin))
 OUT();

When I do that at least it compiles:

#include<LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);

int ir_right_pin = 6;
int ir_left_pin = 7;

int ir_right_state = 0;
int ir_left_state  = 0;

int ir_right_state_last = -1;
int ir_left_state_last  = -1;

int in_counter = 0;
int out_counter = 0;

bool bWalkIn = false;
bool bWalkOut = false;
unsigned long tm;

int count;
int relay=2;

void IN()
{
    count++;
    lcd.clear();
    lcd.print("Person In Room:");
    lcd.setCursor(0,1);
    lcd.print(count);
    delay(1000);
}

void OUT()
{
  count--;
    lcd.clear();
    lcd.print("Person In Room:");
    lcd.setCursor(0,1);
    lcd.print(count);
    delay(1000);
}

void setup()
{
  lcd.begin(16,2);
  lcd.print("Visitor Counter");
  delay(2000);
  pinMode(ir_right_pin, INPUT);
  pinMode(ir_left_pin, INPUT);
  pinMode(relay, OUTPUT);
  lcd.clear();
  lcd.print("Person In Room:");
  lcd.setCursor(0,1);
  lcd.print(count);
}

void loop()
{ 
 
  if(digitalRead(ir_right_pin))
  IN();
  if(digitalRead(ir_left_pin))
  OUT();
 
  if(count<=0)
  {
    lcd.clear();
    digitalWrite(relay, LOW);
    lcd.clear();
    lcd.print("Nobody In Room");
    lcd.setCursor(0,1);
    lcd.print("Light Is Off");
    delay(200);
  }
 
  else
    digitalWrite(relay, HIGH);
 
}

But then I don’t think it will actually work. Although you have variables for the current and previous states of the sensors:

int ir_right_state = 0;
int ir_left_state  = 0;

int ir_right_state_last = -1;
int ir_left_state_last  = -1;

… as far as I can see you don’t use them, so you’re not using the state change detect technique.

You’re just reading each sensor, and immediately incrementing or decremeting count, so you’ll get multiple count++ or count– while the person is in front of the beam. Even if the person keeps moving, loop() is so fast that they will be in the beam for “ages” in computer terms.

sayHovis:
Well for a start it won’t compile.

You need to declare variables count and relay:

int count;

int relay=2;




Then instead of reading pins *in* and *out* you need to use the pins you declared *ir_right_pin* and *ir_left_pin*.

So:



pinMode(ir_right_pin, INPUT);
pinMode(ir_left_pin, INPUT);




And:



if(digitalRead(ir_right_pin))
IN();
if(digitalRead(ir_left_pin))
OUT();




When I do that at least it compiles:



#include<LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);

int ir_right_pin = 6;
int ir_left_pin = 7;

int ir_right_state = 0;
int ir_left_state  = 0;

int ir_right_state_last = -1;
int ir_left_state_last  = -1;

int in_counter = 0;
int out_counter = 0;

bool bWalkIn = false;
bool bWalkOut = false;
unsigned long tm;

int count;
int relay=2;

void IN()
{
    count++;
    lcd.clear();
    lcd.print(“Person In Room:”);
    lcd.setCursor(0,1);
    lcd.print(count);
    delay(1000);
}

void OUT()
{
  count–;
    lcd.clear();
    lcd.print(“Person In Room:”);
    lcd.setCursor(0,1);
    lcd.print(count);
    delay(1000);
}

void setup()
{
  lcd.begin(16,2);
  lcd.print(“Visitor Counter”);
  delay(2000);
  pinMode(ir_right_pin, INPUT);
  pinMode(ir_left_pin, INPUT);
  pinMode(relay, OUTPUT);
  lcd.clear();
  lcd.print(“Person In Room:”);
  lcd.setCursor(0,1);
  lcd.print(count);
}

void loop()
{

if(digitalRead(ir_right_pin))
  IN();
  if(digitalRead(ir_left_pin))
  OUT();

if(count<=0)
  {
    lcd.clear();
    digitalWrite(relay, LOW);
    lcd.clear();
    lcd.print(“Nobody In Room”);
    lcd.setCursor(0,1);
    lcd.print(“Light Is Off”);
    delay(200);
  }

else
    digitalWrite(relay, HIGH);

}




**But then I don't think it will actually work.** Although you have variables for the current and previous states of the sensors:



int ir_right_state = 0;
int ir_left_state  = 0;

int ir_right_state_last = -1;
int ir_left_state_last  = -1;




... as far as I can see you don't use them, so you're not using the [state change detect technique.](https://www.arduino.cc/en/Tutorial/StateChangeDetection)

You're just reading each sensor, and immediately incrementing or decremeting *count*, so you'll get multiple *count++* or *count--* while the person is in front of the beam. Even if the person keeps moving, loop() is so fast that they will be in the beam for "ages" in computer terms.

I cant really understand the IR code sir can you please lend me some help?

kalilinux25:
can you please lend me some help?

Not right now I'm afraid.... busy getting my day going

kalilinux25:
I cant really understand the IR code

The fact that you started out by using state variables for the current and previous state, but don't understand it, says to me you probably lifted that code from somewhere, but only used part of it. Why don't you just use the rest of that code, wherever it is?

But why don't you look at the state change detect example I linked. This is not really an "ir thing" it's a "switch thing".

My ir sensor produces a low when there's something in front of it, high otherwise. So in loop() I would compare the previous value to the new one, and if there's a change, look to see if it's gone low. That means an obstruction has just happened. At that point, I would increment the count. Because we are only looking for a change to low to cause an increment (ie the sensor just became obstructed, as distinct from is obstructed), the obstruction can stay there for eternity, and it will only cause one increment not a gazillion.

#include<LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);

int ir_right_pin = 6;
int ir_left_pin = 7;

int ir_right_state = 0;
int ir_left_state  = 0;

int ir_right_state_last = -1;
int ir_left_state_last  = -1;

int in_counter = 0;
int out_counter = 0;

bool bWalkIn = false;
bool bWalkOut = false;
unsigned long tm;

int count;
int relay=2;

void IN()
{
    count++;
    lcd.clear();
    lcd.print("Person In Room:");
    lcd.setCursor(0,1);
    lcd.print(count);
    delay(1000);
}

void OUT()
{
  count--;
    lcd.clear();
    lcd.print("Person In Room:");
    lcd.setCursor(0,1);
    lcd.print(count);
    delay(1000);
}

void setup()
{
  lcd.begin(16,2);
  lcd.print("Visitor Counter");
  delay(2000);
  pinMode(ir_right_pin, INPUT);
  pinMode(ir_left_pin, INPUT);
  pinMode(relay, OUTPUT);
  lcd.clear();
  lcd.print("Person In Room:");
  lcd.setCursor(0,1);
  lcd.print(count);
}

void loop()
{
 
  if(digitalRead(ir_right_pin))
  IN();
  if(digitalRead(ir_left_pin))
  OUT();
 
  if(count<=0)
  {
    lcd.clear();
    digitalWrite(relay, LOW);
    lcd.clear();
    lcd.print("Nobody In Room");
    lcd.setCursor(0,1);
    lcd.print("Light Is Off");
    delay(200);
  }
 
  else
    digitalWrite(relay, HIGH);
 
}
void setup(void) {
  
   Serial.begin(9600);
   pinMode( ir_right_pin, INPUT);
   pinMode( ir_left_pin , INPUT);
   
  // flip screen, if required
  // u8g.setRot180();
  
  // set SPI backup if required
  //u8g.setHardwareBackup(u8g_backup_avr_spi);

  // assign default color value
  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);     // white
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);         // max intensity
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);         // pixel on
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255,255,255);
  }
  
  updateLCD();
}


void loop(void) {
  
     ir_right_state = digitalRead( ir_right_pin );
     ir_left_state =  digitalRead( ir_left_pin );

     Serial.print( ir_left_state );
     Serial.print( " " );
     Serial.println( ir_right_state );

     checkWalkIn();
     checkWalkOUT();

       
}
void checkWalkIn(){

    if( ir_right_state != ir_right_state_last ){

         ir_right_state_last = ir_right_state;
         if( (bWalkIn == false) && ( ir_right_state == LOW ) ){
              bWalkIn = true;
              tm = millis();
         }
     }

     if( (millis() - tm) > DELAY_TIMEOUT ){
          bWalkIn = false;
     }

     if( bWalkIn && (ir_left_state == LOW) && (ir_right_state == HIGH) ){
          bWalkIn = false;
          in_counter++;
          updateLCD();
     }

}
void checkWalkOUT(){

    if( ir_left_state != ir_left_state_last ){

         ir_left_state_last = ir_left_state;
         if( (bWalkOut == false) && ( ir_left_state == LOW ) ){
              bWalkOut = true;
              tm = millis();
         }
     }

     if( (millis() - tm) > DELAY_TIMEOUT ){
          bWalkOut = false;
     }

     if( bWalkOut && (ir_right_state == LOW) && (ir_left_state == HIGH) ){
          bWalkOut = false;
          out_counter++;
          updateLCD();
     }

Hi sir I tried to mix the code from IR SENSOR but it says Void error

#include "U8glib.h"

#define DELAY_TIMEOUT 1500

U8GLIB_ST7920_128X64_1X u8g(13,11, 10);


int ir_right_pin = 6;
int ir_left_pin = 7;

int ir_right_state = 0;
int ir_left_state  = 0;

int ir_right_state_last = -1;
int ir_left_state_last  = -1;

int in_counter = 0;
int out_counter = 0;

bool bWalkIn = false;
bool bWalkOut = false;
unsigned long tm;

// for display integer value
enum {BufSize=8}; // If a is short use a smaller number, eg 5 or 6 
char buf[BufSize];


void draw(void) {
  // graphic commands to redraw the complete screen should be placed here  
  u8g.setFont(u8g_font_fub20);
  u8g.drawStr( 10, 25, "IN");
  u8g.drawStr( 65, 25, "OUT");
  
  u8g.setFont(u8g_font_timB24);

  if( in_counter < 10){
      snprintf (buf, BufSize, "00%d", in_counter);
  }
  else if( in_counter < 100){
      snprintf (buf, BufSize, "0%d", in_counter);
  }

  u8g.drawStr( 5, 60, buf);

  if( out_counter < 10){
      snprintf (buf, BufSize, "00%d", out_counter);
  }
  else if( out_counter < 100){
      snprintf (buf, BufSize, "0%d", out_counter);
  }
  
  
  u8g.drawStr( 70, 60, buf);

  u8g.drawLine(60,0,60,63);
}

void setup(void) {
  
   Serial.begin(9600);
   pinMode( ir_right_pin, INPUT);
   pinMode( ir_left_pin , INPUT);
   
  // flip screen, if required
  // u8g.setRot180();
  
  // set SPI backup if required
  //u8g.setHardwareBackup(u8g_backup_avr_spi);

  // assign default color value
  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);     // white
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);         // max intensity
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);         // pixel on
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255,255,255);
  }
  
  updateLCD();
}

void loop(void) {
  
     ir_right_state = digitalRead( ir_right_pin );
     ir_left_state =  digitalRead( ir_left_pin );

     Serial.print( ir_left_state );
     Serial.print( " " );
     Serial.println( ir_right_state );

     checkWalkIn();
     checkWalkOUT();

       
}
void checkWalkIn(){

    if( ir_right_state != ir_right_state_last ){

         ir_right_state_last = ir_right_state;
         if( (bWalkIn == false) && ( ir_right_state == LOW ) ){
              bWalkIn = true;
              tm = millis();
         }
     }

     if( (millis() - tm) > DELAY_TIMEOUT ){
          bWalkIn = false;
     }

     if( bWalkIn && (ir_left_state == LOW) && (ir_right_state == HIGH) ){
          bWalkIn = false;
          in_counter++;
          updateLCD();
     }

}
void checkWalkOUT(){

    if( ir_left_state != ir_left_state_last ){

         ir_left_state_last = ir_left_state;
         if( (bWalkOut == false) && ( ir_left_state == LOW ) ){
              bWalkOut = true;
              tm = millis();
         }
     }

     if( (millis() - tm) > DELAY_TIMEOUT ){
          bWalkOut = false;
     }

     if( bWalkOut && (ir_right_state == LOW) && (ir_left_state == HIGH) ){
          bWalkOut = false;
          out_counter++;
          updateLCD();
     }

     
}
void updateLCD(){
   u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );
}

THIS COde he uses 2pcs of E180D80NK AND LCD 128X64

But I only have 16X2 lcd display

How can I change the first code and mix with this code

//Visitors Counter and Automatic Room Light
//Viral Science  www.youtube.com/c/viralscience

#include<LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);

#define in 14
#define out 19
#define relay 2

int count=0;

void IN()
{
    count++;
    lcd.clear();
    lcd.print("Person In Room:");
    lcd.setCursor(0,1);
    lcd.print(count);
    delay(1000);
}

void OUT()
{
  count--;
    lcd.clear();
    lcd.print("Person In Room:");
    lcd.setCursor(0,1);
    lcd.print(count);
    delay(1000);
}

void setup()
{
  lcd.begin(16,2);
  lcd.print("Visitor Counter");
  delay(2000);
  pinMode(in, INPUT);
  pinMode(out, INPUT);
  pinMode(relay, OUTPUT);
  lcd.clear();
  lcd.print("Person In Room:");
  lcd.setCursor(0,1);
  lcd.print(count);
}

void loop()
{  
  
  if(digitalRead(in))
  IN();
  if(digitalRead(out))
  OUT();
  
  if(count<=0)
  {
    lcd.clear();
    digitalWrite(relay, LOW);
    lcd.clear();
    lcd.print("Nobody In Room");
    lcd.setCursor(0,1);
    lcd.print("Light Is Off");
    delay(200);
  }
  
  else
    digitalWrite(relay, HIGH);
  
}

how can I mixed this code

There is absolutely no point trying to “mix” your ir detection sketch with an lcd sketch until you get the ir part working and at least displaying the count to the monitor.

You are clearly just picking up bits of code here and there and hoping for the best.

I don’t have time to help, but have a look at the code below. It’s a template I keep based on the state change detect example I linked earlier, adjusted for a button that goes low when pressed. I just tested it with my ir sensor which goes low when obstructed. I added a count to show that it only increments when it becomes obstructed, then it also tells you when the obstruction clears.

Unless you understand the logic in this sketch, you are wasting your time* trying to get a counter to work. (And even more so if you try to “mix” different sketches without understanding how one of them works in the first place.)

My advice fwiw… use code like below (adjusted for your ir pin and if your sensor gives a high or low when obstructed) and get it to work by actually understanding it. Run it with the monitor open at 9600. You can actually test it with a button from the pin to ground.

// https://forum.arduino.cc/index.php?topic=649119
// ir beam count
//   (when it says "button" it's actually using an ir sensor... it's old code I use for a button ;) )

/*
  BASED ON State change detection (edge detection) changed for INPUT PULLUP
  https://www.arduino.cc/en/Tutorial/StateChangeDetection
*/

// this constant won't change:
const int  buttonPin = 3;    // the pin that the pushbutton is attached to
//   the button must be wired from pin to ground, its pinmode is input_pullup

// Variables will change:
bool buttonState;         // current state of the button
bool lastButtonState;     // previous state of the button
int count;

void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println("....  ....");
  Serial.print("Compiler: ");
  Serial.print(__VERSION__);
  Serial.print(", Arduino IDE: ");
  Serial.println(ARDUINO);
  Serial.print("Created: ");
  Serial.print(__TIME__);
  Serial.print(", ");
  Serial.println(__DATE__);
  Serial.println(__FILE__);

  // initialize the button pin as a input with pullup so active low
  //    make sure the button is from pin to ground
  pinMode(buttonPin, INPUT_PULLUP);

  //initialize button states
  buttonState = digitalRead(buttonPin);
  lastButtonState = buttonState;

  //turn bulitin led off
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  Serial.println(" ");
  Serial.println("Setup done... ");
  Serial.println(" ");
}

void loop()
{
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) // means it changed... but which way?
  {
    if (buttonState == LOW)  // changed to pressed
    {
      // if the current state is LOW then the button was pressed
      count++;
      Serial.print("Newly obstructed, count = ");
      Serial.print(count);
    }
    else
    {
      Serial.println(", obstruction has gone");
    }

    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;

} //loop
  • which is your time to waste as you wish, but don’t waste forum members’ time by just trying to throw stuff together and hope someone can fix it for you. Think… work logically… get one thing working first by understanding not by hoping.

PS... before you continue, perhaps have a look here at the datasheet, and run that simple code there just to prove the sensor works. It says it's normally high, so it should show a 1 when it's clear and 0 when it's obstructed.

If you do that, you at least know it works and any other problems you may have do not relate to the sensor itself.

(Since the sensor forces a high, it's ok to use pinMode at just INPUT, not INPUT_PULLUP.)

sir I already test the sensor then Ive change the code based on the datassheet also

but i have a void problem I cant I dnetify its problem

its says redefinition of ‘void setup()’

#include<LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);

int ir_right_pin = 6;
int ir_left_pin = 7;

int ir_right_state = 0;
int ir_left_state  = 0;

int ir_right_state_last = -1;
int ir_left_state_last  = -1;

int in_counter = 0;
int out_counter = 0;

bool bWalkIn = false;
bool bWalkOut = false;
unsigned long tm;

int count;
int relay=2;

void IN()
{
    count++;
    lcd.clear();
    lcd.print("Person In Room:");
    lcd.setCursor(0,1);
    lcd.print(count);
    delay(1000);
}

void OUT()
{
  count--;
    lcd.clear();
    lcd.print("Person In Room:");
    lcd.setCursor(0,1);
    lcd.print(count);
    delay(1000);
}

void setup()
{
  lcd.begin(16,2);
  lcd.print("Visitor Counter");
  delay(2000);
  pinMode(ir_right_pin, INPUT);
  pinMode(ir_left_pin, INPUT);
  pinMode(relay, OUTPUT);
  lcd.clear();
  lcd.print("Person In Room:");
  lcd.setCursor(0,1);
  lcd.print(count);
}

void loop()
{
 
  if(digitalRead(ir_right_pin))
  IN();
  if(digitalRead(ir_left_pin))
  OUT();
 
  if(count<=0)
  {
    lcd.clear();
    digitalWrite(relay, LOW);
    lcd.clear();
    lcd.print("Nobody In Room");
    lcd.setCursor(0,1);
    lcd.print("Light Is Off");
    delay(200);
  }
 
  else
    digitalWrite(relay, HIGH);
 
}

void setup(void) {
  
   Serial.begin(9600);
   pinMode( ir_right_pin, INPUT);
   pinMode( ir_left_pin , INPUT);
}

void loop(void) {
  
     ir_right_state = digitalRead( ir_right_pin );
     ir_left_state =  digitalRead( ir_left_pin );

     Serial.print( ir_left_state );
     Serial.print( " " );
     Serial.println( ir_right_state );

     checkWalkIn();
     checkWalkOUT();

       
}
void checkWalkIn(){

    if( ir_right_state != ir_right_state_last ){

         ir_right_state_last = ir_right_state;
         if( (bWalkIn == false) && ( ir_right_state == LOW ) ){
              bWalkIn = true;
              tm = millis();
         }
     }

     if( (millis() - tm) > DELAY_TIMEOUT ){
          bWalkIn = false;
     }

     if( bWalkIn && (ir_left_state == LOW) && (ir_right_state == HIGH) ){
          bWalkIn = false;
          in_counter++;
          updateLCD();
     }

}
void checkWalkOUT(){

    if( ir_left_state != ir_left_state_last ){

         ir_left_state_last = ir_left_state;
         if( (bWalkOut == false) && ( ir_left_state == LOW ) ){
              bWalkOut = true;
              tm = millis();
         }
     }

     if( (millis() - tm) > DELAY_TIMEOUT ){
          bWalkOut = false;
     }

     if( bWalkOut && (ir_right_state == LOW) && (ir_left_state == HIGH) ){
          bWalkOut = false;
          out_counter++;
          updateLCD();
     }

kalilinux25:
its says redefinition of 'void setup()'

Yeah well that's because you have 2x setup()s. You don't "mix" sketches just by concatenating them.

void setup()
{
  lcd.begin(16,2);
...
}

....

void setup(void) {
 
Serial.begin(9600);
...
}

You have 2x loop()s also.

That's not too clear even when I zoom, but if that's my code with your sensor counting properly, that's progress.

Now double up the code there for another sensor, but in that one do a count-- not a ++.

That should then keep the running total as folk enter and leave.

THANKS sir , but I really cant Understand How can I make the code for the LCD to display the AMOUNT of person and MINUS the person going out? and the code from my void is pretty measy I think

One step at a time. Get two sensors working correctly with ++ and -- and the total on the monitor... Or have you got that already?

Then if I were you I would test a new sketch with just the lcd. You could have a variable called count=999 say and just make that display, without the sensors

Im going off line now, I have work to do

Have a look at this.

Note that my lcd shield forces me to use certain pins and limits the ones I can use for other stuff, so check all the pins and change them to suit you if necessary.

Also the lcd library I call may not be the right one for you.

// https://forum.arduino.cc/index.php?topic=649119
// ir beam count
//   (when it says "button" it's actually using an ir sensor... it's old code I use for a button ;) )
// one button is ++, other is --, current total is on the lcd.

/*
  BASED ON State change detection (edge detection) changed for INPUT PULLUP
  https://www.arduino.cc/en/Tutorial/StateChangeDetection
*/

#include <LiquidCrystal.h>
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

// this constant won't change:
const int  buttonPin = 3;    // the pin that the pushbutton is attached to
//   the button must be wired from pin to ground, its pinmode is input_pullup
const int  button2Pin = 2;

// Variables will change:
bool buttonState;         // current state of the button
bool lastButtonState;     // previous state of the button
bool button2State;         // current state of the button
bool lastButton2State;
int count;

void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println("....  ....");
  Serial.print("Compiler: ");
  Serial.print(__VERSION__);
  Serial.print(", Arduino IDE: ");
  Serial.println(ARDUINO);
  Serial.print("Created: ");
  Serial.print(__TIME__);
  Serial.print(", ");
  Serial.println(__DATE__);
  Serial.println(__FILE__);

  // initialize the button pin as a input with pullup so active low
  //    make sure the button is from pin to ground
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(button2Pin, INPUT_PULLUP);

  //initialize button states
  buttonState = digitalRead(buttonPin);
  lastButtonState = buttonState;
  button2State = digitalRead(button2Pin);
  lastButton2State = button2State;

  //turn bulitin led off
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  lcd.begin(16, 2);              // start the library
  lcd.setCursor(0, 0);
  lcd.print("No of visitors:");
  lcd.setCursor(4, 1);
  lcd.print(count);

  Serial.println(" ");
  Serial.println("Setup done... ");
  Serial.println(" ");
}

void loop()
{
  readButtons();
  increment();
  decrement();
} //loop

void readButtons()
{
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  button2State = digitalRead(button2Pin);
}

void increment()
{
  //increment....
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) // means it changed... but which way?
  {
    if (buttonState == LOW)  // changed to pressed
    {
      // if the current state is LOW then the button was pressed
      count++;
      Serial.print("Someone entered, count = ");
      Serial.println(count);
      lcd.setCursor(0, 1);
      lcd.print("                ");
      lcd.setCursor(4, 1);
      lcd.print(count);

    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
} //increment

void decrement()
{
  //decrement....
  // compare the buttonState to its previous state
  if (button2State != lastButton2State) // means it changed... but which way?
  {
    if (button2State == LOW)  // changed to pressed
    {
      // if the current state is LOW then the button was pressed
      count--;
      if (count < 0) count = 0;
      Serial.print("    Someone left, count = ");
      Serial.println(count);
      lcd.setCursor(0, 1);
      lcd.print("                ");
      lcd.setCursor(4, 1);
      lcd.print(count);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButton2State = button2State;
}//decrement

@kalilinux25 has another thread about the same project. At a glance, I can't tell whether it's a cross-post or not, but certainly it's very similar:

@kalillinux, blh64 answering in your other thread, is of the opinion that each person coming or going triggers both sensors: someone coming in triggers A then B, and someone leaving triggers B then A, with everyone using the same door.

I got the impression in this thread that there's an "in" sensor and an "out" sensor, and that someone coming in triggers only A, someone leaving triggers only B, like with 2 doors.

My code is written for the latter case.

Please clarify which way it's supposed to work.

You should also click Report to Moderator and let pert know if both threads are actually about the same project or not. (I suspect they are the same.)

Yes sir your right about if someone goes in the IR1 WILL DETECT then I IR2 MUST not detect the person. BUT. if the IR2 is active the IR1 must be deactive... I tried the code that you give and the other problem was if someone entering the door it will count but the same time it will decrease because the two IR sensor is placed at one place. I wanted to use the two sensor with only 1 door.. Can it be possible to add code that if IR1 is ON the. IR2 must be OFF and if IR2 is active the IR1 must be off