// Pin definitions for connecting segments a-g of the seven-segment display
const int segmentPins[] = {2, 3, 4, 5, 6, 7, 8, 9};

// Pin definitions for selecting the active digit on the display
const int selectPins[] = {12, 13, 10};

// Pin definitions for the buttons
const int buttonPins[] = {A0, A1, A2, A3};

// Pin connected to the temperature sensor
const int tempSens = A4;

// Pin connected to the buzzer
const int buzzer = 11;

// Beta Coefficient of the thermistor for temperature calculation
const float BETA = 3950;

// Flag indicating if temperature display is in Fahrenheit (true) or Celsius (false)
bool isF;

// Variables to store temperature values in Celsius and Fahrenheit
float celsius, fahrenheit;

// Initial temperature value
float temp = 23;

// Variables to store individual digits of the displayed temperature
int digit1, digit2, digit3;

// Flag to control the buzzer (enabled: true, disabled: false)
bool enablebuzzer = true;

// Define digit patterns for numbers 0-9 on the seven-segment display
const byte digitPatterns[][8] = {
  {1, 1, 1, 1, 1, 1, 0, 0},  // Digit 0
  {0, 1, 1, 0, 0, 0, 0, 0},  // Digit 1
  {1, 1, 0, 1, 1, 0, 1, 0},  // Digit 2
  {1, 1, 1, 1, 0, 0, 1, 0},  // Digit 3
  {0, 1, 1, 0, 0, 1, 1, 0},  // Digit 4
  {1, 0, 1, 1, 0, 1, 1, 0},  // Digit 5
  {1, 0, 1, 1, 1, 1, 1, 0},  // Digit 6
  {1, 1, 1, 0, 0, 0, 0, 0},  // Digit 7
  {1, 1, 1, 1, 1, 1, 1, 0},  // Digit 8
  {1, 1, 1, 1, 0, 1, 1, 0}   // Digit 9
};

// Setup function, called once at the start
void setup() {
  // Configure segment pins as outputs
  for (int i = 0; i < 8; i++) {
    pinMode(segmentPins[i], OUTPUT);
  }

  // Configure digit selection pins as outputs
  for (int i = 0; i < 3; i++) {
    pinMode(selectPins[i], OUTPUT);
  }

  // Configure button pins as inputs
  for (int i = 0; i < 4; i++) {
    pinMode(buttonPins[i], INPUT);
  }

  // Configure buzzer pin as output
  pinMode(buzzer, OUTPUT);
}

// Function to display a digit on the seven-segment display
void displayDigit(int digit, int relayPin) {
  // Turn off relay for the first display
  digitalWrite(selectPins[relayPin], HIGH);

  // Display each segment of the digit
  for (int i = 0; i < 8; i++) {
    digitalWrite(segmentPins[i], digitPatterns[digit][i]);
  }

  // Handle the decimal point for Celsius display
  if (relayPin == 1 && !isF) {
    digitalWrite(segmentPins[7], HIGH);
  }

  // Delay for stable display
  delay(20);

  // Activate the specified relay
  digitalWrite(selectPins[relayPin], LOW);
}

// Main loop function, repeatedly executed
void loop() {
  // Read analog value from the temperature sensor and calculate Celsius temperature
  int analogValue = analogRead(tempSens);
  celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;

  // Convert temperature to Fahrenheit if needed
  if (!isF) {
    convertCelsius();
  } else {
    convertF();
  }

  // Button to toggle between Celsius and Fahrenheit
  if (digitalRead(buttonPins[0])) {
    isF = !isF;
    if (isF) {
      temp = (temp * 9.0 / 5.0) + 32.0;
    } else {
      temp = (temp - 32) * 5.0 / 9.0;
    }
    delay(300);
  }

  // Buttons to increment and decrement temperature
  if (digitalRead(buttonPins[1])) {
    // Increment temperature by 2°F or 0.5°C
    if (isF) {
      temp += 2;
    } else {
      temp += 0.5;
    }
    unsigned long timer = millis();
    // Display the updated temperature setting
    if (!isF) {
      viewSetTempF();
    } else {
      viewSetTempC();
    }
  }

  if (digitalRead(buttonPins[2])) {
    // Decrement temperature by 2°F or 0.5°C
    if (isF) {
      temp -= 2;
    } else {
      temp -= 0.5;
    }
    unsigned long timer = millis();
    // Display the updated temperature setting
    if (!isF) {
      viewSetTempF();
    } else {
      viewSetTempC();
    }
  }

  // Button to toggle the buzzer
  if (digitalRead(buttonPins[3])) {
    enablebuzzer = !enablebuzzer;
    unsigned long timer = millis();
    // Show if the buzzer is enabled or disabled
    while (timer + 400 > millis()) {
      if (enablebuzzer) {
        displayDigit(0, 0);
        delay(20);

        displayDigit(0, 1);
        delay(20);

        displayDigit(1, 2);
        delay(20);
      } else {
        displayDigit(0, 0);
        delay(20);

        displayDigit(0, 1);
        delay(20);

        displayDigit(0, 2);
        delay(20);
      }
    }
  }

  // Activate the buzzer if temperature threshold is exceeded
  if (enablebuzzer && ((isF && fahrenheit > temp) || (!isF && celsius > temp))) {
    tone(buzzer, 1000);
  } else {
    noTone(buzzer);
  }

  // Display individual digits of the temperature
  displayDigit(digit1, 0);
  delay(20);

  displayDigit(digit2, 1);
  delay(20);

  displayDigit(digit3, 2);
  delay(20);
}

// Display the temperature setting in Fahrenheit
void viewSetTempF() {
  digit1 = int(temp) / 10; // Extract the first digit
  digit2 = int(temp) % 10; // Extract the second digit
  digit3 = int((temp - int(temp)) * 10); // Extract the third digit

  unsigned long timer = millis();
  // Display the temperature setting
  while (timer + 400 > millis()) {
    displayDigit(digit1, 0);
    delay(20);

    displayDigit(digit2, 1);
    delay(20);

    displayDigit(digit3, 2);
    delay(20);
  }
}

// Display the temperature setting in Celsius
void viewSetTempC() {
  digit1 = int(temp) / 100; // Extract the first digit
  digit2 = (int(temp) / 10) % 10; // Extract the second digit
  digit3 = int(temp) % 10; // Extract the third digit

  unsigned long timer = millis();
  // Display the temperature setting
  while (timer + 400 > millis()) {
    displayDigit(digit1, 0);
    delay(20);

    displayDigit(digit2, 1);
    delay(20);

    displayDigit(digit3, 2);
    delay(20);
  }
}

// Convert Celsius temperature to individual digits
void convertCelsius() {
  // Extract digits
  digit1 = int(celsius) / 10; // Extract the first digit
  digit2 = int(celsius) % 10; // Extract the second digit
  digit3 = int((celsius - int(celsius)) * 10); // Extract the third digit

  // Adjust third digit for display precision
  if (digit3 <= 0.4) {
    digit3 = 0;
  } else {
    digit3 = 5;
  }
}

// Convert Fahrenheit temperature to individual digits
void convertF() {
  // Calculate Fahrenheit temperature
  fahrenheit = (celsius * 9.0 / 5.0) + 32.0;

  // Extract Fahrenheit digits
  digit1 = int(fahrenheit) / 100; // Extract the first digit
  digit2 = (int(fahrenheit) / 10) % 10; // Extract the second digit
  digit3 = int(fahrenheit) % 10; // Extract the third digit
}
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module