Hello, I have been working on a project for about two weeks now and I am stuck on the most important part: the bi-directional counting. Any advice is more than helpful.
This is my project (arduino uno):
Wiring:
This is what i managed to program so far:
const int trigPin1 = 7;
const int echoPin1 = 8;
const int trigPin2 = 2;
const int echoPin2 = 4;
double duration1;
double distance1;
double duration2;
double distance2;
int people = 0;
int delaytime = 20;
double doorWidth = 20;
boolean in;
boolean out;
int sens1;
int sens2;
void setup() {
Serial.begin(9600); // starts the serial communication
pinMode(trigPin1, OUTPUT); // sets the trigPin1 as an Output
pinMode(echoPin1, INPUT); // sets the echoPin1 as an Input
pinMode(trigPin2, OUTPUT); // sets the trigPin2 as an Output
pinMode(echoPin2, INPUT); // sets the echoPin2 as an Input
}
double Sensor1() {
digitalWrite(trigPin1, LOW); // clears the trigPin1
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH); // sets the trigPin1 on HIGH state for 10 micro seconds
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH); // reads the echoPin1, returns the sound wave travel time in microseconds
distance1 = duration1 * 0.034 / 2; // calculating the distance
if (distance1 < doorWidth) {
sens1 = 1;
} else {
sens1 = 0;
}
return distance1;
return sens1;
}
void Sensor1Display() {
Serial.print("Distance1: ");
Serial.print(distance1);
Serial.print(" cm ");
Serial.print("sens1: ");
Serial.print(sens1);
Serial.print("\t");
delay(delaytime);
}
double Sensor2() {
digitalWrite(trigPin2, LOW); // clears the trigPin2
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH); // sets the trigPin2 on HIGH state for 10 micro seconds
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH); // reads the echoPin2, returns the sound wave travel time in microseconds
distance2 = duration2 * 0.034 / 2; // calculating the distance
if (distance2 < doorWidth) {
sens2 = 1;
} else {
sens2 = 0;
}
return distance2;
return sens2;
}
void Sensor2Display() {
Serial.print("Distance2: ");
Serial.print(distance2);
Serial.print(" cm ");
Serial.print("sens2: ");
Serial.print(sens2);
Serial.print("\t");
delay(delaytime);
}
double CountPeople() {
if (in == true) {
if ((sens1 == 1) && (sens2 <= 1)) {
people ++;
in = false;
}
}
if (sens1 == 0) {
in = true;
}
/* if (out == true && in == false) {
if ((sens2 == 1) && (sens1 <= 1)) {
people --;
out = false;
}
if (sens2 == 0) {
out = true;
}
}*/
return people;
}
void DisplayPeople() {
Serial.print("People count: ");
Serial.println(people);
}
void loop() {
Sensor1();
Sensor2();
Sensor1Display();
Sensor2Display();
CountPeople();
DisplayPeople();
}
I have not looked at your program in detail but this stands out as a mistake. Only one variable can be returned from a function. As soon as the return statement is encountered within a function the function ends.
In your case, however, as both variables are globals there is no need to return either of them anyway.
You can do bidirectional counting by noting the time from millis() when one sensor becomes active and comparing it with the time from millis() when the second sensor becomes active. Subtract one time from the other and you can not only count the number of people passing the sensors but also their direction.
I have removed the unnecessary returns, changed the double function to void and took only one variable for keeping directions (people walking in or else - out) as suggested. Though it still seems like I am stuck on some simple task which I can't find the solution to. I manage to add the people coming in and change the boolean variable to false so it doesn't continue adding up but then get stuck on the other direction. And if I try the other way then the boolean value's meaning messes up and it adds one when someone crosses and subtracts when someone leaves but doesn't do it continuously. Again any suggestions or comments would help me immensely.
const int trigPin1 = 7;
const int echoPin1 = 8;
const int trigPin2 = 2;
const int echoPin2 = 4;
double duration1;
double distance1;
double duration2;
double distance2;
int people = 0;
int delaytime = 20;
double doorWidth = 20;
boolean in = true;
double time1;
double time2;
void setup() {
Serial.begin(9600); // starts the serial communication
pinMode(trigPin1, OUTPUT); // sets the trigPin1 as an Output
pinMode(echoPin1, INPUT); // sets the echoPin1 as an Input
pinMode(trigPin2, OUTPUT); // sets the trigPin2 as an Output
pinMode(echoPin2, INPUT); // sets the echoPin2 as an Input
}
void Sensor1() {
digitalWrite(trigPin1, LOW); // clears the trigPin1
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH); // sets the trigPin1 on HIGH state for 10 micro seconds
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH); // reads the echoPin1, returns the sound wave travel time in microseconds
distance1 = duration1 * 0.034 / 2; // calculating the distance
if (distance1 < doorWidth) {
time1 = millis();
}
}
void Sensor1Display() {
Serial.print("Distance1: ");
Serial.print(distance1);
Serial.print(" cm ");
Serial.print("time1: ");
Serial.print(time1);
Serial.print("\t");
delay(delaytime);
}
void Sensor2() {
digitalWrite(trigPin2, LOW); // clears the trigPin2
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH); // sets the trigPin2 on HIGH state for 10 micro seconds
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH); // reads the echoPin2, returns the sound wave travel time in microseconds
distance2 = duration2 * 0.034 / 2; // calculating the distance
if (distance2 < doorWidth) {
time2 = millis();
}
}
void Sensor2Display() {
Serial.print("Distance2: ");
Serial.print(distance2);
Serial.print(" cm ");
Serial.print("time2: ");
Serial.print(time2);
Serial.print("\t");
delay(delaytime);
}
void CountPeople() {
/*if (in == true) {
if (((time1 - time2) < 0)) {
people ++;
in = false;
}
}
if (((time2 - time1) < 0)) {
in = true;
}
}*/
if (in == true) {
if (((time1 - time2) < 0)) {
people++;
in = false;
}
}
if (in == false) {
if (((time1 - time2) > 0)) {
people--;
in = true;
}
}
}
void DisplayPeople() {
Serial.print("People count: ");
Serial.println(people);
}
void loop() {
Sensor1();
Sensor2();
Sensor1Display();
Sensor2Display();
CountPeople();
DisplayPeople();
}