Hi everyone, I am currently working on a small project right now, which is a simple person counter that has 2 IR sensors. Here is a simple overview of my project. If a person passes the IR sensor 1 and later passes the IR sensor 2 it will considered to be entering and will increment the counter variable and vice versa (decrement if the person passes sensor 2 and sensor 1 later).. If the conditions are not satisfied, like the person only passes the sensor 1 or sensor 2 the counter will not respond. I've seen some YT tutorials online and adapted their codes, but unfortunately my code is not working (not incrementing and decrementing). I am troubleshooting it today, but I'm not making progress. Any help will be much appreciated. Thank you so much🧡
// Library for I2C communication
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
#define IR1 5
#define IR2 6
int Pcount = 0;
int pos = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(IR1, INPUT_PULLUP);
pinMode(IR2, INPUT_PULLUP);
digitalWrite(13, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PROJECT GENESIS");
delay(2000);
lcd.setCursor(0, 1);
}
void loop() {
// put your main code here, to run repeatedly:
int IR_state1 = digitalRead(IR1);
int IR_state2 = digitalRead(IR2);
display();
Serial.print(Pcount);
if (IR_state1 == LOW) {
delay(20);
while (IR_state1 == LOW) {
/*
Arrangement or placing ofsensors: while entering the room from outside, sensor1 will be
enPcounterd first and sensor2 will be next.
If pos=0, default value; No person is entering/leaving the room/hall
If pos=1, person is entering the room and crossed sensor1 (in)
If pos=2, person has entered the room after crossing both the sensors
If pos=3, person is going out of the room and crossed the sensor2 (out)
If pos=4, person has gone out of the room after crossing both the sensors
*/
if (pos == 0) {
pos = 1;
} else if (pos == 3) {
pos = 4;
}
}
}
if (pos == 4 && Pcount != 0) {
Pcount--;
display();
pos = 0;
}
if (IR_state2 == LOW) {
delay(20);
while (IR_state2 == LOW) {
if (pos == 1)
pos = 2;
else if (pos == 0)
pos = 3;
}
}
if (pos == 2) {
Pcount++;
display();
pos = 0;
} else if (pos == 4 && Pcount != 0) {
Pcount--;
display();
pos = 0;
}
else {
pos = 0;
}
delay(50);
}
void display() {
if (Pcount <= 0) {
//Pcount=0;
lcd.setCursor(0, 1);
lcd.print("No Passenger Yet");
}
else if (Pcount > 0 && Pcount < 10) {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("count: ");
lcd.setCursor(12, 1);
lcd.print(Pcount);
lcd.setCursor(13, 1);
lcd.print(" ");
} else {
lcd.setCursor(0, 1);
lcd.print("count: ");
lcd.setCursor(12, 1);
lcd.print(Pcount);
lcd.setCursor(14, 1);
lcd.print(" ");
}
}