0
#include <RTClib.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>


LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
int nowHr, nowMin, nowSec;
RTC_DS3231 rtc;
const int pin_contact= 6; 
const int buzzer = 9; 
//first alarm
int h1=15;
int m1=51;
int s1=0;

void timeScreen() {              // function to display Date and time in LCD screen

  DateTime now = rtc.now();             // take rtc time and print in display
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Time:");
    lcd.setCursor(6, 0);
    lcd.print(nowHr = now.hour(), DEC);
    lcd.print(":");
    lcd.print(nowMin = now.minute(), DEC);
    lcd.print(":");
    lcd.print(nowSec = now.second(), DEC);
    lcd.setCursor(0, 1);
    lcd.print("Date: ");
    lcd.print(now.day(), DEC);
    lcd.print("/");
    lcd.print(now.month(), DEC);
    lcd.print("/");
    lcd.print(now.year(), DEC);
    delay(500);

}


void setup()
{
Wire.begin();
   //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));            // uncomment this to set the current time and then comment in next upload when u set the tim
  rtc.adjust(DateTime(2021, 03, 27, 15, 50, 50));                // manual time set
  lcd.backlight();
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("hello");                                      // print a messege at startup
  lcd.setCursor(0, 1);
  lcd.print("world");
  delay(1000);
  timeScreen();
  delay(3000);
  lcd.clear();
 // Set buzzer - pin 9 as an output
    pinMode(pin_contact, INPUT);
   
 // Set buzzer - pin 9 as an output
   
    Serial.begin(9600);
}


void loop()
{
   timeScreen();
   lcd.backlight();
   at8am();
   Serial.begin(9600);
}

void at8am() {                      // function to start buzzing at 8am

  DateTime t = rtc.now();

  if (int(t.hour()) == h1 && int(t.minute()) == m1    ) {
        /////////////////////////////////////////////////////
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Time for ");
        lcd.setCursor(0, 1);
        lcd.print("breakfast");
   stopbuzzer();
         delay(5000);


        /////////////////////////////////////////////////////

      }
}


void stopbuzzer ()
{
   int buzzer_state=1;
      
  int stare_contact = digitalRead(pin_contact); 

  if (stare_contact == HIGH && buzzer_state==1) 
   {
buzzer_state=0; 
   }

    digitalWrite(buzzer,buzzer_state);

    }

Hello ! My intention is to stop an alarm, after a push button is pressed. In this code, a buzzer is ringing, when if condition is accomplished, and it stops when push button is pressed, but not forever, it keeps going to ring as long if conditions are available. How can the buzzer stop ringing after the push button is pressed?

10
  • No code as image please. You can copy and paste the code as text into the question and then format it correctly (selecting it and clicking the {} button) – chrisl Mar 29 at 8:39
  • And please provide a full compilable code – chrisl Mar 29 at 8:40
  • I edited now @chrisl – Biancaaa Mar 29 at 9:05
  • I guess with "if conditions are available" you mean, that the alarm will restart for 1 minute every 5 seconds. You only check for the hours and minutes being equal to the alarm time. How about also checking the seconds? Also: Why are you beginning Serial on every loop iteration? That doesn't make sense. – chrisl Mar 29 at 9:07
  • If you want to stop it “forever”, you may call exit(0) or abort() right after stopping it. – Edgar Bonet Mar 29 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.