Alarm Clock with snooze button. Is it possible?

Would you like a video demonstation?

Your grammar is so mixed up, I have no idea what you mean.

Theres obviously something i dont understand. Please enlighten me lol

Let me try again.

I would like the buzzer to sound when hours, mins and seconds match for a period of time, unless the button is pressed which turn the alarm off, wait a period of time, turn on again for a period of time and finally turn off.

Currently the alarm will sound when the time matches. When i press the button the alarm stops and nothing else happens from that point onwards.

You have described "doing several things at one time".
https://forum.arduino.cc/t/demonstration-code-for-several-things-at-the-same-time/217158

You want to do two things at once:

  1. poll the button
  2. run a 60 second timer

I don't believe you. It's not possible. Your code has implemented a long delay after the time match.

Unless, something really weird like the button is shorting out the power supply and resetting the machine...

Here's a link to my alarm clock project. It does everything you want & more. Help yourself HERE

No, thank you.

I did think of one way the button might work during an alarm. If the button causes the processor to crash or reset the buzzer will turn off and the sketch will start over. Some additional serial output would clarify the actual path taken. Try the sketch below with timestamps turned on in Serial Monitor.

#include <Wire.h>                   // for I2C communication
#include <LiquidCrystal_I2C.h>      // for LCD
#include <RTClib.h>                 // for RTC
#define Alarm3Pin 3

const int buttonPin = 4;

int buttonState = 0;


LiquidCrystal_I2C lcd(0x27, 16, 2); // create LCD with I2C address 0x27, 16 characters per line, 2 lines
RTC_DS3231 rtc;                     // create rtc for the DS3231 RTC module, address is fixed at 0x68


char daysOfTheWeek[7][12] =
{
  "Sunday",
  "Monday",
  "Tuesday",
  "Wednesday",
  "Thursday",
  "Friday",
  "Saturday"
};



void setup()
{
  pinMode(Alarm3Pin, OUTPUT);
  pinMode(buttonPin, INPUT);

  Serial.begin(9600);
  while (!Serial) ; // wait for Arduino Serial Monitor
  delay(200);

  Serial.println("Initialize rtc");

  lcd.init();       // initialize lcd
  lcd.backlight();  // switch-on lcd backlight
  lcd.clear();  // clear LCD display
  rtc.begin();       // initialize rtc

  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}



void loop()
{
  DateTime now = rtc.now();

  if (now.hour() == 20 && now.minute() == 58 && now.second() == 00)
  {
    Serial.println("Alarm starting.");
    digitalWrite(Alarm3Pin, HIGH);
    delay(60000);
    digitalWrite(Alarm3Pin, LOW);
    Serial.println("Alarm finished.");
  }

  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);


  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH)
  {
    Serial.println("Button pressed.");
    // turn Alarm3Pin off:
    digitalWrite(Alarm3Pin, LOW);
    delay(60000);
    digitalWrite(Alarm3Pin, HIGH);
    delay(60000);
    digitalWrite(Alarm3Pin, LOW);
    Serial.println("Button press done.");
  }


  if (!rtc.begin())
  {
    Serial.println("RTC Not Running.");
    lcd.print("error");
  }

  Serial.println("Displaying date and time.");
  
  if (now.day() < 10)
    lcd.print("0");
  lcd.print(now.day(), DEC);
  Serial.print(now.day(), DEC);
  lcd.print('/');
  Serial.print('/');
  if (now.month() < 10)
    lcd.print("0");
  if (now.month() < 10)
    Serial.print("0");
  lcd.print(now.month(), DEC);
  Serial.print(now.month(), DEC);
  lcd.print('/');
  Serial.print('/');
  lcd.print(now.year(), DEC);
  Serial.print(now.year(), DEC);
  lcd.print(" (");
  Serial.print(" (");
  lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
  Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  lcd.print(") ");
  Serial.print(") ");
  if (now.hour() < 10)
    lcd.print("0");
  lcd.print(now.hour(), DEC);
  Serial.print(now.hour(), DEC);
  lcd.print(':');
  Serial.print(':');
  if (now.minute() < 10)
    lcd.print("0");
  lcd.print(now.minute(), DEC);
  Serial.print(now.minute(), DEC);
  lcd.print(':');
  Serial.print(':');
  if (now.second() < 10)
    lcd.print("0");
  lcd.println(now.second(), DEC);
  Serial.println(now.second(), DEC);

  delay(1000); // delay 1 seconds
  lcd.clear();
  lcd.setCursor(0, 0);
}

