int Sinput = A1; // Sensor input signal pin (A1)
int Buz = 3; // Buzzer output pin (digital pin 3)
unsigned long previousMillis = 0; // Store the last time the buzzer was triggered
const long interval = 2000; // 2 seconds delay for the buzzer
void setup() {
pinMode(Sinput, INPUT); // Set sensor pin as input
pinMode(Buz, OUTPUT); // Set buzzer pin as output
}
void loop() {
unsigned long currentMillis = millis(); // Get current time
// Check if the sensor input is LOW (inactive state)
if(digitalRead(Sinput) == LOW) {
// If 2 seconds have passed since the last buzzer trigger, turn it on
if(currentMillis - previousMillis >= interval) {
digitalWrite(Buz, HIGH); // Turn on buzzer
}
} else {
// If the sensor input is HIGH (active state), turn off the buzzer
digitalWrite(Buz, LOW); // Turn off buzzer
}
}