#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS1307 rtc;
// זמן נוכחי והתראות
int h = 0, m = 0, s = 0;
int h1 = 0, m1 = 0;
int h2 = 0, m2 = 0;
int st = 0;
#define BTN_SET 4
#define BUZZER_PIN 8
#define POT_PIN A0
void setup() {
rtc.begin();
lcd.init();
lcd.backlight();
Serial.begin(9600);
pinMode(BTN_SET, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
// מאתחל זמן רק אם השעון לא רץ
if (!rtc.isrunning()) {
rtc.adjust(DateTime(F(_DATE), F(TIME_)));
}
}
void loop() {
displayTime();
// הגדרת זמנים
if (st >= 1 && st <= 2) {
handleSetting(h1, m1);
}
if (st >= 4 && st <= 5) {
handleSetting(h2, m2);
}
// בדיקת זמנים והתראת באזר
if ((h == h1 && m == m1) || (h == h2 && m == m2)) {
tone(BUZZER_PIN, 1000);
} else {
noTone(BUZZER_PIN);
}
}
// תצוגת זמן ראשית
void displayTime() {
DateTime now = rtc.now();
h = now.hour();
m = now.minute();
s = now.second();
if (digitalRead(BTN_SET) == LOW) {
delay(200);
st++;
if (st > 6) st = 0;
}
lcd.setCursor(0, 0);
lcd.print("Time ");
printTwoDigits(h); lcd.print(":");
printTwoDigits(m); lcd.print(":");
printTwoDigits(s);
lcd.setCursor(0, 1);
if (st == 0) lcd.print("Set alarm times ");
else if (st == 1 || st == 2) {
lcd.print("Start ");
printTwoDigits(h1); lcd.print(":");
printTwoDigits(m1); lcd.print(" ");
}
else if (st == 4 || st == 5) {
lcd.print("Stop ");
printTwoDigits(h2); lcd.print(":");
printTwoDigits(m2); lcd.print(" ");
}
}
// הגדרת שעה/דקה עם פוטנציו-מטר
void handleSetting(int &hourVal, int &minVal) {
if (st % 3 == 1) {
hourVal = readPotValue(0, 23); // טווח לשעות
} else if (st % 3 == 2) {
minVal = readPotValue(0, 59); // טווח לדקות
}
delay(150); // מונע קפיצות במסך
}
// קריאה מפוטנציו-מטר בטווח נתון
int readPotValue(int minVal, int maxVal) {
int potValue = analogRead(POT_PIN);
return map(potValue, 0, 1023, minVal, maxVal);
}
// תצוגת מספר עם אפס מוביל
void printTwoDigits(int number) {
if (number < 10) lcd.print("0");
lcd.print(number);
}