Bi-Directional Counter using two Ultrasonic Sensors

I am trying to make Bi-Directional Counter using two Ultrasonic sensor.The count will be shown on LCD Screen if count>0 then the lights and other electronics item will Turn ON otherwise if count<0 then they will be Turned OFF.Basically i'm making a classroom automation system in which if a person enters in class then the count will get increment and then the lights and fan inside the class will get Turn ON. Similarly when a person leaves from class then the count will decrease and if no one is there inside class then all lights and fan will Turn OFF.In this i am using two ultrasonic sensor which are placed next to each other facing in same direction and then it is connected to a arduino . I have made two type of a code which i have attached with this post:
In both of them i facing some problems:
1.The value of count gets increment when a person enters(Passes by both Ultrasonic sensors) but when a persons exit the room then it is not doing the decrement in the count.
2.If a person is passing after a particular delay then only increment is happening otherwise no effect.

Please help me out with this.

#include<LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);
const int trigPin1 = 5;
const int echoPin1 = 4;

const int trigPin2 = 7;
const int echoPin2 = 6;

double duration1;
double distance1;
double duration2;
double distance2;

int people = 0,count=0;

int delaytime = 20;

double doorWidth = 20;

boolean current_state1=false,previous_state1=false;
boolean current_state2=false,previous_state2=false;

int relay=0;

void setup()
{
Serial.begin(9600);
lcd.begin(16,2);
lcd.print("Visitor Counter");
Serial.println("Visitor Counter");
delay(2000);

pinMode(trigPin1, OUTPUT);
pinMode(echoPin1,INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(relay, OUTPUT);
digitalWrite(relay,LOW);

lcd.clear();
lcd.print("Person In Room:");
Serial.println("Person in room:");

}
void DisplayPeople()
{
if (people > 0)
{
digitalWrite(relay, LOW);
}

else if(people <= 0)
{
digitalWrite(relay, LOW);
}
Serial.println("People count: ");
delay(1000);
lcd.setCursor(0,1);
delay(100);
lcd.print(people);
Serial.print("People=");
Serial.print(people);
Serial.print("\n");
delay(100);
}

void loop()
{
myloop: while(1)
{
Serial.println("Inside while");
Serial.print("Person Inside:");
Serial.println(count);
people=count;
DisplayPeople();

digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);

duration1 = pulseIn(echoPin1, HIGH);
//Serial.print("Duration1 of US1:");
// Serial.println(duration1);
distance1 = duration1 * 0.034 / 2;
Serial.print("Distance1 of US1:");
Serial.println(distance1);
if(distance1<=10)
{
Serial.println("Inside while if distance1<=10");
current_state1= true;

break;
}

digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);

duration2 = pulseIn(echoPin2, HIGH);
distance2 = duration2 * 0.034 / 2;

Serial.print("Distance1 of US2:");
Serial.println(distance2);

if(distance2<=10)
{
Serial.println("Inside while if distance2<=10");
current_state2=true;

break;
}
}
if(current_state1!=previous_state1)
{
if(current_state1==true)
{
double dis2=0,dur2=0;
delay(50);
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);

dur2 = pulseIn(echoPin2, HIGH);
dis2 = dur2 * 0.034 / 2;
Serial.print("Distance2 of US2 inside CS1:");
Serial.println(dis2);
if(dis2<=10)
{
Serial.println("Inside dis2<=10 count++");
count++;

}
goto myloop;
}
}

if(current_state2!=previous_state2)
{
if(current_state2==true)
{
double dis1=0,dur1=0;
delay(50);
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);

dur1 = pulseIn(echoPin2, HIGH);
dis1 = dur1 * 0.034 / 2;
Serial.print("Distance1 of US1 inside CS2:");
Serial.println(dis1);
if(dis1<=10)
{
Serial.println("Inside if dis1<=10,count--");
count--;

}
if(count<0)
{
count=0;
}
goto myloop;
}
}

/lcd.print("Person in room:"+count);
Serial.print("Person in room count:");
Serial.println(count);
people=count;
DisplayPeople();
/
}

freshcode.ino (3.96 KB)

Overall_sample.ino (770 Bytes)

Hi. Please read the forum guide in the sticky post, then modify you post above so that the code is posted correctly according to the guide. Also please explain more about your project, your explanation so far does not seem to be clear or complete. For example when should the counter increment, and when should it decrement? Why have you posted your code in the post and then attached two more .ino files? Are any 2 of the 3 codes the same?

where do you set previous_state [12] within the condition where the current_state != previous_state

also, there's no need for a goto. loop() is repeatedly called by the arduino main(). you can simply return instead of doing a goto.

myloop: while(1) :o

pulseIn returns unsigned long, not double.

Scrap the LCD for now, and just use print until you've got your code working

I have modified my question again please have a look at both .ino files attached with it. Thank You

now i have to remember what you previously wrote ? .............

arpitskit19:
I have modified my question again please have a look at both .ino files attached with it. Thank You

Good luck with your project.

No you don't have to remember what i wrote before just give suggestion according to above code.

PaulRB:
Good luck with your project.

Please help if you can....