int sensorPin1 = 14; // IR sensor 1
int sensorPin2 = 13; // IR sensor 2
int ledPin1 = 19; // LED for person entry
int ledPin2 = 18; // LED for person exit
int counter = 0; // Initialize counter to track the number of people
void setup()
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(sensorPin1, INPUT);
pinMode(sensorPin2, INPUT);
Serial.begin(9600); // Start serial communication
}
void loop()
{
int sensor1State = digitalRead(sensorPin1); // Read the state of sensor 1
int sensor2State = digitalRead(sensorPin2); // Read the state of sensor 2
if (sensor1State == HIGH && sensor2State == LOW) // Person entered (sensor 1 triggered)
{
counter++; // Increase counter by 1
digitalWrite(ledPin1, HIGH); // Turn on LED 1 to indicate entry
digitalWrite(ledPin2, LOW); // Turn off LED 2
Serial.print("Person entered the room. Counter: ");
Serial.println(counter);
delay(500); // Short delay to avoid rapid state changes
}
else if (sensor1State == LOW && sensor2State == HIGH) // Person exited (sensor 2 triggered)
{
counter--; // Decrease counter by 1
digitalWrite(ledPin1, LOW); // Turn off LED 1
digitalWrite(ledPin2, HIGH); // Turn on LED 2 to indicate exit
Serial.print("Person exited the room. Counter: ");
Serial.println(counter);
delay(500); // Short delay to avoid rapid state changes
}
else // No person detected
{
digitalWrite(ledPin1, LOW); // Turn off LED 1
digitalWrite(ledPin2, LOW); // Turn off LED 2
Serial.println("No person detected");
delay(500); // Short delay
}
}