Gettering an alarm to remain on even after the door sensor has been triggered?

I have a magnetic door sensor/reed switch hooked up to a buzzer so it triggers when the door is open. However, In addition, I would like for the buzzer to still remain on even when the door is shut again.

Is anyone able to please advise on the best way to code this in?

Here is a copy of my void loop:

void loop(){
  
  if (digitalRead(switchReed)==HIGH){
    digitalWrite(ledOpen, LOW);
    digitalWrite(ledClose, HIGH);
    Serial.println("Your Door is Closed");
  }
  else {
    digitalWrite(ledOpen, HIGH);
    digitalWrite(ledClose, LOW);
    tone(buzzer, 1000, 500);
      delay(200);
      tone(buzzer, 1000, 500);
      delay(1000);
    tone(buzzer1, 1000, 500);
      delay(200);
      tone(buzzer1, 1000, 500);
      delay(1000);
    Serial.println("Your Door is Open");
  }
  delay(1);
}

What will stop the buzzer? Or, will it be on forever?

Railroader:
What will stop the buzzer? Or, will it be on forever?

the only way to stop it would be to push the reset button on the actual Arduino board or unplug it from the power source

Okey.
When You've activated the buzzer, and the LEDs, add this line:

while(true);

Railroader:
Okey.
When You've activated the buzzer, and the LEDs, add this line:

while(true);

Thanks for this! Just tried it out and it did keep the LED on, but the buzzer, however, turned off once the door shut.
Heres what it looks like now:

void loop(){
  
  if (digitalRead(switchReed)==HIGH){
    digitalWrite(ledOpen, LOW);
    digitalWrite(ledClose, HIGH);
    Serial.println("Your Door is Closed");
  }
  else {
    digitalWrite(ledOpen, HIGH);
    digitalWrite(ledClose, LOW);
    tone(buzzer, 1000, 500);
      delay(200);
      tone(buzzer, 1000, 500);
      delay(1000);
    tone(buzzer1, 1000, 500);
      delay(200);
      tone(buzzer1, 1000, 500);
      delay(1000);
    Serial.println("Your Door is Open");
    while(true);
  }
  delay(1);
}
  else {
    digitalWrite(ledOpen, HIGH);
    digitalWrite(ledClose, LOW);
    for(;;)
{
    tone(buzzer, 1000, 500);
      delay(200);
      tone(buzzer, 1000, 500);
      delay(1000);
    tone(buzzer1, 1000, 500);
      delay(200);
      tone(buzzer1, 1000, 500);
      delay(1000);
    Serial.println("Your fly is Open");
    }

Idahowalker:

  else {

digitalWrite(ledOpen, HIGH);
   digitalWrite(ledClose, LOW);
   for(;:wink:
{
   tone(buzzer, 1000, 500);
     delay(200);
     tone(buzzer, 1000, 500);
     delay(1000);
   tone(buzzer1, 1000, 500);
     delay(200);
     tone(buzzer1, 1000, 500);
     delay(1000);
   Serial.println("Your fly is Open");
   }

LEGEND! - Thank you so much now. It works perfectly!

iflow1:
LEGEND! - Thank you so much now. It works perfectly!

You are welcome, +1.

You can do this with a simple relay , you don’t need an Arduino .

Google relay latch

He is using the tone library.

Paul__B:
He is using the tone library.

Yeh. Hard to download into any relay....

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.