state = true/false issue

Im having an issue with “bool state;” in this code. state gets declared as true after temperature is set… however it stays true.
In line 123, state is supposed to become false. It doesnt seem to be changing state as per serial monitor and lcd.
Another thing that is odd is that line 129 executes. I know because the green LED comes on.

Any clues to why state is not changing? I’m probably doing something wrong like usual lol
currently I have the project plugged in and its operational. I’m just trying to smooth the operation and user friendly-ness
thanks in advance.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

// Pins
int TR_PIN = A1;  // Thermistor
int SetPin = A3 ; // Pot
int LED_B = 9;    // PWM capable
int LED_G = 10;   // PWM capable
int LED_R = 11;   // PWM capable
int relayPin = 3; // PWM capable
const byte relaySensor = 4;   //
const byte button = 2;        // toggle switch
const byte statePin = 13;     // onboard LED

// Constants
int SensorTimer = 15000;  // 15 seconds

//Variables
int sensorVal;
int setVal;
int sensorTemp;
int setTemp;
int tempDiffPos;
int tempDiffNeg;
int count;
int sensorCount;
int MainsPollVal;
int MainsPollValA;
bool State;
float realTemp;
float trueTemp;
double a1, b1, c1, d1, r2, r1, vo, tempC, tempF, tempK;

unsigned long TimeNow;
unsigned long LastTime;

void setup() {
  // put your setup code here, to run once:
  lcd.begin(16, 2);
  Serial.begin(9600);
  pinMode(statePin, LED_BUILTIN);
  pinMode(LED_B, OUTPUT);
  pinMode(LED_G, OUTPUT);
  pinMode(LED_R, OUTPUT);
  pinMode(SetPin, INPUT);
  pinMode(relayPin, OUTPUT);
  pinMode(relaySensor, INPUT);
  pinMode(button, INPUT);
  pinMode(TR_PIN, INPUT);

  a1 = 3.354016E-03 ;
  b1 = 2.569850E-04 ;
  c1 = 2.620131E-06 ;
  d1 = 6.383091E-08 ;
  r1 = 9720.0;

  count = 0;
  sensorCount = 0;
  tempDiffPos = 0.00;
  tempDiffNeg = 0.00;
}

