# Counting number of people inside a room [closed]

I want to count the number of people in a room using a microcontroller (Arduino Uno).

I have 2 ultrasonic sensors at a doorway about a 1.5m apart from each other. I want to increment the number when someone walks into the room and decrement that number when someone walks out.

To do this: If sensor 1 detects a disturbance and then sensor 2 detects a disturbance then increment the counter.
If sensor 2 detects a disturbance and then sensor 1 detects a disturbance then decrement the counter.

However, my code is not working

``````#include<Ultrasonic.h>

Ultrasonic ultrasonic(5,6);

Ultrasonic ultrasonic2(11,10);

unsigned long currentMillis =0,t1,t2;

int d1,d2,c;

boolean ex=0,en=0,in;

void setup()
{
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
Serial.begin(9600);
delay(5000);
Serial.println("WELCOME TO AUTOMATION !!");
}

void loop()
{
currentMillis=(millis())%100;

if(currentMillis==0)
{
Serial.print("Count is :");
Serial.println(c);
Serial.println(d1);
Serial.println(d2);
}

if(d1<=15 && d2<=15)
{
c=c;
}
else if(d1<=15)
{
if(en==1)
{
c++;
en=0;
}
else ex=1;
}

else if(d2<=15)
{
if(ex==1)
{
c--;
ex=0;
}
else en=0;
}

}
``````
• Please explain what is happening, why is it not working ? You can not use millis like that, you have to use millis like the blink without delay example: arduino.cc/en/Tutorial/BlinkWithoutDelay Which Ultrasonic.h library is that ? The code "c=c;" does not really do something. Do you know the state change detection example: arduino.cc/en/Tutorial/StateChangeDetection Some variables are declared but not used. The code for counting with d1 and d2 is not mirrored code, once says "else ex=1;" but the other one says: "else en=0;" ("=1" versus "=0"). – Jot Nov 15 '17 at 6:13

Considering abandoning the ultra sonic sensors. Instead use 2 light beams positioned so a moving person would block one beam then both as they passed. This problem has been commercially solved for decades using this method.

The technique used for counting people is similar to the technique used in older mouse-pointing-devices to detect direction. The technique is called quadrature decoding such as used by an incremental rotary encoders.

Specifically, you need to detect if someone is walking in or is walking out of a room. But, to do this, you need to assure that first one then both beams will be block by the person. It is likely this requirement will be easier to meet using light beams rather than ultrasonic sensors.

Consider looking here for example Arduino code and an explanation how to use an Arduino with a quadrature encoded knob.

I want to count the number of people in a room using a microcontroller (Arduino Uno).

a few ways:

1) have sensors on the entrance + exit;

2) require people in the room to push a button periodically;

3) embed sensors on the people. rfid would work for example;

4) have an image sensor and put a super-computer to analyze the picture;

5) weigh those people on a scale;

6) route those people through a single file gate and count pulses;

...

just for starters.