Code
Code
h>
#include <Wire.h>
#include <RTClib.h>
#include <EEPROM.h>
#include <Adafruit_I2CDevice.h>
//=================================================================
// SETUP
//=================================================================
void setup() {
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(RELAY, OUTPUT);
pinMode(up_key, INPUT);
pinMode(down_key, INPUT);
if (rtc.lostPower()) {
lcd.clear();
lcd.print("RTC lost power, let's set the time!");
rtc.adjust(DateTime(F(_DATE), F(TIME_)));
}
}
//=================================================================
// Function to control the heater
//=================================================================
void controlHeater(bool turnOn) {
if (turnOn) {
digitalWrite(RELAY, HIGH); // Turn on heater
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_RED, HIGH); // Turn on RED LED
} else {
digitalWrite(RELAY, LOW); // Turn off heater
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH); // Turn on Green LED
}
}
//=================================================================
// LOOP
//=================================================================
void loop() {
DateTime now = rtc.now(); // Get the current time from the RTC module
lcd.setCursor(0, 0);
lcd.print("Temperature:"); // Do not display entered keys
lcd.print(Temperature);
// Check if the current time is within the desired range (from 12 AM to 6 AM)
if (now.hour() >= 5 && now.hour() < 6) {
// Turn on the heater if the temperature is below the set point
if (Temperature < SetPoint) {
controlHeater(true); // Turn on heater
} else {
controlHeater(false); // Turn off heater
}
} else {
// During other hours, turn off the heater
controlHeater(false); // Turn off heater
}
delay(100); // Update every 100 milliseconds
}