user39839: 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 !!");
  Serial.println(ultrasonic.distanceRead());
  Serial.println(ultrasonic2.distanceRead());
}

void loop() 
{
  currentMillis=(millis())%100;
  d1=ultrasonic.distanceRead();
  d2=ultrasonic2.distanceRead();

  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;
  }

}


user20245: 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. 

39839: <p>I want to count the number of people in a room using a microcontroller (Arduino Uno). </p>

<p>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.</p>

<p>To do this: 
If sensor 1 detects a disturbance and then sensor 2 detects a disturbance then increment the counter.<br>
If sensor 2 detects a disturbance and then sensor 1 detects a disturbance then decrement the counter.</p>

<p>However, my code is not working</p>

<pre><code>#include&lt;Ultrasonic.h&gt;

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 !!");
  Serial.println(ultrasonic.distanceRead());
  Serial.println(ultrasonic2.distanceRead());
}

void loop() 
{
  currentMillis=(millis())%100;
  d1=ultrasonic.distanceRead();
  d2=ultrasonic2.distanceRead();

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


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

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

}
</code></pre>
 20245: <p>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.</p>

<p>The technique used for counting people is similar to the technique used in older mouse-pointing-devices to detect direction.  The technique is called <a href="http://www.fpga4fun.com/QuadratureDecoder.html" rel="nofollow noreferrer">quadrature decoding</a> such as used by an <a href="https://en.wikipedia.org/wiki/Rotary_encoder#Incremental_rotary_encoder" rel="nofollow noreferrer">incremental rotary encoders</a>.  </p>

<p>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.</p>

<p>Consider <a href="https://playground.arduino.cc/Main/RotaryEncoders" rel="nofollow noreferrer">looking here</a> for example Arduino code and an explanation how to use an Arduino with a quadrature encoded knob. </p>
