How to make the program loop until i pushed another button?

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);      // select the pins used on the LCD panel

void setup(){
   lcd.begin(16, 2);                     // start the library
   lcd.setCursor(0,0);                   // set the LCD cursor   position 
   lcd.print("LPG Gas Sensor");          // Main Display
   lcd.setCursor(0,1);                   // set the LCD cursor   position 
   lcd.print("by BSIC 2015");            // Main Display 2
   delay(2000);
   lcd.clear();
}
 
void loop(){
  lcd.setCursor(0,0);                   // move to the begining of the first line
  lcd.print("Main Menu");
  lcd.setCursor(0,1);                   // move to the begining of the second line
  lcd.print("<Test>   <About>");
  int adc_in;
  adc_in = analogRead(0);
  
  if (adc_in <= 250 && adc_in >=200){
            lcd.clear();
            lcd.print("LPG Gas Sensor"); // Main Display
            lcd.setCursor(0,1);          // set the LCD cursor   position 
            lcd.print("by BSIC 2015");   // Main Display 2
            delay(2000);
            lcd.clear();
       }
  if (adc_in >= 400 && adc_in <=450){
    do{
      delay(500);
         int sens_val;
         int lvl;
         sens_val=analogRead(1);
         lcd.clear();
         if (sens_val > 1000){           
           lcd.print("WARNING:Gas Leak");
           lcd.setCursor(0,1);
           lcd.print("Detected LVL>: ");
             if (sens_val >= 1000){
             lvl = 9;
             lcd.print(lvl);
             delay(500);
             }
             if (sens_val >= 900){
             lvl = 8;
             lcd.setCursor(15,1);
             lcd.print(lvl);
             delay(500);
             }
             if (sens_val >= 800){
             lvl = 7;
             lcd.setCursor(15,1);
             lcd.print(lvl);
             delay(500);
             }
             if (sens_val >= 700){
             lvl = 6;
             lcd.setCursor(15,1);
             lcd.print(lvl);
             delay(500);
             }
             if (sens_val >= 600){
             lvl = 5;
             lcd.setCursor(15,1);
             lcd.print(lvl);
             }
             if (sens_val >= 500){
             lvl = 4;
             lcd.setCursor(15,1);
             lcd.print(lvl);
             delay(500);
             }
             if (sens_val >= 400){
             lvl = 3;
             lcd.setCursor(15,1);
             lcd.print(lvl);
             delay(500);
             }
             if (sens_val >= 300){
             lvl = 2;
             lcd.setCursor(15,1);
             lcd.print(lvl);
             delay(500);
             }
             else{
               lcd.clear();
               lcd.print("No Gas Leak");
               lcd.setCursor(0,1);
               lcd.print("Detected");
               delay(500);
               lcd.clear();             
             }
           }
           else{
             lcd.clear();
             lcd.print("No Gas Leak");
             lcd.setCursor(0,1);
             lcd.print("Detected");
             delay(500);
             lcd.clear();
           }
    }
    while (adc_in <= 50);
       }
       
     }

Here is my code for arduino lpg gas detector. When I press the key for test the test goes on but once I removed my hand from pressing the button the test returns to main menu.

What i’m trying to do is when i press it and release the button, the test should continue and halt if I press the button again. Im trying to do it with do while but doesn’t work. May adc_in is zero when no keys press.

Hope someone can teach or help me. Thank you in advance.

You need to detect the change in the push button when it is pressed this time and not pressed the previous time. You use that test to toggle ( invert ) a boolean variable or flag as we say. Use this flag to decide if you are in the test mode.

There is an example of how to do this included with the IDE. See the StateChangeDetection example.

UKHeliBob: There is an example of how to do this included with the IDE. See the StateChangeDetection example.

There is, I looked at it before I made the first post as I was going to suggest it, but as examples go it is not very clear.

Here is my code for arduino lpg gas detector. When I press the key for test the test goes on but once I removed my hand from pressing the button the test returns to main menu.

Am I missing something here?
Where in the code are you looking at the push button?

while (adc_in <= 50);
       }
       
     }

while (adc_in <= 50);
Where is adc_in updated? :-[

UKHeliBob:
There is an example of how to do this included with the IDE. See the StateChangeDetection example.

as Grumpy_Mike said I can’t apply the example sir. My programming skills is not yet that good. Tried integrating the idea in the example but i can’t make it work.

Grumpy_Mike:
You need to detect the change in the push button when it is pressed this time and not pressed the previous time. You use that test to toggle ( invert ) a boolean variable or flag as we say. Use this flag to decide if you are in the test mode.

can you give me an example here sir? I can’t understand that well.

LarryD:
Am I missing something here?
Where in the code are you looking at the push button?

while (adc_in <= 50);

}
     
    }




~~while (adc_in <= 50);~~
~~Where is adc_in updated?~~ :-[

I thought it would run because adc_in is zero when no buttons are push. :D. Tried it many times and won’t work.

ried integrating the idea in the example but i can't make it work.

So the way this forum works is that you post the code where you tried to implement that example and we tell you where you have gone wrong.