const int pirPin = 2; // PIR sensor output pin
const int ledPin = 13; // LED pin
int motionCount = 0;
unsigned long firstMotionTime = 0;
void setup() {
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int motionDetected = digitalRead(pirPin);
if (motionDetected == HIGH) {
Serial.println("Motion Detected!");
// First detection time tracking
if (motionCount == 0) {
firstMotionTime = millis();
}
motionCount++;
if (motionCount > 3 && (millis() - firstMotionTime) <= 60000) {
// High alert: blink LED continuously
for (int i = 0; i < 30; i++) {
digitalWrite(ledPin, HIGH);
delay(250);
digitalWrite(ledPin, LOW);
delay(250);
}
motionCount = 0; // Reset count after alert
} else {
// Normal alert: LED on for 5 seconds
digitalWrite(ledPin, HIGH);
delay(5000);
digitalWrite(ledPin, LOW);
}
}
if ((millis() - firstMotionTime) > 60000) {
motionCount = 0;
}
}