#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal.h>
#define LCD_RS 7
#define LCD_EN 8
#define LCD_D4 9
#define LCD_D5 10
#define LCD_D6 11
#define LCD_D7 12
#define BUTTON_PIN 2
#define BUZZER_PIN 3
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // RS, EN, D4, D5, D6, D7
RTC_DS3231 rtc;
// Adjust this value according to your time zone offset
const int timeZoneOffset = 0; // For example, if you're in GMT+0, set it to 0
bool alarmTriggered = false;
void setup() {
Serial.begin(9600);
Wire.begin();
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
pinMode(BUTTON_PIN, INPUT_PULLUP); // Configure button pin as input with pull-up resistor
pinMode(BUZZER_PIN, OUTPUT); // Configure buzzer pin as output
lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
lcd.clear(); // Clear the LCD screen
lcd.print("Time: ");
}
void loop() {
if (Serial.available() > 0) {
setTimeOrAlarmFromSerial();
}
DateTime now = rtc.now() + TimeSpan(0, timeZoneOffset, 0, 0); // Adjust time for time zone offset
// Convert hour to 12-hour format
int hour12 = now.hour() % 12;
if (hour12 == 0) {
hour12 = 12; // Set 12 instead of 0 for 12 AM
}
// Determine AM/PM
String ampm = now.hour() < 12 ? "AM" : "PM";
lcd.setCursor(6, 0); // Move cursor to position (column 7, row 1)
printDigits(hour12); // Print hour
lcd.print(':');
printDigits(now.minute()); // Print minute
lcd.print(':');
printDigits(now.second()); // Print second
lcd.print(' ');
lcd.print(ampm);
lcd.setCursor(0, 1); // Move cursor to position (column 0, row 2)
printDigits(now.day()); // Print day
lcd.print('/');
printDigits(now.month()); // Print month
lcd.print('/');
lcd.print(now.year()); // Print year
if (alarmTriggered) {
digitalWrite(BUZZER_PIN, HIGH); // Turn on buzzer
if (digitalRead(BUTTON_PIN) == LOW) { // Check if button is pushed
alarmTriggered = false; // Reset alarm triggered flag
digitalWrite(BUZZER_PIN, LOW); // Turn off buzzer
rtc.disableAlarm(1, DS3231_A1_Minute); // Disable the alarm
}
}
delay(1000); // Update every second
}
void printDigits(int digits) {
// Add leading 0 if the number is less than 10
if (digits < 10)
lcd.print('0');
lcd.print(digits);
}
void setTimeOrAlarmFromSerial() {
String input = Serial.readStringUntil('\n');
if (input.startsWith("alarm ")) {
input.remove(0, 6); // Remove "alarm " from the input string
int hour, minute;
char ampm[3];
if (sscanf(input.c_str(), "%d:%d %2s", &hour, &minute, ampm) == 3) {
if (strcmp(ampm, "AM") == 0 || strcmp(ampm, "PM") == 0) {
hour = (hour % 12) + ((strcmp(ampm, "PM") == 0 && hour < 12) ? 12 : 0);
rtc.writeSqwPinMode(DS3231_OFF); // Disable the SQW pin
rtc.setAlarm1(DateTime(rtc.now().year(), rtc.now().month(), rtc.now().day(), hour, minute, 0), DS3231_A1_Hour);
rtc.disableAlarm(1, DS3231_A1_Minute); // Enable alarm 1
Serial.println("Alarm set successfully!");
alarmTriggered = false; // Reset alarm triggered flag
} else {
Serial.println("Invalid time format. Please use AM or PM.");
}
} else {
Serial.println("Invalid time format. Please use HH:MM AM/PM format.");
}
}
}