#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>

// Initialize the library with the I2C address 0x27 for a 16x2 LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Define button pins
const int button1Pin = 6;
const int button2Pin = 7;
const int button3Pin = 8;

// Variable to store the temperature
float temperature = 0.0;

unsigned long backlightTimeout = 0;
unsigned long buttonPressTime = 0;
unsigned long screenTimeout = 0;

void setup() {
  // Initialize the LCD with the number of columns and rows
  lcd.begin(16, 2);
  lcd.backlight();

  // Retrieve the last stored temperature from EEPROM
  EEPROM.get(0, (float&)temperature);

  // If the temperature is not a valid number (i.e., NaN), set it to 37.0
  if (isnan(temperature)) {
    temperature = 37.0;
    EEPROM.put(0, temperature);
  }

  // Set button pins as input with internal pull-up resistors
  pinMode(button1Pin, INPUT_PULLUP);
  pinMode(button2Pin, INPUT_PULLUP);
  pinMode(button3Pin, INPUT_PULLUP);
}

void loop() {
  // Read the state of the buttons
  int button1State = digitalRead(button1Pin);
  int button2State = digitalRead(button2Pin);
  int button3State = digitalRead(button3Pin);

  // Check if button 1 is pressed
  if (button1State == LOW && millis() - buttonPressTime > 400) {
    // Display the temperature
    lcd.clear();
    lcd.backlight();
    delay(1000);
    lcd.print(temperature, 1);
    lcd.print((char) 223);
    lcd.print(" ");
    lcd.print("celcius");
   
    backlightTimeout = millis() + 5000; // Set the backlight timeout to 5 seconds from now
    screenTimeout = millis() + 5000; // Set the screen timeout to 5 seconds from now
    buttonPressTime = millis();
  }

  // Check if button 2 is pressed
  if (button2State == LOW && millis() - buttonPressTime > 400) {
    // Increase the temperature by 0.2
    temperature += 0.2;
    lcd.clear();
    lcd.print(temperature, 1);
    lcd.print((char) 223);
    lcd.print(" ");
    lcd.print("celcius");
    lcd.backlight();
    backlightTimeout = millis() + 5000; // Set the backlight timeout to 5 seconds from now
    screenTimeout = millis() + 5000; // Set the screen timeout to 5 seconds from now
    EEPROM.put(0, temperature);
    buttonPressTime = millis();
  }

  // Check if button 3 is pressed
  if (button3State == LOW && millis() - buttonPressTime > 400) {
    // Decrease the temperature by 0.2
    temperature -= 0.2;
    lcd.clear();
    lcd.print(temperature, 1);
    lcd.print((char) 223);
    lcd.print(" ");
    lcd.print("celcius");
    lcd.backlight();
    backlightTimeout = millis() + 5000; // Set the backlight timeout to 5 seconds from now
    screenTimeout = millis() + 5000; // Set the screen timeout to 5 seconds from now
    EEPROM.put(0, temperature);
    buttonPressTime = millis();
  }

  // Check if the backlight timeout has been reached
  if (millis() > backlightTimeout) {
    lcd.noBacklight(); // Turn off the backlight
  }

  // Check if the screen timeout has been reached
  if (millis() > screenTimeout) {
    lcd.clear(); // Clear the screen
  }
}