How do i stop the buzzing action once door sensor is opened (LOW)

Hi everyone, im quite new to arduino and was interested in doing something like an alarm but the function is that when the alarm is == to the RTC timing, the buzzer will start to ring for a minute. But the thing is once i try with my codes, it will work when i seperate the door sensor but once i try and put it together again, it will ring again.. Please help me :frowning: (This would greatly help me as i can use the similar style to input my Sigfox Unabiz device in too)

Please look at the 'Void alarm1()'

#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("Alarm1");

  // 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();


 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
{
  if( Hor == 19 && (Min == 6 || Min == 7) && state == LOW) //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{         //Once user open the door sensor, the alarm will stop buzzing
    noTone(buz);
  }
  delay(200);
}

Have you forgotten to read the door switch input and change the value of the state variable in your program ?

HINT : the buzzer only sounds when its value is LOW

It is not a problem but it is easier to understand if you do not to mix the use of 0/1 and LOW/HIGH in the program.

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

How many of these need to be global?

 else{         //Once user open the door sensor, the alarm will stop buzzing
    noTone(buz);
  }

That comment is wrong. You do NOT want to use an else here. You want to turn the buzzer off if it is before 19:06 (it shouldn't be on), if it is after 19:08, OR if the door is open.

I assume that the comment associated with the uselessly-named state variable means that it is supposed to hold the state of a switch that is pressed when the door is closed.

But, nowhere do you actually read the state of the pin that the switch is connected to, if there is such a switch, so it is pointless to talk about shutting the noise off when the door is closed.

Sorry, all this seems very complicated for me.. will try and look back at the codes again

Ok so @PaulS,

Let's say if i were to remove the else since it will make it complicated.. i changed it to else if and the condition is almost the same except for detecting the state of the switch which is HIGH, but after changing, i tried to run and it is almost the same with my previous code..

i tried to off the switch, it works by stopping the buzzer but once i on the switch, it continued to sound the buzzer (It started and ended the alarm perfectly, did not exceed nor ring before the alarm)

But i just want the alarm to stop once i OFF the switch (Which i dont want it to sound anymore)

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);
}

But i just want the alarm to stop once i OFF the switch (Which i dont want it to sound anymore)

We have seen no proof that the value in state ever changes. state is STILL a lousy name. What is it supposed to be the state of?

Can you please describe in words how the system should work ?
When should the buzzer sound and for how long, for instance ?

ok.. How the system should work:

It is something like a reminder to Close the fridge during the timing

So E.g. when i set the alarm @ 10 pm,

it will ring at 10pm for 2 mins (Hence the min == x || min == y)

But i want the alarm (Buzzer) to stop ringing when i actually close the fridge (Regardless if it left 1 min on the alarm or not)

so yea.. that's the main point of the project.. Hope you understand..
@UKHeliBob

bump

bump

What have you tried?

You want, under some circumstances, to turn the noise maker on.

You want, under some circumstances, to turn the noise maker off.

Turning the noise maker off if it is not on is not a problem. Testing that it is on is simple, though, so turning it off only when it is on is easy.

Notice that turning the noise maker off has little to do with turning it on.

Create a function, makeSomeNoise(). Call it from loop().

Create another function, shutUpStupid(). Call it from loop().

In one, turn the noise maker on, it appropriate.

In the other, turn the noise maker off, if appropriate (and if needed).

This way, you won't be tempted to try to put too much stuff in one function, which seems to be the problem you are having now.