Hae . am having trouble ending the while loop, when the sensor value reaches threashold the lcd displays fire alarm but when the value is less than threshold the lcd still display the same text

void loop()

{
Flame_detected =analogRead(Flame_sensor);

digitalWrite(relay_pin, HIGH);

while (Flame_detected <=threshold)
{
lcd.clear();

lcd.setCursor(1, 0);

lcd.print("!ALERT!ALERT!");

lcd.setCursor(0, 1);

lcd.print("Fire in the house");

delay(2000);
myservo.read();
if ( pos == 0) {

  myservo.attach(10);
  myservo.write(100);

  delay(200);
} else {
 
  myservo.detach();
  myservo.write(0);
  delay(200);
}


Serial.println("Flame detected...! take action immediately.");
while (sms_count < 2) //Number of SMS Alerts to be sent limited at 3
{
  alertMessage(); // Function to send AT Commands to GSM module
}
delay(500);

}
key_pressed = keypad_key.getKey();

if (key_pressed == '#')

change();

if (key_pressed)

{

password[i++] = key_pressed;

lcd.print(key_pressed);
tone(buzzer, 800, 200);

}

if (i == 4)

{

delay(200);

for (int j = 0; j < 4; j++)

  initial_password[j] = EEPROM.read(j);

if (!(strncmp(password, initial_password, 4)))

{
  tone(buzzer, 800, 200);
  delay(300);
  lcd.clear();

  lcd.print("Pass Accepted");

  digitalWrite(relay_pin, LOW);

  delay(2000);

  lcd.setCursor(0, 1);

  lcd.print("Pres # to change");

  delay(2000);

  lcd.clear();

  lcd.print("Enter Password:");

  lcd.setCursor(0, 1);

  sms_count = 0;

  i = 0;




}

else

{

  digitalWrite(relay_pin, HIGH);

  while (sms_count < 2) //Number of SMS Alerts to be sent limited at 3
  {
    passMessage(); // Function to send AT Commands to GSM module
  }


  lcd.clear();

  lcd.print("Wrong Password");

  lcd.setCursor(0, 1);

  lcd.print("Pres # to Change");

  delay(2000);

  lcd.clear();

  lcd.print("Enter Password");

  lcd.setCursor(0, 1);

  i = 0;




}

}

}

void change()

{

int j = 0;

lcd.clear();

lcd.print("Current Password");

lcd.setCursor(0, 1);

while (j < 4)

{

char key = keypad_key.getKey();

if (key)

{

  new_password[j++] = key;

  lcd.print(key);



}

key = 0;

}

delay(500);

if ((strncmp(new_password, initial_password, 4)))

{

lcd.clear();

lcd.print("Wrong Password");

lcd.setCursor(0, 1);

lcd.print("Try Again");

delay(1000);

}

else

{

j = 0;

lcd.clear();

lcd.print("New Password:");

lcd.setCursor(0, 1);

while (j < 4)

{

  char key = keypad_key.getKey();

  if (key)

  {

    initial_password[j] = key;

    lcd.print(key);

    EEPROM.write(j, key);

    j++;



  }

}

lcd.print("Pass Changed");

delay(1000);

}

lcd.clear();

lcd.print("Enter Password");

lcd.setCursor(0, 1);

key_pressed = 0;

}

void initialpassword() {

for (int j = 0; j < 4; j++)

EEPROM.write(j, j + 49);

for (int j = 0; j < 4; j++)

initial_password[j] = EEPROM.read(j);

}

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

You don't appear to be updating Flame_detected inside the while loop

shouldn't there be two actions depending on the value of the flame sensor.

the condition of the while will never change once it's true because the sensor is not read within the loop

consider

const byte pinSensor = A2;
const int  Threshold = 500;

int   sms_count = 0;

void loop()
{
    if (Threshold < analogRead (pinSensor))  {
        Serial.println ("all clear");
        sms_count = 0;
    }
    else  {
        Serial.println ("Alert");

        if (sms_count++ < 3)
            Serial.println (" send sms");
    }
    delay (500);
}

void setup ()
{
    Serial.begin (9600);
}