using two ultrasonic sensors

Hi

I am doing a project on automatic switching of room light, by counting the number of persons.

number of persons are stored in variable n.

i couldn’t find the error why the n is not changing.

can anybody suggest solution.

//code

#define t1 2
#define e1 3
#define t2 4
#define e2 5

long d, rs,ls,n;
void setup()
{
Serial.begin (9600);
pinMode(t1, OUTPUT);
pinMode(e1, INPUT);
pinMode(t2, OUTPUT);
pinMode(e2, INPUT);
}
void loop() {
SonarSensor(t1, e1);
rs = d;
SonarSensor(t2, e2);
ls = d;
Serial.print(rs);
Serial.print(" “);
Serial.print(ls);
Serial.print(” ");
Serial.print(n);
Serial.println();
if(ls<20){
wait1(n);
goto l1;
}
if(rs<20){
wait2(n);
goto l1;
}
l1:if(n>0){
digitalWrite(13, HIGH);
}
else if(n==0){
digitalWrite(13, LOW);
}
}

//-------------------------------

void SonarSensor(int trigPin,int echoPin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
d = pulseIn(echoPin, HIGH);
d = (d/58);
}

//-------------------------------

void wait1(int k)
{
l2: if(rs>20){
SonarSensor(t1, e1);
rs = d;
SonarSensor(t2, e2);
ls = d;
Serial.print(ls);
Serial.print(" “);
Serial.print(rs);
Serial.print(” ");
Serial.print(n);
Serial.println();
goto l2;
}
else if(rs<20){
k++;
}
}

//------------------

void wait2(int k)
{
l3: if(ls>20){
SonarSensor(t1, e1);
rs = d;
SonarSensor(t2, e2);
ls = d;
Serial.print(ls);
Serial.print(" “);
Serial.print(rs);
Serial.print(” ");
Serial.print(n);
Serial.println();
goto l3;
}
else if(ls<20){
k–;
}
}

sm.JPG

Could it be because you never give it a value?

Please use code tags.

Please don't use "goto".

Also, please use functions (Hint only uncompiled, untested)

unsigned long SonarSensor(int trigPin,int echoPin)
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  return pulseIn(echoPin, HIGH) / 58;
}

Also:

  rs = SonarSensor(t1, e1);
  ls = SonarSensor(t2, e2);

It isn't a good idea to take back-to-back readings from sonar sensors like this; you'll often find that distant echoes from the first sensor will be picked up by the second as nearby objects.