Hi. I think you might be right about something weird happening possibly because it is wired incorrectly.

I understand it shouldn't work work but believe me it does.

It does not work in simulation however leading me to believe i have done something silly physically putting it together.

Thank you for your help and sorry for the attitude

That looks amazing. Sadly far beyond my capability at this point. Thank you for providing however

I have updated the code to perform required delay as millis. I think I am nearly there with what I want to achieve.

The one thing that seems to be a problem is when I push the button and turn the buzzer off the required delay is performed but it obviously loops back round and when it reads push button again it turns the buzzer off again during the planned snooze duration.

Am I missing something obvious?

#include <Wire.h>                   // for I2C communication

#include <LiquidCrystal_I2C.h>      // for LCD

#include <RTClib.h>                 // for RTC

#define Alarm3Pin 3

int BuzzerState = LOW;

unsigned long BuzzerStarted = 0;

const long Snooze = 10000;

const int buttonPin = 2;

int buttonState = 0;

LiquidCrystal_I2C lcd(0x27, 16, 2); // create LCD with I2C address 0x27, 16 characters per line, 2 lines

RTC_DS3231 rtc;                     // create rtc for the DS3231 RTC module, address is fixed at 0x68

char daysOfTheWeek[7][12] = {

  "Sunday",

  "Monday",

  "Tuesday",

  "Wednesday",

  "Thursday",

  "Friday",

  "Saturday"

};

void setup()

{

  pinMode(Alarm3Pin,OUTPUT);//

  pinMode(buttonPin, INPUT);

  Serial.begin(9600);

  Serial.println("Initialize rtc");;

  while (!Serial) ; // wait for Arduino Serial Monitor

 

  lcd.init();       // initialize lcd

  lcd.backlight();  // switch-on lcd backlight

  lcd.clear();  // clear LCD display

  rtc.begin();       // initialize rtc

  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

 

}

void loop()

{

  DateTime now = rtc.now();

if (now.hour() == 11 && now.minute() == 50 && now.second()==00)

   

      digitalWrite(Alarm3Pin,HIGH);

     

if (now.hour() == 11 && now.minute() == 55 && now.second()==00)

   

      digitalWrite(Alarm3Pin,LOW);

 

    //if (now.day() == 23 && now.hour() == 06 && now.minute() == 28 &&now.second()==10)

    {

      //digitalWrite(Alarm3Pin,HIGH);

      //delay(3000);

      //digitalWrite(Alarm3Pin,LOW);

    }

        // read the state of the pushbutton value:

    buttonState = digitalRead(buttonPin);

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

    if (buttonState == HIGH)

   

    // turn Alarm3Pin off:

    digitalWrite(Alarm3Pin, LOW);

unsigned long currentMillis = millis();

  if (currentMillis - BuzzerStarted >= Snooze) {

    BuzzerStarted = currentMillis;

    if (BuzzerState == LOW) {

      BuzzerState = HIGH;

    } else {

      BuzzerState = LOW;

    }

    digitalWrite(Alarm3Pin, BuzzerState);

  }

 

  if (!rtc.begin()) lcd.print("error");

 

  if (now.hour() < 10) lcd.print("0");

  lcd.print(now.hour(), DEC);

  Serial.print(now.hour(), DEC);

  lcd.print(':');

  Serial.print(':');

  if (now.minute() < 10) lcd.print("0");

  lcd.print(now.minute(), DEC);

  Serial.print(now.minute(), DEC);

  lcd.print(':');

  Serial.print(':');

  if (now.second() < 10) lcd.print("0");

  lcd.println(now.second(), DEC);

  Serial.println(now.second(), DEC);

  delay(1000); // delay 1 seconds

  lcd.clear();

  lcd.setCursor(0, 0);

}

Don't just check for button down. After its down, wait for it to go back up before continuing.

How do you do that? I'm sorry for being stupid. I genuinely don't know

Read the state of the switch and then check it. Logic is

if switch is down
{
  register a press
  while switch is down
  {
  do nothing
  }
}

Generally, there are two steps to programming:

  1. Figure out how to do it
  2. Translate that into code

Don't try to skip #1.

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