People counter with two ultrasonic sensors

hello
so I am working on a people counter project with two ultrasonic sensors
so I hooked up two ultrasonic sensors after i tested them successfully and connected them to a lcd 16*2 display all together to my nano board and I started coding
And my concept was if sensor 1 detected object within 15 cm then sensor two will still three seconds sensor searching for an object within the same range(15 cm) and if it detected it within the three seconds count variable will be increased by 1 and the otherwise (sensor 2 is followed by sensor 1 count decreased by 1 )
and this is the code :
#include <LiquidCrystal.h>
long unsigned i;
long unsigned j;
int count ;
int distance1 ;
int distance2 ;
int duration1 ;
int duration2 ;
const float vilocity = 0.0343;
LiquidCrystal lcd(12,11,10,9,8,7);
void setup() {
pinMode(2,OUTPUT);
pinMode(4,INPUT);
pinMode(3,OUTPUT);
pinMode(5,INPUT);
pinMode(6,OUTPUT);
lcd.begin(16,2);
Serial.begin(9600);
}
int sensor1(){
digitalWrite(2,HIGH);
delayMicroseconds(2);
digitalWrite(2,LOW);
delayMicroseconds(2);
duration1 = pulseIn(4,HIGH);
distance1 = (duration1/2)vilocity;
return distance1;
}
int sensor2(){
digitalWrite(3,HIGH);
delayMicroseconds(2);
digitalWrite(3,LOW);
delayMicroseconds(2);
duration2 = pulseIn(5,HIGH);
distance2 = duration2/2
vilocity;
return distance2 ;
}
void loop(){
sensor1();
sensor2();
Serial.print("sensor1 = ");Serial.println(sensor1());
Serial.print("sensor2 = ");Serial.println(sensor2());
if(distance1 < 15);{
i = millis;
while ((millis() - i)<=3000){
sensor2();
Serial.print("sensor2 = ");Serial.println(sensor2());
if (distance2 < 15){
count++;
break;
}}
} Serial.print("count = ");Serial.println(count);
sensor2();
if(distance2<15);{
j = millis;
while ((millis() -i )<=3000){
sensor1();
Serial.print("sensor1 = ");Serial.println(sensor1());
if (distance1 < 15){
count--;
break;
}
}}Serial.print("count = ");Serial.println(count);
if(count==0){
lcd.clear();
lcd.setCursor(0,0);
lcd.println ("NoBody in room");
}if (count>0){
lcd.clear();
lcd.setCursor(0,0);
lcd.println("Lights are on");
lcd.setCursor(0,1);
lcd.print("persons =");
lcd.println(count);
digitalWrite(6,HIGH);
}
}
so it never counts what is the problem !!

counter_test_8_.ino (1.8 KB)

You are likely to get more help if you format and post your code properly. Poorly indented code is hard to read and follow. Autoformat (ctrl-t, or in the IDE, Tools, Auto Format) the code to indent in a standard fashion for readability. Read the how to use this forum-please read sticky to see how to post code properly.

Here is your code auto-formatted and in code tags.

#include <LiquidCrystal.h>
long unsigned  i;
long unsigned  j;
int count ;
int distance1 ;
int distance2 ;
int duration1 ;
int duration2 ;
const float vilocity = 0.0343;
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

void setup()
{
   pinMode(2, OUTPUT);
   pinMode(4, INPUT);
   pinMode(3, OUTPUT);
   pinMode(5, INPUT);
   pinMode(6, OUTPUT);
   lcd.begin(16, 2);
   Serial.begin(9600);
}

int sensor1()
{
   digitalWrite(2, HIGH);
   delayMicroseconds(2);
   digitalWrite(2, LOW);
   delayMicroseconds(2);
   duration1 = pulseIn(4, HIGH);
   distance1 = (duration1 / 2) * vilocity;
   return distance1;
}

int sensor2()
{
   digitalWrite(3, HIGH);
   delayMicroseconds(2);
   digitalWrite(3, LOW);
   delayMicroseconds(2);
   duration2 = pulseIn(5, HIGH);
   distance2 = duration2 / 2 * vilocity;
   return distance2 ;
}

void loop()
{
   sensor1();
   sensor2();
   Serial.print("sensor1 = "); Serial.println(sensor1());
   Serial.print("sensor2 = "); Serial.println(sensor2());
   if (distance1 < 15);
   {
      i = millis;
      while ((millis() - i) <= 3000)
      {
         sensor2();
         Serial.print("sensor2 = "); Serial.println(sensor2());
         if (distance2 < 15)
         {
            count++;
            break;
         }
      }
   } Serial.print("count = "); Serial.println(count);
   sensor2();
   if (distance2 < 15);
   {
      j = millis;
      while ((millis() - i ) <= 3000)
      {
         sensor1();
         Serial.print("sensor1 = "); Serial.println(sensor1());
         if (distance1 < 15)
         {
            count--;
            break;
         }
      }
   } Serial.print("count = "); Serial.println(count);
   if (count == 0)
   {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.println ("NoBody in room");
   } if (count > 0)
   {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.println("Lights are on");
      lcd.setCursor(0, 1);
      lcd.print("persons =");
      lcd.println(count);
      digitalWrite(6, HIGH);
   }
}

