Hi. I am trying to create an alarm clock which turns on and off a buzzer when hours, mins and seconds match. In addition I am trying to snooze the alarm (Turn the buzzer off for a period of time when the button is pressed and then back on again for a period of time).
I have set the snooze duration at 10 seconds in the code for testing purposes. I.e push button to cancel alarm then delay for 10 seconds and turn alarm back on again. (I am aware there needs to be another delay added to turn back off again.)
The issue I currently have is if I toggle the button state and keep it on when pressed the buzzer would cancel as anticipated, The delay would be implemented but obviously the loop keeps cycling round and turning the buzzer off during the delay.
Is there an easier way to achieve this or am I missing something simple?
#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() == 13 && now.second()==00)
digitalWrite(Alarm3Pin,LOW);
//if (now.day() == 23 && now.hour() == 06 && now.minute() == 28 &&now.second()==10)
{
//digitalWrite(Alarm3Pin,HIGH);
}
// 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);
}