#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
// إعداد شاشة LCD وعنوانها الافتراضي 0x27 وعرض 16 عمود وسطرين
LiquidCrystal_I2C lcd(0x27, 16, 2);
// إعداد موديل الوقت RTC DS1307
RTC_DS1307 rtc;
// تحديد بنات التوصيل
const int relayPin = 2; // دبوس التحكم بالريلي (يُربط في الدبوس السفلي IN للريلي)
const int ledGreen = 4; // الـ LED الأخضر (مؤشر التشغيل)
const int ledRed = 5; // الـ LED الأحمر (مؤشر الإيقاف)
// تحديد وقت التشغيل والإطفاء الجديد (من 11 إلى 12)
const int startHour = 11; // ساعة البدء (11 صباحاً)
const int startMinute = 0; // دقيقة البدء
const int endHour = 12; // ساعة الإطفاء (12 ظهراً)
const int endMinute = 0; // دقيقة الإطفاء
void setup() {
// تعريف البنات كمخارج
pinMode(relayPin, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(ledRed, OUTPUT);
// تشغيل الشاشة والـ backlight
lcd.init();
lcd.backlight();
// تشغيل موديل الوقت
if (!rtc.begin()) {
lcd.setCursor(0, 0);
lcd.print("RTC Error!");
while (1);
}
// لضبط الوقت تلقائياً في المحاكاة بناءً على وقت جهازك الحالي
// ضبط الوقت يدوياً على تاريخ اليوم والساعة 11:45:00
rtc.adjust(DateTime(2026, 7, 18, 10, 5, 0));
}
void loop() {
// قراءة الوقت الحالي من الـ RTC
DateTime now = rtc.now();
// عرض الوقت على السطر الأول في الشاشة
lcd.setCursor(0, 0);
lcd.print("Time: ");
if (now.hour() < 10) lcd.print('0');
lcd.print(now.hour());
lcd.print(':');
if (now.minute() < 10) lcd.print('0');
lcd.print(now.minute());
lcd.print(':');
if (now.second() < 10) lcd.print('0');
lcd.print(now.second());
lcd.print(" ");
// حساب الوقت الحالي بالدقائق لتسهيل المقارنة
int currentMinutes = (now.hour() * 60) + now.minute();
int startMinutes = (startHour * 60) + startMinute;
int endMinutes = (endHour * 60) + endMinute;
// التحقق مما إذا كان الوقت الحالي يقع بين الساعة 11 و 12
if (currentMinutes >= startMinutes && currentMinutes < endMinutes) {
digitalWrite(relayPin, HIGH); // تشغيل الريلي
digitalWrite(ledGreen, HIGH); // تشغيل الضوء الأخضر
digitalWrite(ledRed, LOW); // إطفاء الضوء الأحمر
lcd.setCursor(0, 1);
lcd.print("Status: ON ");
} else {
digitalWrite(relayPin, LOW); // إطفاء الريلي
digitalWrite(ledGreen, LOW); // إطفاء الضوء الأخضر
digitalWrite(ledRed, HIGH); // تشغيل الضوء الأحمر
lcd.setCursor(0, 1);
lcd.print("Status: OFF ");
}
delay(1000); // تحديث القراءة كل ثانية
}