#include <Wire.h>
#include <LiquidCrystal_I2C.h> // library for LCD with I2C
#include <RTClib.h>
#include <Time.h> // library for DS1307 RTC
const int en = 2, rw = 1, rs = 0, d4 = 4, d5 = 5, d6 = 6, d7 = 7, bl = 3;
// Define I2C Address - change if required
LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS1307 rtc;
#include <EEPROM.h>
const int btnChange = 2;
const int btnOK = 3;
const int btnMode = 4;
const int buzzer = 5;
int alarmHour = 12;
int alarmMinutes = 0;
boolean hourMode = 0;
boolean alarmOn = 1;
int M = 12;
int D = 31;
int Y = 2020;
int day = 7;
int h = 23;
int m = 59;
int addrH[] = {0, 1, 2, 3, 4};
int addrM[] = {5, 6, 7, 8, 9};
int addrActive[] = {10, 11, 12, 13, 14};
int mode = 1;
int submode = 1;
int slot = 0;
byte clockChar[] = {B00000, B01110, B10101, B10101, B10111, B10001, B01110, B00000};
byte alarmChar[] = {B00100, B01110, B01110, B01110, B11111, B00000, B00100, B00000};
byte dateChar[] = {B11111, B00000, B01100, B01010, B01010, B01100, B00000, B11111};
void setup() {
pinMode(btnChange, INPUT_PULLUP);
pinMode(btnOK, INPUT_PULLUP);
pinMode(btnMode, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
lcd.begin(16, 2); // initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.createChar(0, clockChar);
lcd.createChar(1, alarmChar);
lcd.createChar(2, dateChar);
Wire.begin();
rtc.begin();
now = now();
// Uncomment the following line if the RTC has lost power and needs to be set again
//rtc.adjust(DateTime(2023, 6, 18, 12, 0, 0));
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
Serial.begin(9600);
}
void loop() {
if (digitalRead(btnMode) == LOW) {
mode++;
Serial.println("btnMode press");
if (mode == 4) {
mode = 1;
}
submode = 0;
lcd.clear();
delay(200);
}
if (mode == 1) {
clockDisplay();
Serial.println(mode);
}
if (mode == 2) {
alarmMode();
Serial.println(mode);
}
if (mode == 3) {
updateRTC();
Serial.println(mode);
}
if (mode == 0) {
alarm();
Serial.println(mode);
EEPROM.write(addrActive, 0);
}
DateTime now = rtc.now();
for (int x = 0; x < 5; x++) {
int active = EEPROM.read(addrActive[x]);
if (active == 1) {
int alarmHour = EEPROM.read(addrH[x]);
int alarmMinutes = EEPROM.read(addrM[x]);
if (now.hour() == alarmHour && now.minute() == alarmMinutes && now.second() < 2) {
alarm();
EEPROM.write(addrActive[x], 0);
mode = 0;
}
}
}
delay(1000);
}
void clockDisplay() {
lcd.setCursor(0, 0);
lcd.print(" Current Time ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(now.toString("HH:mm:ss"));
lcd.print(" ");
}
void alarm() {
for (int i = 0; i < 5; i++) {
digitalWrite(buzzer, HIGH);
lcd.setBacklight(HIGH);
delay(500);
digitalWrite(buzzer, LOW);
lcd.setBacklight(LOW);
delay(500);
}
}
void alarmMode() {
if (submode == 0) {
slot++;
if (slot == 6) {
slot = 1;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Alarm Slot ");
lcd.print(slot);
lcd.setCursor(0, 1);
lcd.print("Set Alarm:");
delay(200);
while (digitalRead(btnChange) == HIGH) {
if (digitalRead(btnOK) == LOW) {
submode++;
lcd.clear();
delay(200);
}
}
}
if (submode == 1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Set Hour ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(alarmHour);
lcd.print(":");
if (hourMode == 0) {
lcd.print("00");
} else {
lcd.print("30");
}
lcd.print(" ");
while (digitalRead(btnChange) == HIGH) {
if (digitalRead(btnOK) == LOW) {
hourMode = !hourMode;
lcd.clear();
delay(200);
}
}
if (digitalRead(btnChange) == LOW) {
alarmHour++;
if (alarmHour == 13) {
alarmHour = 1;
}
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(alarmHour);
lcd.print(":");
if (hourMode == 0) {
lcd.print("00");
} else {
lcd.print("30");
}
lcd.print(" ");
delay(200);
}
if (digitalRead(btnOK) == LOW) {
submode++;
lcd.clear();
delay(200);
}
}
if (submode == 2) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Set Minute ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(alarmHour);
lcd.print(":");
if (hourMode == 0) {
lcd.print("00");
} else {
lcd.print("30");
}
lcd.print(" ");
while (digitalRead(btnChange) == HIGH) {
if (digitalRead(btnOK) == LOW) {
hourMode = !hourMode;
lcd.clear();
delay(200);
}
}
if (digitalRead(btnChange) == LOW) {
alarmMinutes += 5;
if (alarmMinutes >= 60) {
alarmMinutes = 0;
}
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(alarmHour);
lcd.print(":");
if (hourMode == 0) {
lcd.print("00");
} else {
lcd.print("30");
}
lcd.print(" ");
delay(200);
}
if (digitalRead(btnOK) == LOW) {
submode = 0;
lcd.clear();
EEPROM.write(addrH[slot - 1], alarmHour);
EEPROM.write(addrM[slot - 1], alarmMinutes);
EEPROM.write(addrActive[slot - 1], 1);
delay(200);
}
}
}
void updateRTC() {
if (submode == 0) {
slot++;
if (slot == 6) {
slot = 1;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Update Time ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(now.toString("MM-dd-yyyy"));
lcd.print(" ");
delay(200);
while (digitalRead(btnChange) == HIGH) {
if (digitalRead(btnOK) == LOW) {
submode++;
lcd.clear();
delay(200);
}
}
}
if (submode == 1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Set Month ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(M);
lcd.print(" ");
while (digitalRead(btnChange) == HIGH) {
if (digitalRead(btnOK) == LOW) {
lcd.clear();
delay(200);
submode++;
}
}
if (digitalRead(btnChange) == LOW) {
M++;
if (M == 13) {
M = 1;
}
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(M);
lcd.print(" ");
delay(200);
}
}
if (submode == 2) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Set Day ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(D);
lcd.print(" ");
while (digitalRead(btnChange) == HIGH) {
if (digitalRead(btnOK) == LOW) {
lcd.clear();
delay(200);
submode++;
}
}
if (digitalRead(btnChange) == LOW) {
D++;
if (D == 32) {
D = 1;
}
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(D);
lcd.print(" ");
delay(200);
}
}
if (submode == 3) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Set Year ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(Y);
lcd.print(" ");
while (digitalRead(btnChange) == HIGH) {
if (digitalRead(btnOK) == LOW) {
lcd.clear();
delay(200);
submode++;
}
}
if (digitalRead(btnChange) == LOW) {
Y++;
if (Y == 10000) {
Y = 1;
}
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(Y);
lcd.print(" ");
delay(200);
}
}
if (submode == 4) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Set Day ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(day);
lcd.print(" ");
while (digitalRead(btnChange) == HIGH) {
if (digitalRead(btnOK) == LOW) {
lcd.clear();
delay(200);
submode++;
}
}
if (digitalRead(btnChange) == LOW) {
day++;
if (day == 8) {
day = 1;
}
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(day);
lcd.print(" ");
delay(200);
}
}
if (submode == 5) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Set Hour ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(h);
lcd.print(" ");
while (digitalRead(btnChange) == HIGH) {
if (digitalRead(btnOK) == LOW) {
lcd.clear();
delay(200);
submode++;
}
}
if (digitalRead(btnChange) == LOW) {
h++;
if (h == 24) {
h = 0;
}
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(h);
lcd.print(" ");
delay(200);
}
}
if (submode == 6) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Set Minute ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(m);
lcd.print(" ");
while (digitalRead(btnChange) == HIGH) {
if (digitalRead(btnOK) == LOW) {
lcd.clear();
delay(200);
submode++;
}
}
if (digitalRead(btnChange) == LOW) {
m++;
if (m == 60) {
m = 0;
}
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(m);
lcd.print(" ");
delay(200);
}
}
if (submode == 7) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Updated ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(now.toString("MM-dd-yyyy"));
lcd.print(" ");
delay(2000);
lcd.clear();
rtc.adjust(DateTime(Y, M, D, h, m, 0));
submode = 0;
}
}