Doggie Door Alarm


82 years old and just starting with Arduino sketches. Am trying to code for a flap door system (big flap so large dog can use it which means a human can also possibly use it to gain entry to my house). Flap will have a reed switch which when flap is in it's idle position is held by a magnet in the switch open position. Dog uses door and reed switch closes but only for up to say 2.5 seconds - in this scenario no alarm to result. If open for longer than 2.5 seconds then set an alarm pin on my Arduino Uno to sound the alarm for say 20 seconds then system resets for next event. I used pin 2 on the board as switch sensor input and pin 12 as alarm output and tried modifying the example sketch called Button that is available in the Arduino suite but I am so new at it I have failed to get the response I want.

Any help will be welcome.


Please post your full sketch using code tags (</> button on the toolbar).

// constants won’t change. They’re used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int alarmPin = 12; // the number of the ALARM pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the ALARM pin as an output:
pinMode(alarmPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);

void loop() {
// read the state of the pushbutton value:
int reading = digitalRead(buttonPin);

buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:

if (buttonState == HIGH) {
// turn alarm on:

digitalWrite(alarmPin, HIGH);

} else {
// turn ALARM off:
digitalWrite(alarmPin, LOW);
// delay(7000);