Wait until press a specific button to continue [Keypad]

Hi everyone !

I'm new in the arduino's world (and i'm frensh so sorry for my bad English) and I've a programmation problem.

I want to make blink the led 3 times in the same color when it's light.

How can I make a pause until I press a button on Keypad ?

I've try the do/while but it don't listen the Keypad or don't get stuck by the loop and continue ..

Here is my programme that work but without the option I want to add :

#include <Keypad.h>
#define RED 12
#define GREEN 11
#define BLUE 10

int led=12;

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[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

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

void setup(){
  pinMode(RED,OUTPUT);
  pinMode(BLUE,OUTPUT);
  pinMode(GREEN,OUTPUT);
  
  Serial.begin(9600);

}
  
void loop(){
  char customKey = customKeypad.getKey();
  
  if (customKey){
    Serial.println(customKey);
    int a =(customKey);
    Serial.println(a);
  }
int a = (customKey);
  //if press the "1" button, led blue for 5s
  if (a==49) {
    digitalWrite(BLUE,HIGH);
    delay(5000);
    digitalWrite(BLUE,LOW);   
  }
  //if press the "2" button, led red for 5s
  if (a==50){
    digitalWrite(RED,HIGH);
    delay(5000);
    digitalWrite(RED,LOW);
  }
  //if press the "3" button, led green for 5s
  if (a==51){
    digitalWrite(GREEN,HIGH);
    delay(5000);
    digitalWrite(GREEN,LOW);
  }
}

I don't know if it's clear but I hope you could help me.

Thank you in advance ! :slight_smile:

I'm new in the arduino's world (and i'm frensh so sorry for my bad English) and I've a programmation problem.

The French forum is pretty active :slight_smile:

Why don’t you put your tests and blink in the main if (customKey){ and blink 3 Times there if your program has nothing else to do

The way you coded it the custom key might be gone the next time you loop()

Alternatively read the topic about millis() pinned at the top of the forum and avoid using delay()

hmm that's true, it's simpler.

I didn't know that there is an French forum but many things are in English so I have to work it :slight_smile:

I finally succeed by adding 3 variables that takes value 1 when the led light light in a color.

But I think that I can simplify my code but I don't know how... :confused:

Here you have my new code :

#include <Keypad.h>
#define RED 12
#define GREEN 11
#define BLUE 10

int led=12;
int b;
int r;
int g;

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[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

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

bool BLUEled = false;

void setup(){
  pinMode(RED,OUTPUT);
  pinMode(BLUE,OUTPUT);
  pinMode(GREEN,OUTPUT);
  
  Serial.begin(9600);

}
  
void loop(){
  char customKey = customKeypad.getKey();
  
  if (customKey){
    Serial.println(customKey);
    int a =(customKey);
    Serial.println(a);
    
      //if press the "1" button, led blue for 5s
       if (a==49) {
      digitalWrite(BLUE,HIGH);
      delay(5000);
      digitalWrite(BLUE,LOW);
      b=1;
      Serial.println(b);
      }
      
      //if press the "2" button, led red for 5s
      if (a==50){
      digitalWrite(RED,HIGH);
      delay(5000);
      digitalWrite(RED,LOW);
      r=1;
     }
      
      //if press the "3" button, led green for 5s
      if (a==51){
      digitalWrite(GREEN,HIGH);
      delay(5000);
      digitalWrite(GREEN,LOW);
      g=1;
     }
      
      if(b==1 && a==53){
      for (int i; i<4; i++){
      digitalWrite(BLUE,HIGH);
      delay(100);
      digitalWrite(BLUE,LOW);
      delay(100);
       }
      b=0;
     }
      
      if(r==1 && a==53){
      for (int i=0;i<4;i++){
      digitalWrite(RED,HIGH);
      delay(100);
      digitalWrite(RED,LOW);
      delay(100);
      }
      r=0;
     }
  
      if(g==1 && a==53){
      for (int i;i<4;i++){
      digitalWrite(GREEN,HIGH);
      delay(100);
      digitalWrite(GREEN,LOW);
      delay(100);
      }
      g=0;
     }
  }
}

I'll read the topic about millis(), that seems to be very usefull !

Thank you very much for your fast answer !

to make it easier to read instead of if (a==49) { you could do if (a=='1') {(same for the others)

if you do want the key pad to keep working, you can't code with the delay(5000) thingy - have a look at this french tuto Programmation Automate fini / Machine à état to get some ideas on how to structure the code