const int motionPin = 2; // Pin connected to motion sensor
const int ledPin = 13; // Pin connected to LED
const int bzrpin= 8; // Sets Buzzer PIN
const long timeoutTime = 10000; // Timeout in milliseconds (10 seconds)
int IOOut = 0;
unsigned long lastMotionTime = 0;
void setup() {
pinMode(motionPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(bzrpin, OUTPUT);
digitalWrite(ledPin, LOW); // Turn LED off initially
digitalWrite(bzrpin, LOW); // Turns Buzzer OFF Intially
}
void loop() {
int motionState = digitalRead(motionPin);
if (motionState == HIGH) {
lastMotionTime = millis(); // Reset timer when motion detected
}
if (millis() - lastMotionTime >= timeoutTime) {
IOOut = 1; // Sets IoOutput To Trigger
} else {
IOOut = 0; // Sets IoOutput To Detrigger
}
if (IOOut == 1) {
digitalWrite(ledPin, HIGH);
tone(bzrpin, 262, 250);
} else {
digitalWrite(ledPin, LOW);
digitalWrite(bzrpin, LOW);
}
}