Strange Temperature Sensor

I wrote a temperature sensing program.

const int rLed = 3;
const int gLed = 2;
const int tempSensor = A0;

void setup() {
  
pinMode(rLed, OUTPUT);
pinMode(gLed, OUTPUT);
pinMode(9, INPUT);
Serial.begin(9600);
}

    boolean debounce(void)
    {
      static boolean buttonState=LOW;
      static boolean lastButtonState;
      boolean currentState = digitalRead(4);
      static unsigned long lastDebounceTime;
      if (currentState != lastButtonState){ //if the button state has changed
        lastDebounceTime=millis();  //reset timer
        }
     
      if ((millis()-lastDebounceTime) > 50)  //if 50 milliseconds has passed since last bounce
      {  
        buttonState = currentState;  //read value again now that bouncing is over
        
        }
      lastButtonState = currentState;
      return buttonState;
    } 

void loop() {

int temp = analogRead(tempSensor);
float tempVolt = ((5000.0*temp)/1023.0);
float tempRead = ((9.0*tempVolt)/50.0 - 58.0);

if(tempRead > 71){
 
  if(tempRead < 82){
  digitalWrite(gLed,HIGH);
  digitalWrite(rLed,LOW);
  }

  }
else{
  digitalWrite(rLed,HIGH);
  digitalWrite(gLed,LOW);
  }
  
//Debounce Function
  boolean lastButtonVal;
  boolean buttonVal = debounce();
  static unsigned long lastPrint;
  if (buttonVal == HIGH && lastButtonVal == LOW){ 
  if(millis() - lastPrint > 1000){
  Serial.print("F =");
  Serial.println(tempRead);
  Serial.print("D =");
  Serial.println(temp);
  Serial.print("mV =");
  Serial.println(tempVolt);
  Serial.println();
  lastButtonVal = buttonVal;
  lastPrint = millis();
  }
 
  }
  
}

It works great as long as the temperature is below 82 degrees, however when the actual temperature is above 82 everything goes weird. First, the led won’t turn red like its supposed to, and second, if I reset the program when the actual temperature is 82 degrees, both LEDs do nothing until I push the button. This makes absolutely no sense to me. How does the button affect the LEDs at all? Thanks in advance for the help.

pinMode(9, INPUT);
 boolean currentState = digitalRead(4);

Which pin is connected to the button? Is there a pull up or pull down resistor?

when the actual temperature is above 82 everything goes weird. First, the led won’t turn red like its supposed to

if(tempRead > 71){
 
  if(tempRead < 82){
  digitalWrite(gLed,HIGH);
  digitalWrite(rLed,LOW);
  }

  }
else{
  digitalWrite(rLed,HIGH);
  digitalWrite(gLed,LOW);
  }

Because of the bracketing, The else condition currently applys to temperatures 71 degrees and lower. Try

if(tempRead > 71)
{
  if(tempRead < 82)
  {
   digitalWrite(gLed,HIGH);
   digitalWrite(rLed,LOW);
   }

   else
  {
  digitalWrite(rLed,HIGH);
  digitalWrite(gLed,LOW);
  }
}

Or, another way of writing it:-

if (tempRead > 71 and tempRead < 82)
{
    digitalWrite(gLed, HIGH);
    digitalWrite(rLed, LOW);
}
else
{
    digitalWrite(rLed, HIGH);
    digitalWrite(gLed, LOW);
}