#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2); // set the LCD address, columns, and rows
int enterSensorPin = 2; // select the input pin for enter PIR sensor
int exitSensorPin = 3; // select the input pin for exit PIR sensor
int occupancy = 0; // initialize the occupancy count
int enterState = LOW; // initialize the enter PIR sensor state
int exitState = LOW; // initialize the exit PIR sensor state
void setup() {
lcd.init(); // initialize the LCD screen
lcd.backlight(); // turn on the LCD backlight
pinMode(enterSensorPin, INPUT); // set enter PIR sensor pin as an input
pinMode(exitSensorPin, INPUT); // set exit PIR sensor pin as an input
Serial.begin(9600); // initialize serial communication
}
void loop() {
int enterMotion = digitalRead(enterSensorPin); // read enter PIR sensor value
int exitMotion = digitalRead(exitSensorPin); // read exit PIR sensor value
if (enterMotion == HIGH && enterState == LOW) { // if motion is detected at enter sensor and it was not previously detected
occupancy++; // increase the occupancy count
enterState = HIGH; // set enter sensor state to detected
exitState = LOW; // reset exit sensor state
} else if (exitMotion == HIGH && exitState == LOW) { // if motion is detected at exit sensor and it was not previously detected
occupancy--; // decrease the occupancy count
exitState = HIGH; // set exit sensor state to detected
enterState = LOW; // reset enter sensor state
}
lcd.setCursor(0, 0); // set the cursor to the first row, first column
lcd.print("Occupancy: "); // display the occupancy count
lcd.print(occupancy);
Serial.print("Occupancy: "); // send the occupancy count to the serial monitor
Serial.println(occupancy);
delay(1000); // wait for a second before reading the sensors again
}