Help -Number of people inside a store counter

I've made this project with a SR04 to add and subtract people going in and coming out a store from the same entrance.

The project works correctly. The only issue is that sometimes the sensors skip a measurement.

I see that there's a
" digitalWrite(Trig_pin, LOW);
delayMicroseconds(2);
digitalWrite(Trig_pin, HIGH);
delayMicroseconds(10);" inside the library. Could it help increasing the high timing?

#include <millisDelay.h>
#include <Ultrasonic.h>

Ultrasonic ultrasonicdoor(6, 7); //(trig, echo)
Ultrasonic ultrasonicshop(8, 9); //(trig, echo)

const unsigned long DELAY_DOOR = 1500;     //Change var for queue flow from one person to another  entrance // in mS
const unsigned long DELAY_SHOP = 1500;     //Change var for queue flow from one person to another  2ndpht // in mS
millisDelay doordelay; // the delay object
millisDelay shopdelay; // the delay object

bool doitonceDoor = 0;    //Limits increase to 1  (door)
bool doitonceShop = 0;    //Limits increase to 1  (shop)


const int redled = 3;     //No more can enter
const int greenled = 2;   // Ok to enter

int phtdoor = 0; //   door  pht
int phtshop = 0;  //   shop pht
int vphtdoor = 0; //  Buffer door  pht
int vphtshop = 0; //  Buffer shop  pht

int cust = 0;   //increase number people
int maxcust = 5; //Mod to fix max customers

void Door();
void In();

void setup() {
  //Serial communication
  Serial.begin(9600);

  pinMode(redled, OUTPUT);
  pinMode(greenled, OUTPUT);
  digitalWrite(redled, LOW);
  digitalWrite(greenled, LOW);

}

void loop()
{

  Door();
  In();


  if ((phtdoor == 1) && (vphtshop == 0)) // Somebody passed first by the entrance photocel-- Is entering


  {
    if (doitonceDoor == false)      //limit times scrip is executed
    {
      cust = cust + 1;                //Add one person from counter
      doitonceDoor = true;
    }
  }


  if ((vphtdoor == 0) && (phtshop == 1)) // Somebody passed first by the shop photocell-- Is exiting

  {
    if (doitonceShop == false)    //limit times scrip is executed
    {
      cust = cust - 1;               //Subtract one person from counter
      doitonceShop = true;
    }
  }




  if        (cust >= maxcust)       //Flag red light if shop limit
  {
    //   Serial.print("Luce Rossa \n");
    digitalWrite(redled, HIGH);
    digitalWrite(greenled, LOW);
    delay(10);

  }
  else if (cust < maxcust)        //Flag green light if shop < limit
  {
    //   Serial.print("Luce Verde \n");
    digitalWrite(redled, LOW);
    digitalWrite(greenled, HIGH);
    delay(10);
  }





//DEBUG

  /* Serial.print("CUSTOMERS\n");
    Serial.print(cust);
    Serial.print("\n");
    delay(10);
    Serial.print("IN\n");
    Serial.print(phtshop);
    Serial.print("\n");
    delay(10);
    Serial.print("OUT\n");
    Serial.print(phtshop);
    Serial.print("\n");
    delay(10);
    Serial.print("CUSTOMERS\n");
    Serial.print(cust);
    Serial.print("\n");
    delay(10);*/
}


void Door()
{


  if ((ultrasonicdoor.Ranging(CM) <= 65))    //If someone breaks the trigger distance, fix phtdoor to 1 and virtual pht to 1
  {

    phtdoor = 1;  //real pht door

    doordelay.start(DELAY_DOOR);    // This sets the buffer variable to 1 for x seconds, then will reset via function (doordelay.justFinished())
    vphtdoor = 1; //virtual pht door for buffer
    Serial.println("DOORDelay.start called temp turned ON");
  }
  else
  {
    phtdoor = 0;
  }

  if (doordelay.justFinished())
  {
    // delay timed out
    vphtdoor = 0;       //Reset vphtdoor
    doitonceDoor = 0;   //Reset counter
    Serial.println("DOOR Delay finished, temp turned off.");
  }

}




void In()
{


  if ((ultrasonicshop.Ranging(CM) <= 65))     //If someone breaks the trigger distance, fix phtshop to 1 and virtual pht to 1
  {
    phtshop = 1;    // real pht shop

    shopdelay.start(DELAY_SHOP);      // This sets the buffer variable to 1 for x seconds, then will reset via function (doordelay.justFinished())
    vphtshop = 1;   //virtual pht shop for buffer
    Serial.println("SHOP.start called temp shop turned ON");

  }
  else
  {
    phtshop = 0;
  }

  if (shopdelay.justFinished())
  {
    // delay timed out
    vphtshop = 0;               //Reset pht shop
    doitonceShop = false;     //Reset counter
    Serial.println("SHOP Delay finished, temp turned off.");
  }


}

Could it help increasing the high timing?

No, because the length of the pulse is not relevant. It is the low to high transition that triggers the rangefinder.

The strength of the echo return depends, heavily, on the material of the target. Soft fluffy material will not reflect well. Also the angle of the surface will affect the strength of the echo. A hard surface perpendicular to the sonar beam will reflect well. The more the angle the less the reflection.

A break-beam sensor would probably be more reliable. Actually you would need a pair of them suitable spaced so that you could tell if the person breaking the beams is entering or leaving.

If people are allowed to enter and leave at the same time, or in groups it will be difficult or impossible to count them.

...R

The strength of the echo return depends, heavily, on the material of the target. Soft fluffy material will not reflect well. Also the angle of the surface will affect the strength of the echo. A hard surface perpendicular to the sonar beam will reflect well. The more the angle the less the reflection.

This is a great answer. It really explains why it detects perfectly my hand, or even more a book, but 3/5 if il walk past the pht, it may skip as it sees my trousers

would this work?

That is a reflective sensor so the range will be limited. And it will be affected by ambient light. A better way would be a "light gate" using an infrared LED that is modulated and a IR receiver that will detect modulated IR and is made for light gate use.

First of all, have you considered privacy issue?

Those small 3D cameras are a little more expensive than the SR04 but they can give you a picture of what is happening. Each pixel gives you the distance to the object, so you could recognize shapes (humans/objects, etc). At the same time it is GDPR compliant, so you won’t have to deal with privacy issues. You can not recognize who is who