#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
RTC_DS1307 myRtc;
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
int hour = 0;
int minute = 0;
int second = 0;
LiquidCrystal_I2C myLcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
byte letter_K[8] = {
0b00000,
0b00000,
0b10010,
0b10100,
0b11000,
0b10100,
0b10010,
0b00000,
};
byte letter_CH_Uppercase[8] = {
0b00000,
0b10010,
0b10010,
0b10010,
0b11110,
0b00010,
0b00010,
0b00000,
};
byte letter_CH_Lowercase[8] = {
0b00000,
0b00000,
0b10010,
0b10010,
0b11110,
0b00010,
0b00010,
0b00000,
};
byte letter_T[8] = {
0b00000,
0b00000,
0b11111,
0b00100,
0b00100,
0b00100,
0b00100,
0b00000,
};
int alarmHour = 0; // Година для будильника
int alarmMinute = 0; // Хвилина для будильника
int alarmSecond = 0; // Секунда для будильника
void setup()
{
Serial.begin(9600);
if (!myRtc.begin())
{
Serial.println("RTC module not found. Connect RTC!");
while (1);
}
myLcd.backlight();
myLcd.begin(20, 4);
myLcd.createChar(0, letter_K);
myLcd.createChar(1, letter_CH_Uppercase);
myLcd.createChar(2, letter_CH_Lowercase);
myLcd.createChar(3, letter_T);
myLcd.setCursor(0, 0);
myLcd.write(byte(1)); // Ч
myLcd.print("e");
myLcd.write(byte(2)); // ч
myLcd.write(byte(0)); // к
myLcd.print("o");
myLcd.setCursor(0, 1);
myLcd.print("H");
myLcd.print("i");
myLcd.write(byte(0)); // к
myLcd.print("i");
myLcd.write(byte(3)); // т
myLcd.print("a");
// Введення часу будильника з консолі
}
void loop()
{
static unsigned long lastUpdateTime = 0; // Зберігаємо час останнього оновлення
DateTime now = myRtc.now();
// Оновлення часу на дисплеї кожну секунду
if (millis() - lastUpdateTime >= 1000) {
DateTime now = myRtc.now();
myLcd.setCursor(0, 2);
if (now.hour() < 10) myLcd.print("0");
myLcd.print(now.hour(), DEC);
myLcd.print(":");
if (now.minute() < 10) myLcd.print("0");
myLcd.print(now.minute(), DEC);
myLcd.print(":");
if (now.second() < 10) myLcd.print("0");
myLcd.print(now.second(), DEC);
}
setAlarm();
if (now.hour() == hour && now.minute() == minute && now.second() == second)
{
// Якщо настав час будильника, то викликаємо функцію обробки будильника
while (true) {
handleAlarm();
}
}
}
void handleAlarm()
{
static unsigned long lastUpdateTime = 0;
static int currentRow = 0; // Start with the first row
if (millis() - lastUpdateTime >= 1000) {
myLcd.clear(); // Clear the LCD
DateTime now = myRtc.now(); // Get the current time
// Display the information in the desired order
myLcd.setCursor(0, currentRow);
myLcd.write(byte(1)); // Ч
myLcd.print("e");
myLcd.write(byte(2)); // ч
myLcd.write(byte(0)); // к
myLcd.print("o");
myLcd.setCursor(0, (currentRow + 1) % LCD_LINES);
myLcd.print("H");
myLcd.print("i");
myLcd.write(byte(0)); // к
myLcd.print("i");
myLcd.write(byte(3)); // т
myLcd.print("a");
myLcd.setCursor(0, (currentRow + 2) % LCD_LINES);
myLcd.print(now.hour(), DEC);
myLcd.print(':');
myLcd.print(now.minute(), DEC);
myLcd.print(':');
myLcd.print(now.second(), DEC);
// Move to the next row in the reverse direction
currentRow = (currentRow - 1 + LCD_LINES) % LCD_LINES;
lastUpdateTime = millis();
}
}
void setAlarm() {
String str;
str = Serial.readStringUntil(10);
int index_1, index_2;
index_1 = str.indexOf(':', 0);
index_2 = str.indexOf(':', index_1+1);
hour = str.substring(0,index_1).toInt();
minute = str.substring(index_1+1,index_2).toInt();
second = str.substring(index_2+1,str.length()).toInt();
Serial.print(hour);
}