// MAIN LOOP
void loop() {
// show state ture
  if (State == true) {
    lcd.setCursor(0, 1);
    lcd.print("^");
  }
// show state false
  if (State == false) {
    lcd.setCursor(0, 1);
    lcd.print(" ");
  }
// check sensor
  TimeNow = millis();
  if (LastTime <= TimeNow - SensorTimer) {
    sensorPoll();
    LastTime = TimeNow;
    Serial.print(State);
  }

  tempDiffPos = (trueTemp - setTemp);
  tempDiffNeg = (setTemp - trueTemp);

  lcd.setCursor(0, 0);
  lcd.print("SENSOR      ");
  lcd.setCursor(12, 0);
  lcd.print(trueTemp);
  lcd.setCursor(1, 1);
  lcd.print(" SET        ");
  lcd.setCursor(14, 1);
  lcd.print(setTemp);
  //Serial.print(tempF);
  //Serial.print("  ");

  while (digitalRead(button) == HIGH) {
    count = 0;
    MainsPollVal = 0;
    SetTempVal();
  }
  while ((setTemp == 0) && (digitalRead(button) == LOW)) {
    Start_Up();
  }

  if (((MainsPollVal == 10) || (MainsPollVal - MainsPollValA == 10)) && (State == true)) {
    MainsPollValA = MainsPollVal;
    Fault();
  }


  // COOLING
  if ((trueTemp - setTemp >= 0) && (State == true)) {
    digitalWrite(relayPin, HIGH);
    digitalWrite(LED_G, LOW);
    digitalWrite(LED_R, LOW);
    delay(25);
    MainsCheck();
    if (setTemp - trueTemp >= 1) {
      digitalWrite(relayPin, LOW);
      MainsPollVal = 0;
      MainsPollValA = 0;
      State = false;
      lcd.clear();
    }
    Fade_Blue();
  }
  //GOOD TEMP
  if ((((setTemp - trueTemp > 0) && (setTemp - trueTemp < 1.75)) || ((trueTemp - setTemp > 0) && (trueTemp - setTemp < 1.75)) && (State == false))) {
    digitalWrite(LED_B, LOW);
    digitalWrite(LED_R, LOW);
    digitalWrite(relayPin, LOW);
    if (trueTemp - setTemp > 1.8) {
      State = true;
    }
    Fade_Green();
  }
  // HOT TEMP
  if (setTemp - trueTemp > 1.8) {
    digitalWrite(LED_B, LOW);
    digitalWrite(LED_G, LOW);
    Fade_Red();
  }
}
//StartUP
void Start_Up() {
  lcd.setCursor(0, 0);
  lcd.print(" !  Mmmmmmmm  ! ");
  lcd.setCursor(0, 1);
  lcd.print("  KOEL'D  BEER  ");
  digitalWrite(relayPin, LOW);
  analogWrite(LED_B, random(10, 255));
  delay(10);
  analogWrite(LED_G, random(10, 255));
  delay(10);
  analogWrite(LED_R, random(10, 255));
  delay(10);
  State = true;
}
// TEMP SENSOR VALUE
// read temp sensor and return temp value
int sensorPoll() {
  // READ REAL TEMP
  sensorCount++;
  vo = analogRead(TR_PIN);
  vo = vo / (1023.0 / 5.0);

  // voltage divider calculation
  // vo = 5 * r2 /(r1+r2)
  // solve for r2
  // get the exact value for voltage divider r1

  r2 = ( vo * 7840) / (5.0 - vo);

  //equation from data sheet
  tempK = 1.0 / (a1 + (b1 * (log(r2 / 32500))) + (c1 * pow(log(r2 / 20040), 2.0)) + (d1 * pow(log(r2 / 11210), 3.0)));
  tempC  = (tempK - 273.15);
  tempF  = ((tempC * 1.8) + 32.0) * 10;
  realTemp = map(tempF, 1350, 860, 350, 740);
  trueTemp = realTemp / 10;
  return trueTemp;
}
// SET TEMPURATURE
// read potentiometer for setting temperature
int SetTempVal() {
  lcd.setCursor(0, 0);
  lcd.print("  KOEL'D  BEER  ");
  lcd.setCursor(0, 1);
  lcd.print(" SET TEMP     ");
  setVal = analogRead(SetPin);
  setTemp = map (setVal, 2.0, 880.0, 28.0, 80.0);   // change 120 to 80
  lcd.setCursor(14, 1);
  lcd.print(setTemp);
  delay(100);
  return setTemp;
}
//LED FADE
void Fade_Blue() {
  for (int b = 15; b <= 255; b++) {
    analogWrite(LED_B, b);
    delay(5);
  }
  for (int b = 255; b >= 15; b--) {
    analogWrite(LED_B, b);
    delay(5);
  }
}
void Fade_Green() {
  for (int g = 15; g <= 255; g++) {
    analogWrite(LED_G, g);
    delay(5);
  }
  for (int g = 255; g >= 15; g--) {
    analogWrite(LED_G, g);
    delay(5);
  }
}
void Fade_Red() {
  for (int r = 15; r <= 255; r++) {
    analogWrite(LED_R, r);
    delay(5);
  }
  for (int r = 255; r >= 15; r--) {
    analogWrite(LED_R, r);
    delay(5);
  }
}
// CHECK MAINS SENSOR
// check mains voltage sensor for voltage
int MainsCheck() {
  if (digitalRead(relaySensor) == LOW) {
    digitalWrite(relayPin, HIGH);
    MainsPollVal++;
    delay(20);
    return MainsPollVal;
  }
}
//FAULT
void Fault() {
  lcd.setCursor(0, 0);
  lcd.print("     SYSTEM     ");
  lcd.setCursor(0, 1);
  lcd.print("     FAULT!     ");
  delay(1000);
  lcd.clear();
  digitalWrite(LED_R, HIGH);
  delay(150);
  digitalWrite(LED_R, LOW);
  delay(150);
  digitalWrite(LED_R, HIGH);
  delay(150);
  digitalWrite(LED_R, LOW);
  delay(150);
  digitalWrite(LED_R, HIGH);
  delay(150);
  digitalWrite(LED_R, LOW);
  delay(150);
}

How do you conclude it is not being set?

Your Serial.print is only updated every 15 seconds, your LCD every time through loop() (ie, hundreds of times a second). LCD update is way too fast to see and serial print value could have changed many times in between those updates.

If the code is running (LED was turned on) then the value was set and is probably reset by the time you get to the serial update. I doubt that you will notice the change on the LCD.

I would try changing the serial update to be more frequent and see if that makes a difference, as a start. You can also not print the space on the LCD and just put on a ^ when it changes to true.

should I update the LCD with each sensor poll?
after some further research and another hour and half of staring at my screen, I found the issue with line 129… my parentheses were incorrect so it wasnt even considering the && false. and I think on line 122 since the parameter wasnt within the “parent” if statement parameters, I think it was being ignored?

should I change the if to while?

A general rule with LCD modules is to update them when the data displayed changes unless that is too frequent. They do not update quickly and rapid updates dim the display and make then harder to read.

