// Pin assignments for IR sensors and relay module
int irSensorEntry = 9;
int irSensorExit = 8;
int relayPin = 13;
// Variables to keep track of the number of people
int count = 0;
bool personInside = false;
void setup() {
// Initialize IR sensor pins as input and relay pin as output
pinMode(irSensorEntry, INPUT);
pinMode(irSensorExit, INPUT);
pinMode(relayPin, OUTPUT);
// Turn off the lights to start
digitalWrite(relayPin, LOW);
// Initialize serial communication for debugging
Serial.begin(9600);
}
void loop() {
// Read the state of the IR sensors
int entryState = digitalRead(irSensorEntry);
int exitState = digitalRead(irSensorExit);
// If someone enters, turn on the lights and increment the count
if (entryState == HIGH && !personInside) {
digitalWrite(relayPin, HIGH);
count++;
personInside = true;
Serial.print("Person entered. Count = ");
Serial.println(count);
}
// If someone exits, turn off the lights and update the personInside flag
if (exitState == HIGH && personInside) {
digitalWrite(relayPin, LOW);
personInside = false;
Serial.println("Person exited.");
}
// Wait a short amount of time to debounce the sensors
delay(100);
}