What do your serial prints tell you about what is happening? Does the program flow as you expect?

okay the just shows that nobody is in room
and also I followed each step in serial monitor all reads is correct

#include <LiquidCrystal.h>
long unsigned  i;
long unsigned  j;
int count ;
int distance1 ;
int distance2 ;
int duration1 ;
int duration2 ;
const float vilocity = 0.0343;
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
void setup() {
  pinMode(2, OUTPUT);
  pinMode(4, INPUT);
  pinMode(3, OUTPUT);
  pinMode(5, INPUT);
  pinMode(6, OUTPUT);
  lcd.begin(16, 2);
  Serial.begin(9600);
}
int sensor1() {
  digitalWrite(2, HIGH);
  delayMicroseconds(2);
  digitalWrite(2, LOW);
  delayMicroseconds(2);
  duration1 = pulseIn(4, HIGH);
  distance1 = (duration1 / 2) * vilocity;
  return distance1;
}
int sensor2() {
  digitalWrite(3, HIGH);
  delayMicroseconds(2);
  digitalWrite(3, LOW);
  delayMicroseconds(2);
  duration2 = pulseIn(5, HIGH);
  distance2 = duration2 / 2 * vilocity;
  return distance2 ;
}
void loop() {
  sensor1();
  sensor2();
  Serial.print("sensor1 = "); Serial.println(sensor1());
  Serial.print("sensor2 = "); Serial.println(sensor2());
  if (distance1 < 15); {
    i = millis;
    while ((millis() - i) <= 3000) {
      sensor2();
      Serial.print("sensor2 = "); Serial.println(sensor2());
      if (distance2 < 15) {
        count++;
        break;
      }
    }
  } Serial.print("count = "); Serial.println(count);
  sensor2();
  if (distance2 < 15); {
    j = millis;
    while ((millis() - i ) <= 3000) {
      sensor1();
      Serial.print("sensor1 = "); Serial.println(sensor1());
      if (distance1 < 15) {
        count--;
        break;
      }
    }
  } Serial.print("count = "); Serial.println(count);
  if (count == 0) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.println ("NoBody in room");
  } if (count > 0) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.println("Lights are on");
    lcd.setCursor(0, 1);
    lcd.print("persons =");
    lcd.println(count);
    digitalWrite(6, HIGH);
  }
}

so I am working on a people counter project with two ultrasonic sensors
so I hooked up two ultrasonic sensors after i tested them successfully and connected them to a lcd 16*2 display all together to my nano board and I started coding
And my concept was if sensor 1 detected object within 15 cm then sensor two will still three seconds sensor searching for an object within the same range(15 cm) and if it detected it within the three seconds count variable will be increased by 1 and the otherwise (sensor 2 is followed by sensor 1 count decreased by 1 )
and this is the code :

#include <LiquidCrystal.h>
long unsigned  i;
long unsigned  j;
int count ;
int distance1 ;
int distance2 ;
int duration1 ;
int duration2 ;
const float vilocity = 0.0343;
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
void setup() {
  pinMode(2, OUTPUT);
  pinMode(4, INPUT);
  pinMode(3, OUTPUT);
  pinMode(5, INPUT);
  pinMode(6, OUTPUT);
  lcd.begin(16, 2);
  Serial.begin(9600);
}
int sensor1() {
  digitalWrite(2, HIGH);
  delayMicroseconds(2);
  digitalWrite(2, LOW);
  delayMicroseconds(2);
  duration1 = pulseIn(4, HIGH);
  distance1 = (duration1 / 2) * vilocity;
  return distance1;
}
int sensor2() {
  digitalWrite(3, HIGH);
  delayMicroseconds(2);
  digitalWrite(3, LOW);
  delayMicroseconds(2);
  duration2 = pulseIn(5, HIGH);
  distance2 = duration2 / 2 * vilocity;
  return distance2 ;
}
void loop() {
  sensor1();
  sensor2();
  Serial.print("sensor1 = "); Serial.println(sensor1());
  Serial.print("sensor2 = "); Serial.println(sensor2());
  if (distance1 < 15); {
    i = millis;
    while ((millis() - i) <= 3000) {
      sensor2();
      Serial.print("sensor2 = "); Serial.println(sensor2());
      if (distance2 < 15) {
        count++;
        break;
      }
    }
  } Serial.print("count = "); Serial.println(count);
  sensor2();
  if (distance2 < 15); {
    j = millis;
    while ((millis() - i ) <= 3000) {
      sensor1();
      Serial.print("sensor1 = "); Serial.println(sensor1());
      if (distance1 < 15) {
        count--;
        break;
      }
    }
  } Serial.print("count = "); Serial.println(count);
  if (count == 0) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.println ("NoBody in room");
  } if (count > 0) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.println("Lights are on");
    lcd.setCursor(0, 1);
    lcd.print("persons =");
    lcd.println(count);
    digitalWrite(6, HIGH);
  }
}

and it just shows on the lcd display that nobdy in room
it doesn't change whatever I did

if (distance1 < 15); <———<<<< Hummm you might want to check what the ; does.
There may be more than one instance :wink:
. . .

@omrcorc, please do not cross-post. Threads merged.