// Sensor pin definitions
const int sensor1A = 2; // First sensor at Point A
const int sensor1B = 3; // Second sensor at Point A
const int sensor2A = 4; // First sensor at Point B
const int sensor2B = 5; // Second sensor at Point B
// Counter for the number of objects in the area
int objectsInArea = 0;
// Timing variables
unsigned long lastTime1A = 0;
unsigned long lastTime1B = 0;
unsigned long lastTime2A = 0;
unsigned long lastTime2B = 0;
unsigned long lastMotionTime = 0;
const unsigned long timeout = 500; // 500 milliseconds to complete passage
const unsigned long resetTimeout = 15000; // 60 seconds to reset counter
// State tracking for each sensor
bool lastState1A = LOW;
bool lastState1B = LOW;
bool lastState2A = LOW;
bool lastState2B = LOW;
void setup() {
// Initialize sensor pins as inputs
pinMode(sensor1A, INPUT);
pinMode(sensor1B, INPUT);
pinMode(sensor2A, INPUT);
pinMode(sensor2B, INPUT);
// Start the serial communication
Serial.begin(9600);
}
void loop() {
// Read sensor states
bool currentState1A = digitalRead(sensor1A);
bool currentState1B = digitalRead(sensor1B);
bool currentState2A = digitalRead(sensor2A);
bool currentState2B = digitalRead(sensor2B);
unsigned long currentTime = millis();
// Detect object entering the area from A to B
if (lastState1A == LOW && currentState1A == HIGH) {
lastTime1A = currentTime;
lastMotionTime = currentTime;
}
if (lastState1B == LOW && currentState1B == HIGH && (currentTime - lastTime1A) < timeout) {
objectsInArea++;
lastTime1A = 0; // Reset the timer
lastMotionTime = currentTime;
Serial.print("Objects in area: ");
Serial.println(objectsInArea);
Serial.println("Entry at A");
}
// Detect object entering the area from B to A
if (lastState2A == LOW && currentState2A == HIGH) {
lastTime2A = currentTime;
lastMotionTime = currentTime;
}
if (lastState2B == LOW && currentState2B == HIGH && (currentTime - lastTime2A) < timeout) {
objectsInArea++;
lastTime2A = 0; // Reset the timer
lastMotionTime = currentTime;
Serial.print("Objects in area: ");
Serial.println(objectsInArea);
Serial.println("Entry at B");
}
// Detect object leaving the area from B to A
if (lastState2B == LOW && currentState2B == HIGH) {
lastTime2B = currentTime;
lastMotionTime = currentTime;
}
if (lastState2A == LOW && currentState2A == HIGH && (currentTime - lastTime2B) < timeout) {
objectsInArea--;
lastTime2B = 0; // Reset the timer
lastMotionTime = currentTime;
Serial.print("Objects in area: ");
Serial.println(objectsInArea);
Serial.println("Exit at B");
}
// Detect object leaving the area from A to B
if (lastState1B == LOW && currentState1B == HIGH) {
lastTime1B = currentTime;
lastMotionTime = currentTime;
}
if (lastState1A == LOW && currentState1A == HIGH && (currentTime - lastTime1B) < timeout) {
objectsInArea--;
lastTime1B = 0; // Reset the timer
lastMotionTime = currentTime;
Serial.print("Objects in area: ");
Serial.println(objectsInArea);
Serial.println("Exit at A");
}
// Reset counter if no motion detected for resetTimeout duration
if (currentTime - lastMotionTime > resetTimeout) {
objectsInArea = 0;
Serial.println("Objects in area reset to 0 due to inactivity.");
lastMotionTime = currentTime; // Reset the last motion time to avoid repeated reset messages
}
// Update last states
lastState1A = currentState1A;
lastState1B = currentState1B;
lastState2A = currentState2A;
lastState2B = currentState2B;
// Small delay to avoid bouncing issues
delay(50);
}