#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#include <Keypad.h>
// Initialize DS1307RTC
RTC_DS1307 rtc;
// Initialize LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define keypad matrix
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {22, 24, 26, 28};
byte colPins[COLS] = {30, 32, 34, 36};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// Alarm time variables
struct Alarm {
int hour;
int minute;
bool enabled;
};
Alarm alarms[3];
int currentAlarm = 0;
void setup() {
Serial.begin(9600);
rtc.begin();
lcd.init();
lcd.backlight();
// Set initial time (adjust as needed)
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press '*' to set");
lcd.setCursor(0, 1);
lcd.print("alarm time");
}
void loop() {
DateTime now = rtc.now();
char key = keypad.getKey();
if (key != NO_KEY) {
if (key == '*') {
setAlarmTime();
}
}
// Display the current time every 2 seconds
static unsigned long lastDisplayTime = 0;
if (millis() - lastDisplayTime >= 1000) {
displayCurrentTime(now);
lastDisplayTime = millis();
}
for (int i = 0; i < 3; i++) {
if (alarms[i].enabled && now.hour() == alarms[i].hour && now.minute() == alarms[i].minute) {
triggerAlarm(i);
}
}
}
void displayCurrentTime(DateTime currentTime) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Current Time:");
lcd.setCursor(0, 1);
lcd.print(currentTime.hour(), DEC);
lcd.print(":");
if (currentTime.minute() < 10) {
lcd.print("0");
}
lcd.print(currentTime.minute(), DEC);
}
void setAlarmTime() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Alarm " + String(currentAlarm + 1) + ":");
lcd.setCursor(0, 1);
lcd.print("HH:MM");
static int cursorPos = 0;
static char inputBuffer[5];
char key = NO_KEY;
while (key != '#') {
key = keypad.getKey();
if (key != NO_KEY) {
if (cursorPos <= 4) {
if (key >= '0' && key <= '9') {
inputBuffer[cursorPos] = key;
lcd.setCursor(cursorPos, 1);
lcd.print(key);
cursorPos++;
if (cursorPos == 2) {
cursorPos += 1;
}
}
else if (key == '*') {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Alarm " + String(currentAlarm + 1) + ":");
lcd.setCursor(0, 1);
lcd.print("HH:MM");
cursorPos = 0;
}
}
}
}
if (cursorPos == 5) {
alarms[currentAlarm].hour = (inputBuffer[0] - '0') * 10 + (inputBuffer[1] - '0');
alarms[currentAlarm].minute = (inputBuffer[3] - '0') * 10 + (inputBuffer[4] - '0');
alarms[currentAlarm].enabled = true;
lcd.setCursor(0, 1);
lcd.print("Alarm set ");
delay(1000);
lcd.clear();
currentAlarm++;
if (currentAlarm >= 3) {
currentAlarm = 0;
}
}
lcd.setCursor(0, 0);
lcd.print("Press '*' to set");
lcd.setCursor(0, 1);
lcd.print("alarm time");
cursorPos = 0;
}
void triggerAlarm(int alarmIndex) {
// Implement your alarm action here
// For example, you can turn on a buzzer or an LED
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ALARM " + String(alarmIndex + 1) + "!");
delay(2000); // Delay to show the alarm message
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("Set Alarm " + String(currentAlarm + 1) + ":");
// lcd.setCursor(0, 1);
// lcd.print("HH:MM");
// alarms[alarmIndex].enabled = false;
}