#define PIR_PIN 2 // PIR sensor connected to digital pin 2
#define LED_PIN 13 // LED connected to pin 13
unsigned long motionTimestamps[10]; // Store timestamps of recent motions
int motionCount = 0;
bool highAlert = false;
unsigned long lastMotionTime = 0;
unsigned long ledOnTime = 0;
void setup() {
pinMode(PIR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
unsigned long currentTime = millis();
int motionDetected = digitalRead(PIR_PIN);
// Handle new motion
if (motionDetected == HIGH && currentTime - lastMotionTime > 1000) {
lastMotionTime = currentTime;
Serial.println("Motion detected!");
// Turn on LED for 5 seconds
digitalWrite(LED_PIN, HIGH);
ledOnTime = currentTime;
// Add to motion history
motionTimestamps[motionCount % 10] = currentTime;
motionCount++;
// Check for high alert condition
int recentCount = 0;
for (int i = 0; i < motionCount && i < 10; i++) {
if (currentTime - motionTimestamps[i] <= 60000) {
recentCount++;
}
}
if (recentCount > 3) {
highAlert = true;
Serial.println("High Alert Mode Activated!");
}
}
// Handle LED timing (keep it ON for 5 seconds unless in high alert)
if (!highAlert && digitalRead(LED_PIN) == HIGH && currentTime - ledOnTime >= 5000) {
digitalWrite(LED_PIN, LOW);
}
// High alert mode: blink LED continuously
if (highAlert) {
if ((currentTime / 500) % 2 == 0) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
}
}