// Forum: https://forum.arduino.cc/t/16x2-lcd-screen-crashes-after-a-while/1242503
// This Wokwi project: https://wokwi.com/projects/393967786342550529
//
// Changes to the sketch: none

#include <avr/sleep.h>
#include <LiquidCrystal.h>
#include <math.h>

int const aREF = 0b00000000;
int ThermistorPin = 0;
int Vo;
float R1 = 20000;
float logR2, T, Tc, Vo_2, R2;
float c1 = 1.129241e-03, c2 = 2.341077e-04, c3 = 0.087755e-06;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  ADMUX = aREF;
  ADCSRA |= 1 << ADEN;
  ADCSRA |= 1 << ADIE;
  ADCSRA |= ((1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0)); // Corrected ADPS setting
  sleep_enable();
  set_sleep_mode(SLEEP_MODE_ADC);
  analogReference(EXTERNAL);
  lcd.begin(16, 2); // Initialize LCD
}

void loop() {
  sei();
  sleep_cpu();
  Vo = analogRead(ThermistorPin);
  Vo_2 = ((float)Vo / 1023.0) * 5.0;
  R2 = R1 * ((5.0 - Vo_2) / Vo_2);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
  Tc = T - 273.15;

  lcd.setCursor(0, 0);
  lcd.print("Temp = ");
  lcd.print(Tc);
  lcd.print(" C");

  delay(500);
  lcd.clear();

}

ISR(ADC_vect) {}
NTC module