#include <TM1637Display.h>

// Pin definitions for TM1637
#define CLK 2
#define DIO 3

// Pin definitions for buttons
#define BUTTON_MENU 4
#define BUTTON_UP 5
#define BUTTON_DOWN 6
#define LED_PIN 7

// Pin for the thermistor
#define THERMISTOR_PIN A0

// Variables
TM1637Display display(CLK, DIO);

const uint8_t C[] = {
  SEG_B | SEG_A | SEG_F | SEG_G,
  SEG_A | SEG_D | SEG_E | SEG_F
  };

float temperature;
int setPoint = 25; // Default setpoint in Celsius
bool inSetPointMode = false; // Set to true when setpoint mode is active

// Button state
bool menuButtonState = false;
bool upButtonState = false;
bool downButtonState = false;

void setup()
 {
  // Initialize display
  display.setBrightness(0x0f);
  
  // Button inputs
  pinMode(BUTTON_MENU, INPUT_PULLUP);
  pinMode(BUTTON_UP, INPUT_PULLUP);
  pinMode(BUTTON_DOWN, INPUT_PULLUP);

  // Thermistor pin
  pinMode(THERMISTOR_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW); // Ensure LED is off initially

  Serial.begin(9600);
}

void loop() {
  // Check for button presses
  checkButtons();

  // Read temperature from thermistor
  temperature = readThermistor();

  // If in setpoint mode, display the setpoint
  if (inSetPointMode) {
    displaySetPoint();
  } else {
    // Otherwise, display the temperature
    displayTemperature();
  }

 if (temperature > setPoint) {
    digitalWrite(LED_PIN, HIGH); // Turn LED on if temp > set point
  } else {
    digitalWrite(LED_PIN, LOW);  // Turn LED off if temp <= set point
  }



  delay(200); // Small delay to debounce buttons



}

// Function to read thermistor and calculate temperature
float readThermistor()
 {
  int adcValue = analogRead(THERMISTOR_PIN);
  float resistance = (1023.0 / adcValue )- 1.0; // Assuming 10k resistor
   resistance = 10000.0 / resistance;
  float tempCelsius = 1 / (0.001129148 + (0.000234125 * log(resistance)) + (0.0000000876741 * pow(log(resistance), 3))) - 273.15;
  return tempCelsius;
}

// Function to display the temperature on the TM1637 display
void displayTemperature()
 {
  int displayTemp = (int)temperature * 1; // Convert to a form that can be displayed
  display.showNumberDecEx(displayTemp, 0x20, false, 2, 0); // Display with decimal point
// display.showNumberDecEx(displayTemp, 0x40, false, 2, 0); // Display with decimal point
display.setSegments(C,2,2);
   delay(1000);  // Update every second
}

// Function to display the setpoint
void displaySetPoint() {
  display.showNumberDec(setPoint, false, 2, 0);
}

// Function to check button presses and switch modes or adjust setpoint
void checkButtons() {
  if (digitalRead(BUTTON_MENU) == LOW) {
    delay(100); // Debounce delay
    if (!menuButtonState) {
      inSetPointMode = !inSetPointMode; // Toggle setpoint mode
      menuButtonState = true;
    }
  } else {
    menuButtonState = false;
  }

  if (inSetPointMode) {
    if (digitalRead(BUTTON_UP) == LOW) {
      delay(100); // Debounce delay
      if (!upButtonState) {
        setPoint++;
        upButtonState = true;
      }
    } else {
      upButtonState = false;
    }

    if (digitalRead(BUTTON_DOWN) == LOW) {
      delay(100); // Debounce delay
      if (!downButtonState) {
        setPoint--;
        downButtonState = true;
      }
    } else {
      downButtonState = false;
    }
  }
}
4-Digit Display