// Pin definitions for connecting segments a-g of the seven-segment display
const int a = 7;
const int b = 6;
const int c = 4;
const int d = 3;
const int e = 2;
const int f = 8;
const int g = 9;
const int DP = 5;

// Pin definitions for selecting the active digit on the display
const int mosfet1 = 12;
const int mosfet2 = 11;
const int mosfet3 = 10;

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

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

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

// 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 = 30;

// 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 flickeringDelay 5     // delay to show
#define flickeringDelay2 5    // delay to erase

// Setup function, called once at the start
void setup() {
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(DP, OUTPUT);

  pinMode(mosfet1, OUTPUT);
  pinMode(mosfet2, OUTPUT);
  pinMode(mosfet3, OUTPUT);

  pinMode(buttonPins[0], INPUT);
  pinMode(buttonPins[1], INPUT);
  pinMode(buttonPins[2], INPUT);
  pinMode(buttonPins[3], INPUT);

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

// Main loop function, repeatedly executed
void loop() {
  // Read analog value from the temperature sensor and calculate Celsius temperature
  int analogValue = analogRead(tempSens);
  celsius = (((analogValue * 5 / 1024.0) * 1000) - 2103) / (-10.9);

  // 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) {
        digitalWrite(mosfet1, HIGH);
        getal(1);
        delay(flickeringDelay);
        getal(10);  // zorgt ervoor dat alle LEDs van elk segment uit gaat
        digitalWrite(mosfet1, LOW);
        delay(flickeringDelay2);

        digitalWrite(mosfet2, HIGH);
        getal(0);
        delay(flickeringDelay);
        getal(10);
        digitalWrite(mosfet2, LOW);
        delay(flickeringDelay2);

        digitalWrite(mosfet3, HIGH);
        getal(0);
        delay(flickeringDelay);
        getal(10);
        digitalWrite(mosfet3, LOW);
        delay(flickeringDelay2);
      } else {
        digitalWrite(mosfet1, HIGH);
        getal(0);
        delay(flickeringDelay);
        getal(10);  // zorgt ervoor dat alle LEDs van elk segment uit gaat
        digitalWrite(mosfet1, LOW);
        delay(flickeringDelay2);

        digitalWrite(mosfet2, HIGH);
        getal(0);
        delay(flickeringDelay);
        getal(10);
        digitalWrite(mosfet2, LOW);
        delay(flickeringDelay2);

        digitalWrite(mosfet3, HIGH);
        getal(0);
        delay(flickeringDelay);
        getal(10);
        digitalWrite(mosfet3, LOW);
        delay(flickeringDelay2);
      }
    }
  }

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

  // Display individual digits of the temperature
  digitalWrite(mosfet1, HIGH);
  getal(digit1);
  delay(flickeringDelay);
  getal(10);  // zorgt ervoor dat alle LEDs van elk segment uit gaat
  digitalWrite(mosfet1, LOW);
  delay(flickeringDelay2);

  digitalWrite(mosfet2, HIGH);
  if (isF == false) digitalWrite(DP, HIGH);
  getal(digit2);
  delay(flickeringDelay);
  digitalWrite(DP, LOW);
  getal(10);
  digitalWrite(mosfet2, LOW);
  delay(flickeringDelay2);

  digitalWrite(mosfet3, HIGH);
  getal(digit3);
  delay(flickeringDelay);
  getal(10);
  digitalWrite(mosfet3, LOW);
  delay(flickeringDelay2);
}

// 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()) {
    digitalWrite(mosfet1, HIGH);
    getal(digit1);
    delay(flickeringDelay);
    getal(10);  // zorgt ervoor dat alle LEDs van elk segment uit gaat
    digitalWrite(mosfet1, LOW);
    delay(flickeringDelay2);

    digitalWrite(mosfet2, HIGH);
    getal(digit2);
    delay(flickeringDelay);
    getal(10);
    digitalWrite(mosfet2, LOW);
    delay(flickeringDelay2);

    digitalWrite(mosfet3, HIGH);
    getal(digit3);
    delay(flickeringDelay);
    getal(10);
    digitalWrite(mosfet3, LOW);
    delay(flickeringDelay2);
  }
}

// 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()) {
    digitalWrite(mosfet1, HIGH);
    getal(digit1);
    delay(flickeringDelay);
    getal(10);  // zorgt ervoor dat alle LEDs van elk segment uit gaat
    digitalWrite(mosfet1, LOW);
    delay(flickeringDelay2);

    digitalWrite(mosfet2, HIGH);
    digitalWrite(DP, HIGH);
    getal(digit2);
    delay(flickeringDelay);
    digitalWrite(DP, LOW);
    getal(10);
    digitalWrite(mosfet2, LOW);
    delay(flickeringDelay2);

    digitalWrite(mosfet3, HIGH);
    getal(digit3);
    delay(flickeringDelay);
    getal(10);
    digitalWrite(mosfet3, LOW);
    delay(flickeringDelay2);
  }
}

// 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 <= 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
}

void getal(int x) {
  if (x == 0) {
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, HIGH);
    digitalWrite(e, HIGH);
    digitalWrite(f, HIGH);
    digitalWrite(g, LOW);
  } else if (x == 1) {
    digitalWrite(a, LOW);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
    digitalWrite(f, LOW);
    digitalWrite(g, LOW);
  } else if (x == 2) {
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, LOW);
    digitalWrite(d, HIGH);
    digitalWrite(e, HIGH);
    digitalWrite(f, LOW);
    digitalWrite(g, HIGH);
  } else if (x == 3) {
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, HIGH);
    digitalWrite(e, LOW);
    digitalWrite(f, LOW);
    digitalWrite(g, HIGH);
  } else if (x == 4) {
    digitalWrite(a, LOW);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
    digitalWrite(f, HIGH);
    digitalWrite(g, HIGH);
  } else if (x == 5) {
    digitalWrite(a, HIGH);
    digitalWrite(b, LOW);
    digitalWrite(c, HIGH);
    digitalWrite(d, HIGH);
    digitalWrite(e, LOW);
    digitalWrite(f, HIGH);
    digitalWrite(g, HIGH);
  } else if (x == 6) {
    digitalWrite(a, HIGH);
    digitalWrite(b, LOW);
    digitalWrite(c, HIGH);
    digitalWrite(d, HIGH);
    digitalWrite(e, HIGH);
    digitalWrite(f, HIGH);
    digitalWrite(g, HIGH);
  } else if (x == 7) {
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
    digitalWrite(f, LOW);
    digitalWrite(g, LOW);
  } else if (x == 8) {
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, HIGH);
    digitalWrite(e, HIGH);
    digitalWrite(f, HIGH);
    digitalWrite(g, HIGH);
  } else if (x == 9) {
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, HIGH);
    digitalWrite(e, LOW);
    digitalWrite(f, HIGH);
    digitalWrite(g, HIGH);
  } else if (x == 10) {
    digitalWrite(a, LOW);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
    digitalWrite(f, LOW);
    digitalWrite(g, LOW);
    digitalWrite(DP, LOW);
  }
}
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module