// Define the pins for the PIR sensor and LEDs
const int PIR_PIN = 2;
const int LED_1_PIN = 3;
const int LED_2_PIN = 4;
const int LED_3_PIN = 5;
// Define the LED sequence pattern
const int LED_SEQUENCE[] = {LED_1_PIN, LED_2_PIN, LED_3_PIN, LED_2_PIN, LED_1_PIN};
const int LED_SEQUENCE_LENGTH = sizeof(LED_SEQUENCE) / sizeof(LED_SEQUENCE[0]);
// Define the variable to store the current LED index
int currentLedIndex = 0;
void setup() {
// Initialize the PIR sensor and LEDs
pinMode(PIR_PIN, INPUT);
pinMode(LED_1_PIN, OUTPUT);
pinMode(LED_2_PIN, OUTPUT);
pinMode(LED_3_PIN, OUTPUT);
}
void loop() {
// Check if the PIR sensor is detecting something
if (digitalRead(PIR_PIN) == HIGH) {
// Turn on the current LED in the sequence
digitalWrite(LED_SEQUENCE[currentLedIndex], HIGH);
// Delay for a short time
delay(2000);
// Turn off the current LED in the sequence
digitalWrite(LED_SEQUENCE[currentLedIndex], LOW);
// Increment the LED index
currentLedIndex = (currentLedIndex + 1) % LED_SEQUENCE_LENGTH;
// If the LED index has reached the end of the sequence, reset it to 0
if (currentLedIndex == 0) {
delay(1000); // Wait for 1 second before resetting the sequence
}
} else {
// Reset the LED sequence
currentLedIndex = 0;
digitalWrite(LED_1_PIN, LOW);
digitalWrite(LED_2_PIN, LOW);
digitalWrite(LED_3_PIN, LOW);
}
// Wait for a short time before checking the PIR sensor again
delay(850);
}