LED Blinking continuously with 4X4 Keypad

I try to make a program which two LED blink continuously by press A, B and C on membrane keypad 4X4. If user press A the LED will keep blinking for 1 second each, B for 5 second and C for 10 second. Since I am new with Arduino, I try it using IF statement which LED blink with given time but not continuously blink.

type or paste code here
#include <Key.h>
#include <Keypad.h>
#define LED1 10
#define LED2 11

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup() {
  Serial.begin(19200);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
}

void loop() {
 
 char customkey = customKeypad.getKey();
 if(customkey){
    if(customkey == 'A'){
                  
    conditionA();
    }
    
    else if(customkey == 'B'){
                      
    conditionB();
    }
    
    else if(customkey == 'C'){
                      
    conditionC();
    }
    
  }
}


void conditionA(){
 
    digitalWrite(LED1, HIGH);
    delay(1000);
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    delay(1000);
    digitalWrite(LED2, LOW);
}

void conditionB(){
 
    digitalWrite(LED1, HIGH);
    delay(5000);
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    delay(5000);
    digitalWrite(LED2, LOW);
}

void conditionC(){
 
    digitalWrite(LED1, HIGH);
    delay(10000);
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    delay(10000);
    digitalWrite(LED2, LOW);
}

I try to change if statement to while statement but it keep looping indefinitely. I didn't find any similar program that I can refer in the forum. Can anyone suggest the best solution to create this program. Thanks in advance.

Welcome.

Perhaps you are expecting something that is a too perfect match for what you want. You will probably never find that. But there is example code and explanations of the techniques you must use everywhere on this forum because your question is one of the most frequently asked.

You need to remove use of delay() and use millis() for timing. Others may say you need to use a state machine, but with minor changes to your code, you already have that: your customkey variable is your "state". You simply need to avoid updating that if no keypad key is being pressed. Like this:

void loop() {
 
  static char customkey;
  char newKey = customKeypad.getKey();
  if(newKey)  customkey = newKey;
  
  if(customkey == 'A'){
                  
    conditionA();
    }
    
  else if(customkey == 'B'){
                      
    conditionB();
    }
    
  else if(customkey == 'C'){
                      
    conditionC();
    }
}

Did you notice I added static to the definition of customkey? This means that the variable will not lose its value each time loop() ends: it will keep it so that the pattern can be repeated over.

I feel like these two references will help you get where you are wanting to go:

Blink without using delay()

With static modifier added with the sketch of OP, the sketch now does not sense any closed key of the Keypad at all?

Yes, it will. But you are correct to say that there was something wrong with my code changes, so thank you @GolamMostafa! I have corrected the fault now, I hope @husnie never saw the incorrect version.

1. You can make your LEDs blinking continuously one-after-another at 1-sec interval upon closing Key-A by placing the following codes in your sketch at the appropriate point. However, the sketch will not detect Key-B or Key-C due to the presence of delay() function in the conditionA() subroutine. You may use millis() function as advised by @PaulRB in post #2, which will allow inserting time delay as well as looking for closed keys.

while(1)
  conditionA();

2. millis() Version
The following sketch is a functional test of what you have wanted. The time interval is set at 200 ms, 500 ms, and 1-sec in order to see quick affect of closing Key-A, Key-B, and Key-C. You may work on it to reduce the number of code lines.

#include <Key.h>
#include <Keypad.h>
#define LED1 10
#define LED2 11

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char customkey;

void setup()
{
  Serial.begin(19200);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
}

void loop()
{
  customkey = customKeypad.getKey();
  switch (customkey)
  {
    case 'A':
      while (1)
        conditionA(200);
    case 'B':
      while (1)
        conditionA(500);
    case 'C':
      while (1)
        conditionA(1000);
  }
}

void conditionA(unsigned long t1)
{
  digitalWrite(LED1, HIGH);
  timeDelay(t1);
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, HIGH);
  timeDelay(t1);
  digitalWrite(LED2, LOW);
}

void timeDelay(unsigned long t2)
{
  unsigned long prMillis = millis();
  while (millis() - prMillis < t2)
  {
    customkey = customKeypad.getKey(); 
    switch (customkey)
    {
      case 'A':
        digitalWrite(LED1, LOW);
        digitalWrite(LED2, LOW);
        while (1)
          conditionA(200);
      case 'B':
        digitalWrite(LED1, LOW);
        digitalWrite(LED2, LOW);
        while (1)
          conditionA(500);
      case 'C':
        digitalWrite(LED1, LOW);
        digitalWrite(LED2, LOW);
        while (1)
          conditionA(1000);
    }
  }
}

Thanks @GolamMostafa . Your solution work fine. Its exactly the program that I need. So now I need to study deeper on millis() function. Thank a lot. Thanks also to all that response. You guys are great.