#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <RTClib.h>
// Pin definitions
const int menuButtonPin = 2;
const int upButtonPin = 3;
const int downButtonPin = 4;
const int setButtonPin = 5;
const int relayPin = 6;
// Create LCD and RTC objects
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust I2C address if needed
RTC_DS3231 rtc;
struct AlarmEvent {
uint8_t hour;
uint8_t minute;
};
AlarmEvent alarms[10]; // Array to store 10 alarm events
int currentMenu = 0; // 0: Time, 1: Alarm Settings
int selectedAlarm = 0; // Index of selected alarm event
bool isSettingTime = true;
bool settingHour = true; // Toggle between hour and minute settings
// Variables to hold the last displayed information
String lastDisplay = "";
int lastAlarm = -1;
// Buzzer control variables
bool buzzerOn = false;
unsigned long buzzerStartTime = 0;
const unsigned long buzzerDuration = 5000; // Buzzer duration in milliseconds
// Add a new variable to store the alarm duration
unsigned long alarmDuration = 10000; // 10 seconds
unsigned long alarmStartTime = 0;
void setup() {
pinMode(menuButtonPin, INPUT_PULLUP);
pinMode(upButtonPin, INPUT_PULLUP);
pinMode(downButtonPin, INPUT_PULLUP);
pinMode(setButtonPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW);
lcd.init();
lcd.backlight();
if (!rtc.begin()) {
lcd.clear();
lcd.print("RTC not found!");
while (1);
}
// Load alarms from EEPROM
for (int i = 0; i < 10; i++) {
alarms[i].hour = EEPROM.read(i * 2);
alarms[i].minute = EEPROM.read(i * 2 + 1);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" PISD SHS");
lcd.setCursor(0,1);
lcd.print("AUTO BELL ALARM");
delay(2000);
}
void loop(){
// Read button states
bool menuButtonState = digitalRead(menuButtonPin) == LOW;
bool upButtonState = digitalRead(upButtonPin) == LOW;
bool downButtonState = digitalRead(downButtonPin) == LOW;
bool setButtonState = digitalRead(setButtonPin) == LOW;
// Handle menu navigation
if (menuButtonState) {
digitalWrite(relayPin, LOW);
delay(200); // Debounce
currentMenu = (currentMenu + 1) % 2;
if (currentMenu == 1) { // When in Alarm Settings menu, reset to first alarm
selectedAlarm = 0;
}
while (digitalRead(menuButtonPin) == LOW); // Wait until button is released
}
// Handle setting change
if (setButtonState) {
delay(200); // Debounce
if (currentMenu == 0) {
isSettingTime = !isSettingTime;
} else {
if (settingHour) {
settingHour = false; // Switch to minute setting
} else {
// Save alarm settings
EEPROM.write(selectedAlarm * 2, alarms[selectedAlarm].hour);
EEPROM.write(selectedAlarm * 2 + 1, alarms[selectedAlarm].minute);
settingHour = true; // Reset to hour setting for the next alarm
selectedAlarm++; // Move to the next alarm
if (selectedAlarm >= 10) {
// All alarms set
lcd.clear();
lcd.print("ALARM SAVED!");
delay(2000); // Display the message for 2 seconds
selectedAlarm = 0; // Reset to the first alarm
currentMenu = 0; // Go back to time display
}
}
}
while (digitalRead(setButtonPin) == LOW); // Wait until button is released
}
// Handle up/down buttons
if (upButtonState) {
delay(200); // Debounce
if (currentMenu == 0) {
if (isSettingTime) {
// Update time settings here (code for updating current time should be added)
}
} else {
// Adjust alarm settings
if (settingHour) {
alarms[selectedAlarm].hour = (alarms[selectedAlarm].hour + 1) % 24;
} else {
alarms[selectedAlarm].minute = (alarms[selectedAlarm].minute + 1) % 60;
}
}
while (digitalRead(upButtonPin) == LOW); // Wait until button is released
}
if (downButtonState) {
delay(200); // Debounce
if (currentMenu == 0) {
if (isSettingTime) {
// Update time settings here (code for updating current time should be added)
}
} else {
// Adjust alarm settings
if (settingHour) {
alarms[selectedAlarm].hour = (alarms[selectedAlarm].hour - 1 + 24) % 24;
} else {
alarms[selectedAlarm].minute = (alarms[selectedAlarm].minute - 1 + 60) % 60;
}
}
while (digitalRead(downButtonPin) == LOW); // Wait until button is released
}
// Update LCD based on menu
DateTime now = rtc.now();
String displayText;
if (currentMenu == 0) {
if (isSettingTime) {
displayText = "Time: ";
displayText += format12Hour(now.hour()) + ":" + formatTime(now.minute()) + " " + (now.hour() < 12 ? "AM" : "PM");
lcd.setCursor(0,1); // Set cursor to second line
lcd.print(displayText); // Print the time on the second line
} else {
displayText = "Time: ";
displayText += format12Hour(now.hour()) + ":" + formatTime(now.minute()) + " " + (now.hour() < 12 ? "AM" : "PM");
lcd.setCursor(0,1); // Set cursor to second line
lcd.print(displayText); // Print the time on the second line
}
lcd.setCursor(0, 0); // Set cursor to first line
lcd.print("Date: ");
lcd.println(formatDate(now)); // Print the date on the first line
} else {
lcd.setCursor(0, 0);
lcd.print("SET ALARM " + String(selectedAlarm + 1)); // Display Alarm setting
String timeText = format12Hour(alarms[selectedAlarm].hour) + ":" + formatTime(alarms[selectedAlarm].minute);
String amPm = alarms[selectedAlarm].hour < 12 ? "AM" : "PM";
lcd.setCursor(0, 1); // Set cursor to second line
lcd.print(timeText + " " + amPm); // Print the time with AM/PM on the second line
if (settingHour) {
lcd.print(" Hour "); // Append "(Hour)" if setting the hour
} else {
lcd.print(" Minute "); // Append "(Minute)" if setting the minute
}
}
// Only update the display if the text has changed
if (displayText != lastDisplay || selectedAlarm != lastAlarm) {
lcd.clear();
lcd.print(displayText);
lastDisplay = displayText;
lastAlarm = selectedAlarm;
}
// Check alarms and control buzzer
if (!buzzerOn) {
for (int i = 0; i < 10; i++) {
if (alarms[i].hour == now.hour() && alarms[i].minute == now.minute()) {
buzzerOn = true;
alarmStartTime = millis(); // Store the start time of the alarm
lcd.setCursor(0,1);
lcd.print(" Alarm is ON..");
digitalWrite(relayPin, HIGH); // Turn on the buzzer
delay(10000);
digitalWrite(relayPin, LOW);
delay(50000);
break;
}
}
} else {
// Check if the alarm has been active for the specified duration
if (millis() - alarmStartTime >= alarmDuration) {
buzzerOn = false;
digitalWrite(relayPin, LOW); // Turn off the buzzer
}
}
}
// Helper function to format time with leading zeros
String formatTime(int value) {
if (value < 10) {
return"0" + String(value);
} else {
return String(value);
}
}
// Helper function to convert 24-hour format to 12-hour format
String format12Hour(int hour) {
if (hour == 0) return"12"; // Midnight
if (hour == 12) return"12"; // Noon
if (hour > 12) return String(hour - 12); // PM times
return String(hour); // AM times
}
// Helper function to format date
String formatDate(DateTime now) {
return String(now.day(), DEC) + "/" + String(now.month(), DEC) + "/" + String(now.year(), DEC);
}