Using 2 PIR sensors on Leonardo board to detect passerby.

Hello everybody.

So I’m trying to use an Arduino Leonardo R3 board interfacing with a PC to trigger 2 PIR sensors (one on each side of a hallway) to detect people passing through each side (‘left and right’), count them, trigger a led and press the Right Key or Left key arrow anytime the corresponding sensor goes off.

I am new to arduino and an absolute noob in coding, but I did some research and was able to code something and compile it successfully (though I have no way to try it), it looks something like this:

const int LEDPin = 13;        // pin para el LED A

const int PIRPinA = 2;         // pin de entrada (for PIR sensor A)
int pirStateA = LOW;           // de inicio no hay movimiento en A
int valA = 0;                 // estado del pin para A

const int PIRPinB = 3;         // pin de entrada (for PIR sensor B)
int pirStateB = LOW;          // de inicio no hay movimiento en B
int valB = 0;                 // estado del pin para B

int count = 0;

#include <Keyboard.h>

void setup()
{
  pinMode(LEDPin, OUTPUT);
  pinMode(PIRPinA, INPUT);
  pinMode(PIRPinB, INPUT);
  Serial.begin(9600);
}

void loop()
{
  
  ///Sensor A///
  
  valA = digitalRead(PIRPinA);
  if (valA == HIGH)   //si está activado
  {
    Keyboard.press(KEY_RIGHT_ARROW)  //presionar tecla derecha
    ; digitalWrite(LEDPin, HIGH); //LED ON
    if (pirStateA == LOW)  //si previamente estaba apagado
      ; {
      Serial.println("Izquierda");
      count ++;
      Serial.println("Fuerza:");
      Serial.println(count);
      pirStateA = HIGH;
    }
  }
  else   //si esta desactivado
  {
    ; digitalWrite(LEDPin, LOW); // LED OFF
    if (pirStateA == HIGH)  //si previamente estaba encendido
      Keyboard.releaseAll()  //soltar todas las teclas
      ; {
      pirStateA = LOW;
    }
  }

  ///Sensor B///
  
  valB = digitalRead(PIRPinB);
  if (valB == HIGH)   //si está activado
  {
    Keyboard.press(KEY_LEFT_ARROW)  //presionar tecla izquierda
    ; digitalWrite(LEDPin, HIGH); //LED ON
    if (pirStateB == LOW)  //si previamente estaba apagado
      ; {
      Serial.println("Derecha");
      count ++;
      Serial.println("Fuerza:");
      Serial.println(count);
      pirStateB = HIGH;
    }
  }
  else   //si esta desactivado
  {
    ; digitalWrite(LEDPin, LOW); // LED OFF
    if (pirStateB == HIGH)  //si previamente estaba encendido
      Keyboard.releaseAll()  //soltar todas las teclas
      ; {
      pirStateB = LOW;
    }
  }

  ///Contador///

  if (count <= 0)
 {digitalWrite(LEDPin, LOW);}

 else{
  digitalWrite(LEDPin, HIGH);
 }

So, the problem I see so far (notice I haven’t been able to test the code since I don’t get the parts till monday) is that I have only one counter for both inputs and I’m probably going to need separate counters for each input, so it counts how many people have gone through each side of the hallway and not a total on both sides.

The precision of the counting is not crucial, though I’d like it to be as precise as possible with the parts I already mentioned (2 pir sensors on each end of the hallway, to keep it simple, Im worried adding more PIRs will make the code too complex for me), I’m also wondering what would happen with the input if 2 people walked through the sensors at the same time, and if that could trigger some sort of bug on the code since it would trigger the left and right key arrow at the same time?

I’ll be here hoping someone guides me.
Greetings and thx in advance.

See the StateChangeDetection example for handling multiple signal sources at the same time.

You should worry more about multiple people passing through the same gate at the same time, then you won't get a reliable count. This is a known problem, with no real solution. A video camera may yield better results, but usual entry controls use gates that can be passed by only one person at any time.

Post a link to the PIR sensors you intend to use. Not all are suitable for this because they have a fixed triggered period (example 9 seconds) which could cause you to miss some counts. However, in this case, it may be possible to hack the board to change the behaviour.