How to make a button run different codes when pushed

Hello
I am using a LCD Keypad shield and need the select button to switch between codes when pushed. So it should run one bit of code and then when pushed, run another section of code. Is this the right way to go about this?
Here is a simplified version of the code.
Many thanks for any help.

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);


#define btnSELECT 4
#define btnNONE   5

int Mopin1=3;
int Mopin2=12;
int MoSpeedpin=11;
int ThermistorPin = 1;

boolean Mode= 0;

void setup() { {
  pinMode(Mopin1,OUTPUT);
  pinMode(Mopin2,OUTPUT);
  pinMode(MoSpeedpin,OUTPUT);
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  
}

 
      
int read_LCD_buttons()
{
  int adc_key_in = analogRead(0);
  if (adc_key_in > 1000) return btnNONE;
  if (adc_key_in < 790)  return btnSELECT;
  return btnNONE;
  }


   }

void loop() {

 int lcd_key;
  lcd_key = read_LCD_buttons();
   if (lcd_key == btnSELECT) {

    delay(500);
    if (Mode == 0){
      Mode = 1;}
      else Mode = 0;
   }

if (Mode == 0) {

 lcd.setCursor(5,0);
      lcd.print("NO");
}
  

if (Mode == 1); {
lcd.setCursor(10,0);
  lcd.print("YES");}
}

Is this the right way to go about this?

Do you want to select what to run only at power up or reset or do you want to be able to change what is running at other times ?

I note that the code posted does not compile

int read_LCD_buttons()
{
  int adc_key_in = analogRead(0);
  if (adc_key_in > 1000) return btnNONE;
  if (adc_key_in < 790)  return btnSELECT;
  return btnNONE;
  }

So, in other words...

int read_LCD_buttons()
{
   int adc_key_in = analogRead(A0);
   if (adc_key_in < 790)
     return btnSELECT;
   else
     return btnNONE;
}

Is that what you wanted to do?

UKHeliBob:
Do you want to select what to run only at power up or reset or do you want to be able to change what is running at other times ?

I note that the code posted does not compile

To change during operation

The basic approach is as follows:

if (button_is_pushed()) {
do_something();
}
else {
do_something_else();
}

To change during operation

Then it is no good reading the input state only in setup(), you need to read it often in loop() which, in turn means not slowing down the repetition rate of loop() using delay() statements or while loops.

In any case, your requirements are not clear. Do you want to be able to switch between the sections of code being executed by holding the button down or releasing it or do you want to switch by momentarily pressing the button ?

You really do need to state your requirements clearly

UKHeliBob:
Then it is no good reading the input state only in setup(), you need to read it often in loop() which, in turn means not slowing down the repetition rate of loop() using delay() statements or while loops.

In any case, your requirements are not clear. Do you want to be able to switch between the sections of code being executed by holding the button down or releasing it or do you want to switch by momentarily pressing the button ?

You really do need to state your requirements clearly

I accidentally put that statement in the setup instead of loop i have changed this in the question, apologies. The button is pushed down and released.

i have changed this in the question

That is bad manners because it makes nonsense of subsequent comments

he button is pushed down and released.

Sorry, but I am still not clear which way you want it to work. Please describe the actions that will cause the program to change the code that it is running