I think on line 122 since the parameter wasnt within the "parent" if statement parameters

Not sure what this means but if it now works as expected, then that may have been your problem?

should I change the if to while?

Should you change the avocado into a pineapple :)? 'if' and 'while' are very different constructs and do different things, so you need to be clear about your intent.

Line 122 is a "nested" if statement. What I referred to as the parent is the original statement that line 122 falls under. It did seem to help my code, I'm having a couple more little issues regarding my fade functions and my project seems to get stuck in the for loop. The LED will stay at a specific pwm and fail to complete its loop.

SOLVED… it seems to be working perfectly. I did change some ifs to whiles.
here is the updated code.
thanks to all who took the time to read.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

// Pins
int TR_PIN = A1;  // Thermistor
int SetPin = A3 ; // Pot
int LED_B = 9;    // PWM capable
int LED_G = 10;   // PWM capable
int LED_R = 11;   // PWM capable
int relayPin = 3; // PWM capable
const byte relaySensor = 4;   //
const byte button = 2;        // toggle switch

// Constants
int SensorTimer = 7000;  // 7  seconds

//Variables
int sensorVal;
int setVal;
int sensorTemp;
int setTemp;
int tempDiffPos;
int tempDiffNeg;
int count;
int sensorCount;
int MainsPollVal;
int MainsPollValA;
bool State;
float realTemp;
float trueTemp;
double a1, b1, c1, d1, r2, r1, vo, tempC, tempF, tempK;

unsigned long TimeNow;
unsigned long LastTime;

void setup() {
  // put your setup code here, to run once:
  lcd.begin(16, 2);
  Serial.begin(9600);

  pinMode(LED_B, OUTPUT);
  pinMode(LED_G, OUTPUT);
  pinMode(LED_R, OUTPUT);
  pinMode(SetPin, INPUT);
  pinMode(relayPin, OUTPUT);
  pinMode(relaySensor, INPUT);
  pinMode(button, INPUT);
  pinMode(TR_PIN, INPUT);

  a1 = 3.354016E-03 ;
  b1 = 2.569850E-04 ;
  c1 = 2.620131E-06 ;
  d1 = 6.383091E-08 ;
  r1 = 9720.0;

  tempDiffPos = 0.00;
  tempDiffNeg = 0.00;
}

