I am using 2 IR sensors on one UNO and can't change anything with that. My target is to set these on a door and check which one is triggered first. If both are triggered, I want to know if a person entered or left.
My plan with the code is to take a counter for entering and exit and assign 1 to each sensor. If the sensor with enter as its counter is triggered first(which is on the front side of the door) and then the exit one(comes after the previous sensor if we look from outside), it means someone entered and someone left if vice versa. From what I have till now, It works fine for the first trigger but after that, the count for the previous entry or exit doesn't become zero. This is my code:-
#include <LiquidCrystal.h>
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
const int enterSensor1 = 8;
const int enterSensor2 = 9;
const int exitSensor1 = 10;
const int exitSensor2 = 11;
int LED = 13;
int enter1 = HIGH;
int exit1 = HIGH;
void setup() {
lcd.begin(16, 2);
pinMode(LED, OUTPUT);
pinMode(enterSensor1, INPUT);
pinMode(exitSensor1, INPUT);
}
int enter11 = 0;
int exit11 = 0;
void loop() {
boolean change = false;
enter1 = digitalRead(enterSensor1);
exit1 = digitalRead(exitSensor1);
if (enter1 == LOW)
{
digitalWrite(LED, HIGH);
enter11++;
if (exit11 > 0) {
lcd.clear();
lcd.print("Someone Entered");
change = true;
}
exit11 = 0;
}
if (exit1 == LOW) {
digitalWrite(LED, HIGH);
if (enter11 > 0)
{
lcd.clear();
lcd.print("Someone Left");
change = true;
}
else
{
exit11++;
}
enter11 = 0;
}
if (change) {
enter11 = 0;
exit11 = 0;
}
delay(100);
}
My plan was that if it displays anything, the counters should become 0 and it should start checking again. The boolean variable 'change' becomes true if something is printed and makes my counters 0 again as they were in the beginning. Please correct my code.