Bi-Directional person counter

Hello,
I would like to make a Bi-Directional person counter with two PIR sensors like these

http://www.circuitstoday.com/wp-content/uploads/2016/02/PIR_Sensor_Pin_Out.jpg [/ img]

placed one after the other.

the purpose is to detect on the basis of which of the two sensors has been turned on before (or in any other way) if a person enters or leaves and increase or decrease a counter to turn on the LED if there are people in the room and turn it off if not .

this is the code:

const int Sensora = 3;
const int sensorB = 4;
const int output = 13;
int count = 0;

sensorATriggered boolean = false;
unsigned long sensorATime;
currStateA bytes;
prevStateA bytes = LOW;

sensorBTriggered boolean = false;
unsigned long sensorBTime;
currStateB bytes;
prevStateB bytes = LOW;

void setup () {
Serial.begin (9600);
pinMode (Sensora, INPUT);
pinMode (sensorB, INPUT);
pinMode (output, OUTPUT);
}

void loop () {
currStateA = digitalRead (Sensora);

 if (currStateA! = prevStateA)
 {
 if (currStateA == HIGH)
 {
 Serial.println ( "PIR1");
 sensorATriggered = true;
 sensorATime = millis ();
 }
 }
 prevStateA = currStateA;

 currStateB = digitalRead (sensorB);
 if (currStateB! = prevStateB)
 {
 if (currStateB == HIGH)
 {
 Serial.println ( "PIR2");
 sensorBTriggered = true;
 sensorBTime = millis ();
 }
 }
 prevStateB = currStateB;

 if (sensorBTriggered && sensorATriggered)
{
 if (sensorATime> sensorBTime)
 {
 count ++;
 
 }
 if (sensorBTime> sensorATime)
 {
 count--;
 
 }
 else
 {
 
 
 }
}
 
if (count> 0)
{
 digitalWrite (output, HIGH);
}
else
{
digitalWrite (output, LOW);
}
 

 

}
[/ Code]
Thanks for your help :)

Try here

can't do this right?

sensorATriggered boolean = false;

if (currStateA! = prevStateA)And what happened when you tried to compile it?

BobStark:
can't do this right?

Not with a PIR sensor. They are not directional. You can use IR emitters and receivers, which can be set up on either side of an entryway.

const int Sensora = 3;
const int sensorB = 4;

Fire the drunken monkey that came uP wITh ThEsE naMes.

sensorATriggered boolean = false;

What type is THAT? You can't use a type name for a variable name.

currStateA bytes;
prevStateA bytes = LOW;

More unknown types...

The thing about coding is that it needs to be exact. Your code is 'approximate', and as you have found, it doesn't work. Don't exchange capital and lower case, don't add spaces in the middle of a word, and put things in the proper order.

In Paul's example, your type names need to precede the variable names.

ChrisTenone:
The thing about coding is that it needs to be exact. Your code is 'approximate', and as you have found, it doesn't work compile.

thanks for the replies and sorry for the code, I’m a beginner with programming and steal sketches here and there trying to figure out something …
this sketch seems to work if you use two switches instead of the sensors:

#define interval 1000
int buttonPin1 = 3;    
int buttonPin2 = 5;
int ledPin = 13;
int c=0;

int giro = 0;

int statoPulsante1 = 0;          // Stato attuale pulsante 1
int lastStatoPulsante1 = 0;      // Stato precedente pulsante 1


int statoPulsante2 = 0;         // Stato attuale pulsante 2
int lastStatoPulsante2 = 0;     // Stato precedente pulsante 2

int Segnale1=0;               //segnale_memoria 1
long Tempo1;                  //tempo_trascorso 1
long TempoM1;                 //tempo_memoria 1

int Segnale2=0;               //segnale_memoria 2
long Tempo2;                  //tempo_trascorso 2
long TempoM2;                 //tempo_memoria 2