// MAIN LOOP
void loop() {

  while ((setTemp == 0) && (digitalRead(button) == LOW)) {
    Start_Up();
  }

  while (digitalRead(button) == HIGH) {
    count = 0;
    MainsPollVal = 0;
    MainsPollValA = 0;
    SetTempVal();
  }

  // show state ture
  if (State == true) {
    lcd.setCursor(0, 1);
    lcd.print("^");
  }

  // check sensor
  TimeNow = millis();
  if (LastTime <= TimeNow - SensorTimer) {
    sensorPoll();
    LastTime = TimeNow;
    Serial.print(State);
  }

  tempDiffPos = (trueTemp - setTemp);
  tempDiffNeg = (setTemp - trueTemp);

  while ((MainsPollVal == 10) || (MainsPollVal - MainsPollValA == 10)) {
    MainsPollValA = MainsPollVal;
    Fault();
    if (digitalRead(button) == HIGH) {
      break;
    }
  }


  // COOLING
  while ((trueTemp - setTemp >= 0) && (State == true)) {
    digitalWrite(relayPin, HIGH);
    // digitalWrite(LED_B, HIGH);
    digitalWrite(LED_G, LOW);
    digitalWrite(LED_R, LOW);
    delay(25);
    Display();
    MainsCheck();
    Fade_Blue();
    // check sensor
    TimeNow = millis();
    if (LastTime <= TimeNow - SensorTimer) {
      sensorPoll();
      LastTime = TimeNow;
      Serial.print(State);
    }
     if (digitalRead(button) == HIGH) {
      break;
    }
    if (setTemp - trueTemp >= 0) {
      digitalWrite(relayPin, LOW);
      MainsPollVal = 0;
      MainsPollValA = 0;
      State = false;
      lcd.clear();
      break;
    }
    if ((MainsPollVal == 10) || (MainsPollVal - MainsPollValA == 10)) {
      MainsPollValA = MainsPollVal;
      Fault();
      break;
    }

  }
  //GOOD TEMP
  while ((((setTemp - trueTemp > 0) && (setTemp - trueTemp < 1.75)) || ((trueTemp - setTemp > 0) && (trueTemp - setTemp < 1.75))) && (State == false))  {
    // digitalWrite(LED_G, HIGH);
    digitalWrite(LED_B, LOW);
    digitalWrite(LED_R, LOW);
    digitalWrite(relayPin, LOW);
    Display();
    Fade_Green();
    // check sensor
    TimeNow = millis();
    if (LastTime <= TimeNow - SensorTimer) {
      sensorPoll();
      LastTime = TimeNow;
      Serial.print(State);
    }
     if (digitalRead(button) == HIGH) {
      break;
    }
    if (trueTemp - setTemp > 1.6) {
      State = true;
      break;
    }
  }

  // HIGH SET TEMP
  if (setTemp - trueTemp > 1.75) {
    // digitalWrite(LED_R, HIGH);
    digitalWrite(LED_B, LOW);
    digitalWrite(LED_G, LOW);
    digitalWrite(relayPin, LOW);
    // State = false;
    Fade_Red();
  }
}
//StartUP
void Start_Up() {
  lcd.setCursor(0, 0);
  lcd.print(" !  Mmmmmmmm  ! ");
  lcd.setCursor(0, 1);
  lcd.print("  KOEL'D  BEER  ");
  digitalWrite(relayPin, LOW);
  analogWrite(LED_B, random(10, 255));
  delay(50);
  analogWrite(LED_G, random(10, 255));
  delay(50);
  analogWrite(LED_R, random(10, 255));
  delay(50);
}
// TEMP SENSOR VALUE
// read temp sensor and return temp value
int sensorPoll() {
  // READ REAL TEMP
  sensorCount++;
  vo = analogRead(TR_PIN);
  vo = vo / (1023.0 / 5.0);

  // voltage divider calculation
  // vo = 5 * r2 /(r1+r2)
  // solve for r2
  // get the exact value for voltage divider r1

  r2 = ( vo * 7840) / (5.0 - vo);

  //equation from data sheet
  tempK = 1.0 / (a1 + (b1 * (log(r2 / 32500))) + (c1 * pow(log(r2 / 20040), 2.0)) + (d1 * pow(log(r2 / 11210), 3.0)));
  tempC  = (tempK - 273.15);
  tempF  = ((tempC * 1.8) + 32.0) * 10;
  realTemp = map(tempF, 1350, 860, 350, 740);
  trueTemp = realTemp / 10;
  return trueTemp;
}
// SET TEMPURATURE
// read potentiometer for setting temperature
int SetTempVal() {
  lcd.setCursor(0, 0);
  lcd.print("  KOEL'D  BEER  ");
  lcd.setCursor(0, 1);
  lcd.print(" SET TEMP     ");
  setVal = analogRead(SetPin);
  setTemp = map (setVal, 2.0, 880.0, 28.0, 80.0);   // change 120 to 80
  lcd.setCursor(14, 1);
  lcd.print(setTemp);
  delay(100);
  State = true;
  return setTemp;
}
//LED FADE
void Fade_Blue() {
  for (int b = 15; b <= 255; b++) {
    analogWrite(LED_B, b);
    delay(5);
  }
  for (int b = 255; b >= 15; b--) {
    analogWrite(LED_B, b);
    delay(5);
  }
}
void Fade_Green() {
  for (int g = 15; g <= 255; g++) {
    analogWrite(LED_G, g);
    delay(5);
  }
  for (int g = 255; g >= 15; g--) {
    analogWrite(LED_G, g);
    delay(5);
  }
}
void Fade_Red() {
  for (int r = 15; r <= 255; r++) {
    analogWrite(LED_R, r);
    delay(5);
  }
  for (int r = 255; r >= 15; r--) {
    analogWrite(LED_R, r);
    delay(5);
  }
}
// CHECK MAINS SENSOR
// check mains voltage sensor for voltage
int MainsCheck() {
  if (digitalRead(relaySensor) == LOW) {
    digitalWrite(relayPin, HIGH);
    MainsPollVal++;
    delay(20);
    return MainsPollVal;
  }
}
//FAULT
void Fault() {
  digitalWrite(LED_B,LOW);
  lcd.setCursor(0, 0);
  lcd.print("     SYSTEM     ");
  lcd.setCursor(0, 1);
  lcd.print("     FAULT!     ");
  digitalWrite(LED_R, HIGH);
  delay(500);
  digitalWrite(LED_R, LOW);
  delay(500);
}

void Display() {
  lcd.setCursor(0, 0);
  lcd.print("SENSOR      ");
  lcd.setCursor(12, 0);
  lcd.print(trueTemp);
  lcd.setCursor(1, 1);
  lcd.print("  SET        ");
  lcd.setCursor(14, 1);
  lcd.print(setTemp);
  // show state ture
  if (State == true) {
    lcd.setCursor(0, 1);
    lcd.print("^");
  }
}