ultrasonic people counter not work

Pls I am working with 2 ultrasonic sensors. I want to use for people counter but it is not working. Pls help me out:

Code:

#include <NewPing.h>
//Ping sensors
#define trigPin1 2
#define echoPin1 3
#define trigPin2 4
#define echoPin2 5

unsigned long duration1 = 0;
unsigned long duration2 = 0;

int SensorA;
int SensorB;

void setup() {
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
}

//method to measure distance of ping sensor1
void measureSensorA() {
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
digitalWrite(trigPin1, HIGH);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
delayMicroseconds(1000);
}

//method to measure distance of ping sensor2
void measureSensorB() {
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
digitalWrite(trigPin2, HIGH);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
delayMicroseconds(1000);
}

//method to calculate distance based on information recieved with measure distance -method for ping sensor1
int calculateSensorA() {
measureSensorA();
duration1 = duration1 / 2;
SensorA = int(duration1 / 29);

}

//method to calculate distance based on information recieved with measure distance -method for ping sensor2
void calculateSensorB() {
measureSensorB();
duration2 = duration2 / 2;
SensorB = int(duration2 / 29);

}

//loop for calculating how many peopleInRooms have passed
int peopleInRoom;
void loop() {
calculateSensorA();
calculateSensorB();

if (SensorA <= 10 && SensorB <=10) { //if sensorA detects ping before sensorB
peopleInRoom++;
}

if (SensorB <= 10 && SensorA <=10) { //if sensorB detects ping before sensorA
peopleInRoom–;
}
Serial.print("People ");
Serial.println(peopleInRoom );
delay(500);

}

You really should have read the How to use this forum - please read post at the top of the index page and How to use this forum before posting.

ie Your code and any error messages should always be placed between code tags. Posting it inline as you have done makes it much harder to read or copy and paste for diagnosis.

It’s still not too late to edit your post and do this. You’ll make potential helpers much happier. :slight_smile:

Edit: Why did you include the “NewPing” library then not use it?

Edit2: These definitely don’t do what your comments say.
The first will add 1 to the number of people in the room if there is someone within 10cm of both sensors at the same time, then immediately after that the second one will subtract 1 from the number of people in the room. (The values won’t have changed.) :-

if (SensorA <= 10 && SensorB <= 10)  //if sensorA detects ping before sensorB
    {
        peopleInRoom++;
    }

    if (SensorB <= 10 && SensorA <= 10)  //if sensorB detects ping before sensorA
    {
        peopleInRoom--;
    }

You need to rethink your code.

You do NOT need two functions to read the two sensors. You don't see a bunch of functions to read from digital pins, do you? The digitalRead() function takes an argument. Your ONE function should, too. Two of them, actually.