How to detect Door Sensor opening ONCE to stop alarm

Hi guys, i am back.. took a few weeks but still have no progress.. still stuck at the same spot..

#include <DS3231.h>
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
DS3231  rtc(SDA, SCL);
Time  t;
#define buz 11      //This is Buzzer that is plug into pin 11 (Change it accordingly to what you will be putting it in the arduino)
int Hor;            // This is declaring the alarm in Hours
int Min;            // This is declaring the alarm in Minutes
int Sec;            // This is declaring the alarm in Seconds
const int sensor = 10;    // Door sensor connected to Pin 10
int state; // 0 close - 1 open switch

void setup()
{  
  Wire.begin();
  rtc.begin();
  Serial.begin(9600);
  pinMode(buz, OUTPUT);
  lcd.begin(16,2);     
  lcd.setCursor(0,0);
  lcd.print("Alarm");
  lcd.setCursor(0,1);
  lcd.print("Test");
  // The following lines can be uncommented to set the date and time
  //rtc.setDOW(WEDNESDAY);     // Set Day-of-Week to SUNDAY
  //rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(1, 1, 2014);   // Set the date to January 1st, 2014
  delay(2000);
  pinMode(sensor, INPUT_PULLUP);
}

void loop()
{
  t = rtc.getTime();
  Hor = t.hour;
  Min = t.min;
  Sec = t.sec;
  lcd.setCursor(0,0);
  lcd.print("Time: ");
  lcd.print(rtc.getTimeStr());
  lcd.setCursor(0,1);
  lcd.print("Date: ");
  lcd.print(rtc.getDateStr());

  alarm1();
  alarm2();
  delay(1000); 
}

void Buzzer()
{
  digitalWrite(buz,HIGH);
  delay(500);
  digitalWrite(buz, LOW);
  delay(500);
}

void alarm1() //E.g. This is the first alarm for the medicine
{
  state = digitalRead(sensor);
  if( Hor == 20 && (Min == 11 || Min == 12) && state == 0) //Comparing the current time with the Alarm time
  {   
      Buzzer();
      lcd.clear();
      lcd.print("1st Alarm ON");
      lcd.setCursor(0,1);
      lcd.print("Morning Medicine");
     } 
  else if ( Hor == 20 && (Min == 11 || Min == 12) && state == 1)
  {
    noTone(buz);
  }
  delay(200);
} 

void alarm2()
{
  if( Hor == 19 && (Min == 48 || Min == 49) && state == 0) //Comparing the current time with the Alarm time
  {   
      Buzzer();
      lcd.clear();
      lcd.print("2nd Alarm ON");
      lcd.setCursor(0,1);
      lcd.print("Afternoon Medicine");
     } 
  else
  {         //Once user open the door sensor, the alarm will stop buzzing
    noTone(buz);
  }
  delay(200);
}

So the output for this code is:

  1. The alarm rang when the door sensor by default is closed
  2. When i take apart the door sensor, it stop the alarm (Temporary)
  3. But once i put it back, it continue to ring for the alarm duration (Which is 1 min)

What my goal of this project:
-When Door sensor is closed, appointed alarm can ring when it matches the RTC time.
-But once i take apart the door sensor, alarm event should end and wait / listen for next alarm (which is alarm2 in this case)
-Even if i put the door sensor back together, alarm1 shouldnt trigger or continue to ring..

I think that the problem with my codes is the logic.. i can explain but cant seem to form it to codes...

thanks for your time

Separate the steps. If an alarm needs to sound has no direct relation to time or door status. But also if the door was opened during the alarm.

So seperate it, pseudo code

if(time == alarmTime && time != lastCheckedTime){
alarmOn = true
}

if(alarmOn && doorBecomesOpen){
alarmOn = false
}

if(alarmOn){
soundAlarm(0
}

Also, when you start doing almost the same twice there is an easier way :wink:

AND link to the library please. There are tons of libraries and some even have the same name but work differently.