void setup(){
  Serial.begin(9600);
  pinMode(buttonPin1, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(buttonPin1, LOW);
  pinMode(buttonPin2, INPUT);
  digitalWrite(buttonPin2, LOW);

}
void loop() {  

  // Calcolo il tempo trascorso dall'inizio dell'esecuzione al momento in cui si preme il pulsante1
  
  statoPulsante1 = digitalRead(buttonPin1);  
  
  if (statoPulsante1 != lastStatoPulsante1) {  

    if(statoPulsante1 == HIGH) {  
      if(Segnale1==0){
      Segnale1=1;
      Tempo1=millis()-TempoM1;
      Serial.print("Tempo1 ");
      Serial.println(Tempo1);
      }
     
    }
     
    if(Segnale1==1){
       Segnale1=0;
    }

    lastStatoPulsante1 = statoPulsante1;  
  }

  //------------------------------------------------------------------------------------------------


  // Calcolo il tempo trascorso dall'inizio dell'esecuzione al momento in cui si preme il pulsante2
  
  statoPulsante2 = digitalRead(buttonPin2);  

  
  if (statoPulsante2 != lastStatoPulsante2) {  

    if (statoPulsante2 == HIGH) {  

      if(Segnale2==0){
      Segnale2=1;
      Tempo2=millis()-TempoM2;
      Serial.print("Tempo2 ");
      Serial.println(Tempo2);
      }
  
    }
    if(Segnale2==1){
      Segnale2=0;
    }
  lastStatoPulsante2 = statoPulsante2;  
  }

  //------------------------------------------------------------------------------------------------

  //Paragonando i tempi dei 2 pulsanti calcolo quale dei 2 è stato premuto per primo stampando a video l'eventuale risultato
  
   if(statoPulsante1 == HIGH && giro == 0){
      giro = 1;

      //se il pulsanete1 è stato premuto prima del pulsante2 e questa sequenza avviene nell'intervallo di 1 secondo, incrementa il contatore
      
      if(Tempo1<Tempo2 && Tempo2-Tempo1<interval){
         if(c<20){
           c++;
           Serial.print("Nella stanza e' presente ");
           Serial.print(c);
           Serial.print(" persona-e   ");
           Serial.println();
           delay(600);
           }
      }
 
   }
   if(statoPulsante2 == HIGH && giro == 1){
      giro = 0;
   } 
    
   if(statoPulsante2 == HIGH && giro == 0){
    
   //se il pulsanete2 è stato premuto prima del pulsante1 e questa sequenza avviene nell'intervallo di 1 secondo, decrementa il contatore
    giro=1;
   if(Tempo2<Tempo1 && Tempo1-Tempo2<interval){
      if(c>=1){
         c--;
         Serial.print("Nella stanza e' presente ");
         Serial.print(c);
         Serial.print(" persona-e   ");
         Serial.println();
         delay(600);
       }
   }
   
   }
   
if(statoPulsante1 == HIGH && giro == 1){
      giro = 0;
   } 
   
  //Accendo il led se il contatore è maggiore di 0
   
   if(c>0){
      digitalWrite(ledPin, HIGH);
   } 
   else{
      digitalWrite(ledPin, LOW);
   } 
}

the problem seems to be that the PIR sensor remains high for at least 3 seconds, i will try using the modules used in this project

One other comment.
This method will not be reliable if more than one person 'overlaps' another e,g. walking side-by-side. Or otherwise compromising the field of view.
There are other strategies if this is important.

This is not a problem, through my door can pass only one person at a time, the idea was to put those modules on the switchbox lights between the two doors.
I have two tcrt500, I could use the LEDs in them (I do not know if they are suitable) put them opposite each other but Arduino provides a few millivolts and should make a circuit that drives the LED :confused:

BobStark:
the problem seems to be that the PIR sensor remains high for at least 3 seconds,

If that's the way that your hardware works, it makes this project much easier.

When pir A changes from low to high, AND pir B is currently high, increment the counter.
When pir B changes from low to high, AND pir A is currently high, decrement the counter.

This gives a 3-second interval for a person to get far enough through the door to activate the second sensor, which is heaps.

You could also do it the other way, which will will double the count and you'll have to divide by 2 to get the real number.

When pir A changes from low to high, AND pir B is currently low, decrement the counter.
When pir B changes from low to high, AND pir A is currently low, increment the counter.

Your count won't ever be accurate - it will tend to lowball the number of people - but it might be good enough.

Note how similar this problem is to quadrature encoding.