Good Day!
I'm still new in arduino programming but always try my best to learn something new.
Currently I am trying to make a bi-directional visitor counter using an Arduino Uno and 2 pieces of ultra sonic sensor (HC-SR04). I have managed to make an initial code for my project and manage to make it slightly work. My problem is that sometimes the total visitor/ guest doesn't increase nor decrease. I am suspecting that there are flaws in my codes. I tried troubleshooting using the serial monitor and I think that the problem is at this part of my control structure.
int control_1 = 0, control_2 = 0;
int oldcontrol_1 = 0, oldcontrol_2 = 0;
int total = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
//PERSON ENTERS THE ROOM
if (d_1 <= 48)
{
control_1 = 1;
}
else control_1 = 0;
if (control_1 != oldcontrol_1)
{
sensor(trig_2, echo_2, d_2);
if (d_2 <= 48)
{
control_2 = 1;
} else control_2 = 0;
if (control_2 != oldcontrol_2)
{
total = total + 1;
}
}
}
So if the first if statement is true, control_1's value becomes 1 right? So by observing the results from the serial monitor when the first if statement becomes true and control_1 = 1 and what should happen next is that the next if statement is executed because the condition becomes true right? Now what my conclusion on why the total count of visitors/ guests does not increase because once the first if statement becomes false count_1 now has the value of 0.
What I am thinking of next as a solution for my problem is to know when was the two sensor active. For example if a person enters the room. Sensor 1 was active last 1 second and sensor 2 was active last 0.5 second so "if sensor1lastActivetime > seonsor2lastActivetime then the total count of the visitor should increase" and vice versa for exit.
Is there a way for it to be coded just like that? If there are better ways that it can be coded so that it can function more accurately I'd be grateful for the help.
Here is my full code for my project and I'll be attaching a picture of my initial setup of my project.
#define trig_1 2
#define echo_1 3
#define trig_2 4
#define echo_2 5
float d_1, d_2;
int control_1 = 0, control_2 = 0;
int oldcontrol_1 = 0, oldcontrol_2 = 0;
int total = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
sensor(trig_1, echo_1, d_1);
delay(30);
sensor(trig_2, echo_2, d_2);
delay(30);
//PERSON ENTERS THE ROOM
if (d_1 <= 48)
{
control_1 = 1;
}
else control_1 = 0;
if (control_1 != oldcontrol_1)
{
sensor(trig_2, echo_2, d_2);
if (d_2 <= 48)
{
control_2 = 1;
} else control_2 = 0;
if (control_2 != oldcontrol_2)
{
total = total + 1;
}
}
//PERSON EXITS THE ROOM
if (d_2 <= 48)
{
control_2 = 1;
}
else control_2 = 0;
if (control_2 != oldcontrol_2)
{
sensor(trig_1, echo_1, d_1);
if (d_1 <= 48)
{
control_1 = 1;
} else control_1 = 0;
if (control_1 != oldcontrol_1)
{
total = total - 1;
}
}
Serial.print(control_1);
Serial.print(control_2);
Serial.print(" Total: ");
Serial.println(total);
}
void sensor(int trig, int echo, float &distance)
{
float duration;
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
digitalWrite(trig, LOW);
delay(20);
digitalWrite(trig, HIGH);
delay(50);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = (0.013464 * duration) / 2; //inches
}
Thank you for your kindness.