Bidirectional people counter using sharp IR sensors?

Hi there,

Please forgive me as I only have a very rudimentary knowlege of Arduino and know little to no C++, I am just on the level of copying and pasting tutorials, and then working mostly in max/msp or touch designer.

I am working with two sharp IR sensors for an installation I am doing - I want to make a bidirectional people counter like in this tutorial: https://www.youtube.com/watch?v=QynTEeFF5-k, I am using sharp IR sensors and not the ones with a narrower range because I needed them to cover a greater distance.

This is the code for that tutorial:

int irPin1=7;
int irPin2=8;
int count=0;
boolean state1 = true;
boolean state2 = true;
boolean insideState = false;
boolean outsideIr=false;
boolean isPeopleExiting=false;
int i=1;
void setup() {
Serial.begin(9600);
pinMode(irPin1, INPUT);
pinMode(irPin2, INPUT);
}

void loop() {
  
  if (!digitalRead(irPin1) && i==1 && state1){
     outsideIr=true;
     delay(100);
     i++;
     state1 = false;
  }

   if (!digitalRead(irPin2) && i==2 &&   state2){
     Serial.println("Entering into room");
     outsideIr=true;
     delay(100);
     i = 1 ;
     count++;
     Serial.print("No of persons inside the room: ");
     Serial.println(count);
     state2 = false;
  }

   if (!digitalRead(irPin2) && i==1 && state2 ){
     outsideIr=true;
     delay(100);
     i = 2 ;
     state2 = false;
  }

  if (!digitalRead(irPin1) && i==2 && state1 ){
     Serial.println("Exiting from room");
     outsideIr=true;
     delay(100);
     count--;
       Serial.print("No of persons inside the room: ");
       Serial.println(count);
     i = 1;
     state1 = false;
  }  

    if (digitalRead(irPin1)){
     state1 = true;
    }

     if (digitalRead(irPin2)){
     state2 = true;
    }
  
}

I need something like that but for the sharp IR sensors. All I have is this code, which just prints the distance from the two sensors I have set up:

/*SHARP GP2Y0A21YK0F IR distance sensor with Arduino and SharpIR library example code. More info: https://www.makerguides.com */

// Include the library:
#include <SharpIR.h>

// Define model and input pin:
#define IRPin A0
#define model 1080
#define IRPin2 A1
#define model 1080

// Create variable to store the distance:
int distance_cm;

/* Model :
  GP2Y0A02YK0F --> 20150
  GP2Y0A21YK0F --> 1080
  GP2Y0A710K0F --> 100500
  GP2YA41SK0F --> 430
*/

// Create a new instance of the SharpIR class:
SharpIR mySensor = SharpIR(IRPin, model);
SharpIR mySensor2 = SharpIR(IRPin2, model);

void setup() {
  // Begin serial communication at a baudrate of 9600:
  Serial.begin(9600);
}

void loop() {
  // Get a distance measurement and store it as distance_cm:
  distance_cm = mySensor.distance();

  // Print the measured distance to the serial monitor:
  Serial.print(distance_cm);
  Serial.println(" cm");

   // Get a distance measurement and store it as distance_cm:
  distance_cm = mySensor2.distance();

  // Print the measured distance to the serial monitor:
  Serial.print("second sensor");
  Serial.print(distance_cm);
  Serial.println(" cm");

  delay(500);
}

I need something like the bidirectional people counter code, except for analog pins and with the sharp library code stuff included.

Can anyone possibly help me out? I am totally clueless.

Well, the good news is that the hardware is already on different pins, so you should be able to run both codes independently on the same hardware.

The bad news is that both codes depend on delay, which makes it hard for them to cooperate. But they don't need to cooperate. You want to replace the sensors in the first code with the sensors for the second code, which shouldn't be too bad.

Looking at the first code, I'd set up two new variables to interpret the sensors into a meaningful term:

bool nobodyAtSensor1 = (digitalRead(irPin1) == HIGH);
bool nobodyAtSensor2 = (digitalRead(irPin2) == HIGH);

(See == - Arduino Reference )

... and then rewrite the conditions to use those new variables.

After I got that working, I'd add in the bits from code 2 to enable and read the other sensors, and then replace those two lines with:

bool nobodyAtSensor1 = mySensor.distance() > 150; // or whatever distance means there is no person
bool nobodyAtSensor2 = mySensor2.distance() > 100;

Let me just say this problem is a lot harder to get to work in the real world than in your imagination.

The only real way is to use a turnstile where the direction of people movement is controlled to one direction only. Even the commercial people counting solutions do not work 100%.

[quote="merih89, post:1, topic:1105047"]

// Create variable to store the distance:
int distance_cm;

/* Model :
  GP2Y0A02YK0F --> 20150
  GP2Y0A21YK0F --> 1080
  GP2Y0A710K0F --> 100500
  GP2YA41SK0F --> 430
*/

// Create a new instance of the SharpIR class:
SharpIR mySensor = SharpIR(IRPin, model);
SharpIR mySensor2 = SharpIR(IRPin2, model);

[/quote]I think I am close but it is just printing 0s and 1s non-stop, any guidance?

/*SHARP GP2Y0A21YK0F IR distance sensor with Arduino and SharpIR library example code. More info: https://www.makerguides.com */

// Include the library:
#include <SharpIR.h>

// Define model and input pin:
#define IRPin A0
#define model 1080
#define IRPin2 A1
#define model 1080

// Create variable to store the distance:
int distance;

/* Model :
  GP2Y0A02YK0F --> 20150
  GP2Y0A21YK0F --> 1080
  GP2Y0A710K0F --> 100500
  GP2YA41SK0F --> 430
*/

// Create a new instance of the SharpIR class:
SharpIR mySensor = SharpIR(IRPin, model);
SharpIR mySensor2 = SharpIR(IRPin2, model);


int irPin1=7;
int irPin2=8;
int count=0;
boolean state1 = true;
boolean state2 = true;
boolean insideState = false;
boolean outsideIr=false;
boolean isPeopleExiting=false;
int i=1;

bool nobodyAtSensor1 = mySensor.distance() > 20; // or whatever distance means there is no person
bool nobodyAtSensor2 = mySensor2.distance() > 20;


void setup() {
Serial.begin(9600);
pinMode(irPin1, INPUT);
pinMode(irPin2, INPUT);
}

void loop() {
  
  if (!digitalRead(irPin1) && i==1 && nobodyAtSensor1){
     outsideIr=true;
     delay(100);
     i++;
     nobodyAtSensor1 = false;
  }

   if (!digitalRead(irPin2) && i==2 &&   nobodyAtSensor2){
     Serial.println("Entering into room");
     outsideIr=true;
     delay(100);
     i = 1 ;
     count++;
     Serial.print("No of persons inside the room: ");
     Serial.println(count);
     nobodyAtSensor2 = false;
  }

   if (!digitalRead(irPin2) && i==1 && nobodyAtSensor2 ){
     outsideIr=true;
     delay(100);
     i = 2 ;
     nobodyAtSensor2 = false;
  }

  if (!digitalRead(irPin1) && i==2 && nobodyAtSensor1 ){
     Serial.println("Exiting from room");
     outsideIr=true;
     delay(100);
     count--;
       Serial.print("No of persons inside the room: ");
       Serial.println(count);
     i = 1;
   nobodyAtSensor1 = false;
  }  

    if (digitalRead(irPin1)){
     nobodyAtSensor1 = true;
    }

     if (digitalRead(irPin2)){
    nobodyAtSensor2 = true;
    }
  
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.