why i have to press several times the button to make the action?

Hi, I’m working on a project with a 4x4 keypad that when you press ‘1’, start a process (in this example I will just put a counter) and when you press ‘2’, stops the process.

The button ‘1’ works just fine… when you press it, immediately starts the counting but in the case of button ‘2’, I have to press multiple time the button to get the action done (stop the counting) it is like I am never in the right time of the loop until I get it by coincidence…

Can someone help me solving this problem?

Here is the example code:

#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
    { '1','2','3','A'},
    { '4','5','6','B'},
    { '7','8','9','C'},
    { '*','0','#','D'}
};
byte rowPins[ROWS] = {0, 1, 2, 3}; 
byte colPins[ROWS] = {4, 5, 6, 7};
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd (0x27, 16, 2);
void screen0() 
{ lcd.clear();
  lcd.setCursor(3, 0);
  lcd.print("Welcome");
  delay(1000);
}
void setup()

{   
    Serial.begin(115200);
    lcd.init();
    lcd.backlight();
    lcd.setCursor(0, 0);
    screen0();
}
byte Count = 0;
void loop()
{ 
  char Key = customKeypad.getKey();
  if (Key =='1'){                       //when I press button 1, starts the counter
        while (Key != '2'){             //If I do not press button 2, counts. (I want it to be a while because this is just a example of a more complicated program that includes a while)
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(Count);
        Count++;
        Key = customKeypad.getKey();
        Serial.println(Key);            //Here I wanted to know why it does not activate they key by printing the key in the Serial monitor while I press it
        delay(100);
      }
  }
  if (Key == '2'){                      //If I press button 2, stops counting
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("STOP COUNTING");
  }
}

Mephirius:
t is like I am never in the right time of the loop until I get it by coincidence...

Exactly true. You have a delay() function inside your while loop and while that is functioning, nothing else is happening so your code is unaware that you are pressing any keys.

Why is it present? Also, you are updating your lcd display each time through your while() loop which will be way faster than a human could ever read/see.

Have a look at several things at the same time
You should also look at the Blink Without Delay example in the IDE for a better way to time things without delay()

Oh, true! when I remove the delay It stops exactly when u press the button... GOOD. Now I have to check if I really needed that delay or no because as I say in the code comments this increasing counter is just to simulate a process of sending messages from another code and I put the delay to simulate a delay that was in the other code so... I am going to make the pertinent changes and I will update you.
Thanks in any case!