#include   <LiquidCrystal.
h>
#include   <Wire.h>
#include   <RTClib.h>
#include   <EEPROM.h>
#include   <Adafruit_I2CDevice.h>
RTC_DS3231 rtc;    // Create an RTC object
LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
const int LED_RED = 13;   // Red LED
const int LED_GREEN = 11; // Green LED
const int RELAY = 12;     // Lock Relay or motor
// Key connections with Arduino
const int up_key = 3;
const int down_key = 2;
int SetPoint = 30;
//=================================================================
// SETUP
//=================================================================
void setup() {
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(RELAY, OUTPUT);
  pinMode(up_key, INPUT);
  pinMode(down_key, INPUT);
    // Pull up for setpoint keys
    digitalWrite(up_key, HIGH);
    digitalWrite(down_key, HIGH);
    // set up the LCD's number of columns and rows:
    lcd.begin(16, 2);
    // Print a message to the LCD.
    lcd.print("GYSER SYSTEM");
    lcd.setCursor(0, 1); // Move cursor to the second line
    lcd.print(" CONTROLLER    ");
    digitalWrite(LED_GREEN, HIGH); // Green LED Off
    digitalWrite(LED_RED, LOW);    // Red LED On
    digitalWrite(RELAY, LOW);      // Turn off Relay
    delay(2000);
    // Initialize the RTC
    if (!rtc.begin()) {
      lcd.clear();
      lcd.print("Couldn't find RTC");
      while (1);
    }
    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
  double Temperature = ((5.0 / 1024.0) * analogRead(A0)) * 100; // 10mV per degree
0.01V/C. Scaling
  lcd.setCursor(0, 0);
  lcd.print("Temperature:"); // Do not display entered keys
  lcd.print(Temperature);
  // Get user input for setpoints
  if (digitalRead(down_key) == LOW) {
    if (SetPoint > 0) {
      SetPoint--;
    }
  }
  if (digitalRead(up_key) == LOW) {
    if (SetPoint < 150) {
      SetPoint++;
    }
  }
  // Display Set point on LCD
  lcd.setCursor(0, 1);
  lcd.print("Set Point:");
  lcd.print(SetPoint);
  lcd.print("C   ");
  // 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
}