Momentary push buttons to run the program continuously

I am trying to write a sketch for four momentary push buttons where-in I want to run a statement continuously when a particular button is pressed once. When I press another push button , then programm statement against that button should be played continuously.

Below is the sketch , but in that case I need to keep the push button pressed continuously to run the statement continuously. Moment I release the button, the program statement stops immediately. Can you plz help on below code and modify it for desired functionality?
Thanks,


int value = 0;
 
void setup() {

Serial.begin(9600);
pinMode (3,INPUT);
pinMode (4,INPUT);
pinMode (5,INPUT);
pinMode (6,INPUT);

}

void loop() {
    
   
    if (digitalRead(3) > 0) 
    {
       
      Serial.println("A Button is  Pressed");
      delay(1000);
    }

   else if  (digitalRead(4) > 0) 
    {
      Serial.println("B Button is Pressed");
      delay(1000);
    }

    else if  (digitalRead(5)> 0) 
    {
      Serial.println("C Button is Pressed");
      delay(1000);
    }
   else  
  Serial.println("D Button is Pressed");
      delay(1000);
}

Open the IDE, and look for the "state change detection" example sketch.

StateChangeDetection

Thanks , but it didn’t help me… managed with while loop and attachedinterupt command.

Can you show your code for the benefit of others?

Here is an implementation using state change detection. It uses INPUT_PULLUP so you don't need resistors. I can make it even shorter using arrays but I thought this may be more clear to you:

const int buttonAPin = 3;
const int buttonBPin = 4;
const int buttonCPin = 5;
const int buttonDPin = 6;

enum func_t
{
  FUNCTION_A,
  FUNCTION_B,
  FUNCTION_C,
  FUNCTION_D,
  NO_FUNCTION
};

func_t currFunc = NO_FUNCTION;

void setup()
{
  Serial.begin(9600);
  pinMode (buttonAPin, INPUT_PULLUP);
  pinMode (buttonBPin, INPUT_PULLUP);
  pinMode (buttonCPin, INPUT_PULLUP);
  pinMode (buttonDPin, INPUT_PULLUP);
}

void loop()
{
  static int lastButtonA = digitalRead(buttonAPin);
  static int lastButtonB = digitalRead(buttonBPin);
  static int lastButtonC = digitalRead(buttonCPin);
  static int lastButtonD = digitalRead(buttonDPin);

  int currButtonA = digitalRead(buttonAPin);
  int currButtonB = digitalRead(buttonBPin);
  int currButtonC = digitalRead(buttonCPin);
  int currButtonD = digitalRead(buttonDPin);
  
  if (currButtonA != lastButtonA)
  {
    delay(50); //debounce
    lastButtonA = currButtonA;
    if (currButtonA == LOW) currFunc = FUNCTION_A;
  }
  else if (currButtonB != lastButtonB)
  {
    delay(50); //debounce
    lastButtonB = currButtonB;
    if (currButtonB == LOW) currFunc = FUNCTION_B;
  }
  else if (currButtonC != lastButtonC)
  {
    delay(50); //debounce
    lastButtonC = currButtonC;
    if (currButtonC == LOW) currFunc = FUNCTION_C;
  }
  else if (currButtonD != lastButtonD)
  {
    delay(50); //debounce
    lastButtonD = currButtonD;
    if (currButtonD == LOW) currFunc = FUNCTION_D;
  }

  switch (currFunc)
  {
    case FUNCTION_A:
      Serial.println("Executing Function A");
      break;
    
    case FUNCTION_B:
      Serial.println("Executing Function B");
      break;

    case FUNCTION_C:
      Serial.println("Executing Function C");
      break;

    case FUNCTION_D:
      Serial.println("Executing Function D");
      break;
  }
}

Here is an implementation using state change detection and arrays:

enum func_t
{
  FUNCTION_A,
  FUNCTION_B,
  FUNCTION_C,
  FUNCTION_D,
  NO_FUNCTION
};

const int buttonPins[] = {3, 4, 5, 6};
const int NUM_BUTTONS = sizeof (buttonPins)/sizeof(int);
const func_t functions[] = {FUNCTION_A, FUNCTION_B, FUNCTION_C, FUNCTION_D};

func_t currFunc = NO_FUNCTION;
int lastButtonStat[NUM_BUTTONS];

  
void setup()
{
  Serial.begin(9600);
  for (int i=0; i < NUM_BUTTONS; i++) 
  {
    pinMode (buttonPins[i], INPUT_PULLUP);
    lastButtonStat[i] = digitalRead(buttonPins[i]);
  }
}

void loop()
{
  int currButtonStat;
  
  for (int i=0; i < NUM_BUTTONS; i++) 
  {
    currButtonStat = digitalRead(buttonPins[i]);
    if (currButtonStat != lastButtonStat[i])
    {
      delay(50); // debounce
      lastButtonStat[i] = currButtonStat;
      if (currButtonStat == LOW) currFunc = functions[i];
    }
  }

  switch (currFunc)
  {
    case FUNCTION_A:
      Serial.println("Executing Function A");
      break;
    
    case FUNCTION_B:
      Serial.println("Executing Function B");
      break;

    case FUNCTION_C:
      Serial.println("Executing Function C");
      break;

    case FUNCTION_D:
      Serial.println("Executing Function D");
      break;
  }
}