#define PIR_PIN 16
#define LED_PIN 15
unsigned long lastMotionTime = 0;
const unsigned long timeoutMs = 10000;
void setup() {
pinMode(PIR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
}
void loop() {
int motion = digitalRead(PIR_PIN);
if (motion == HIGH) {
digitalWrite(LED_PIN, HIGH); // có chuyển động thì bật LED
lastMotionTime = millis(); // cập nhật thời điểm cuối cùng phát hiện chuyển động
} else {
// nếu đã quá 10 giây từ lần phát hiện cuối thì tắt LED
if (millis() - lastMotionTime >= timeoutMs) {
digitalWrite(LED_PIN, LOW);
}
}
delay(50);
}