#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Pin definitions
const int relayPin = 6;
const int selectButtonPin = 5;
const int nextButtonPin = 4;
const int upButtonPin = 3;
const int downButtonPin = 2;
const int tempSensorPin = 7;
// LCD and RTC setup
LiquidCrystal_I2C lcd(0x27, 20, 4); // Adjust address as needed
RTC_DS3231 rtc;
// Temperature sensor setup
OneWire oneWire(tempSensorPin);
DallasTemperature sensors(&oneWire);
// On/Off times
struct Time {
int hour;
int minute;
};
Time onTimes[7][2]; // 2 on times for each day of the week
Time offTimes[7][2]; // 2 off times for each day of the week
void setup() {
pinMode(relayPin, OUTPUT);
pinMode(selectButtonPin, INPUT_PULLUP);
pinMode(nextButtonPin, INPUT_PULLUP);
pinMode(upButtonPin, INPUT_PULLUP);
pinMode(downButtonPin, INPUT_PULLUP);
lcd.begin(20, 4);
lcd.backlight();
Wire.begin();
rtc.begin();
sensors.begin();
if (!rtc.begin()) {
lcd.print("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Load times from EEPROM
loadTimes();
// Set default times if needed
setDefaultTimes();
lcd.clear();
lcd.print("Timer Initialized");
delay(2000);
lcd.clear();
}
void loop() {
DateTime now = rtc.now();
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
displayTimeAndTemperature(now, temperature);
checkRelay(now);
handleButtons();
delay(500);
}
void setDefaultTimes() {
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 2; j++) {
if (EEPROM.read(i * 4 + j * 2) == 0xFF) {
onTimes[i][j] = {8, 0}; // Default on time 8:00 AM
offTimes[i][j] = {20, 0}; // Default off time 8:00 PM
}
}
}
}
void displayTimeAndTemperature(DateTime now, float temperature) {
lcd.setCursor(0, 0);
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
}
void checkRelay(DateTime now) {
for (int i = 0; i < 2; i++) {
if (now.hour() == onTimes[now.dayOfTheWeek()][i].hour && now.minute() == onTimes[now.dayOfTheWeek()][i].minute) {
digitalWrite(relayPin, LOW); // Turn relay on
} else if (now.hour() == offTimes[now.dayOfTheWeek()][i].hour && now.minute() == offTimes[now.dayOfTheWeek()][i].minute) {
digitalWrite(relayPin, HIGH); // Turn relay off
}
}
}
bool debounceButton(int pin) {
bool state = digitalRead(pin);
delay(50);
return state == digitalRead(pin);
}
void handleButtons() {
static int mode = 0;
static int day = 0;
static int timeSlot = 0;
if (!debounceButton(selectButtonPin)) {
mode++;
if (mode > 2) mode = 0;
delay(200);
}
if (mode == 1) {
lcd.setCursor(0, 2);
lcd.print("Set On Time: ");
lcd.print(day);
lcd.print(", ");
lcd.print(timeSlot);
if (!debounceButton(nextButtonPin)) {
timeSlot++;
if (timeSlot > 1) timeSlot = 0;
delay(200);
}
if (!debounceButton(upButtonPin)) {
onTimes[day][timeSlot].hour++;
if (onTimes[day][timeSlot].hour > 23) onTimes[day][timeSlot].hour = 0;
delay(200);
}
if (!debounceButton(downButtonPin)) {
onTimes[day][timeSlot].minute++;
if (onTimes[day][timeSlot].minute > 59) onTimes[day][timeSlot].minute = 0;
delay(200);
}
lcd.setCursor(0, 3);
lcd.print(onTimes[day][timeSlot].hour);
lcd.print(':');
lcd.print(onTimes[day][timeSlot].minute);
}
if (mode == 2) {
lcd.setCursor(0, 2);
lcd.print("Set Off Time: ");
lcd.print(day);
lcd.print(", ");
lcd.print(timeSlot);
if (!debounceButton(nextButtonPin)) {
timeSlot++;
if (timeSlot > 1) timeSlot = 0;
delay(200);
}
if (!debounceButton(upButtonPin)) {
offTimes[day][timeSlot].hour++;
if (offTimes[day][timeSlot].hour > 23) offTimes[day][timeSlot].hour = 0;
delay(200);
}
if (!debounceButton(downButtonPin)) {
offTimes[day][timeSlot].minute++;
if (offTimes[day][timeSlot].minute > 59) offTimes[day][timeSlot].minute = 0;
delay(200);
}
lcd.setCursor(0, 3);
lcd.print(offTimes[day][timeSlot].hour);
lcd.print(':');
lcd.print(offTimes[day][timeSlot].minute);
}
if (mode == 0) {
saveTimes();
}
}
void saveTimes() {
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 2; j++) {
EEPROM.write(i * 4 + j * 2, onTimes[i][j].hour);
EEPROM.write(i * 4 + j * 2 + 1, onTimes[i][j].minute);
EEPROM.write(i * 4 + j * 2 + 64, offTimes[i][j].hour);
EEPROM.write(i * 4 + j * 2 + 65, offTimes[i][j].minute);
}
}
}
void loadTimes() {
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 2; j++) {
onTimes[i][j].hour = EEPROM.read(i * 4 + j * 2);
onTimes[i][j].minute = EEPROM.read(i * 4 + j * 2 + 1);
offTimes[i][j].hour = EEPROM.read(i * 4 + j * 2 + 64);
offTimes[i][j].minute = EEPROM.read(i * 4 + j * 2 + 65);
}
}
}