Keypad Delay

Hi all,

I am new to Arduino, currently trying to input keypad number into the serial monitor. However, there has been suspected delay whenever a keypad number is physically pressed.

Could anyone kindly please look at these codes and determine if there has been a delay somewhere?

Much much appreciated!

*/
Keypad Printing
*/

{
char keypressed = kpd.getKey();
if (keypressed != NO_KEY)
{
Serial.println(keypressed);
}

// for PC input at Serial Monitor
if (Serial.available())
{
DoOneTest();
delay(500);

}
lcd.display();
delay(500);

// Button Input
digitalWrite(3, HIGH);
int buttonValue = digitalRead(buttonPin);

if (buttonValue == HIGH) {
lcd.clear();
DoOneTest();
delay(500);

}
digitalWrite(3, LOW);
delay(500);

// Print a message to the LCD.
for (int j = 0; j <= 1; j++)
{
lcd.setCursor(0,j);
lcd.print(“Q”);
lcd.setCursor(1,j);
lcd.print(j);

delay(500);
}
}

there has been suspected delay whenever a keypad number is physically pressed.

Could that be due to the delay()s in the program ?

Part of your code has been turned into italics in your post because you did not use code tags.

Please read this before posting a programming question and follow the advice given

I have tried removing the delay()s. However, it has continuously print something else that it was designed to print initially.

//If key is pressed, this key is stored in 'keypressed' variable
//If key is not equal to 'NO_KEY', then this key is printed out
void loop()
{

     char keypressed = kpd.getKey();
     if (keypressed != NO_KEY)
     { 
          Serial.println(keypressed);
     }
  
 // for PC input at Serial Monitor 
  if (Serial.available())
  {
    DoOneTest();
    delay(500);
    
  }
  lcd.display();
  delay(500);

// Button Input
digitalWrite(3, HIGH); 
  int buttonValue = digitalRead(buttonPin);
  
  if (buttonValue == HIGH) {
    lcd.clear();
    DoOneTest();
    delay(500);
    
  } 
  digitalWrite(3, LOW); 
  delay(500);

// Print a message to the LCD.
for (int j = 0; j <= 1; j++)
{
  lcd.setCursor(0,j);
  lcd.print("Q");
  lcd.setCursor(1,j);
  lcd.print(j);

  delay(500);
}
}

What do you see on the Serial monitor if you reduce the loop() function to just

void loop()
{
  char keypressed = kpd.getKey();
  if (keypressed != NO_KEY)
  {
    Serial.println(keypressed);
  }
}

What is the purpose of the keypad in the program because you do not use the value entered ?

The purpose is to use a number to activate a "high" response from the Arduino port. However, i am still at it's infant stage whereby I am still trying to print the number onto the Serial Monitor without delays or lag.

Try the very much reduce loop() function from my post

After reducing the void loop() to the above mentioned, i am able to press the button and display it on the serial monitor with ease (without delays).

minwei192: After reducing the void loop() to the above mentioned, i am able to press the button and display it on the serial monitor with ease (without delays).

Good. That is what I expected. Because of the delay()s in your you will need to hold down the keys for some time before the loop() function ends and goes back to read the keypad again.

However, after reducing the delays, the serial monitor printing the "test" results.

How many times do you want to print the results and how many times ?

I would like to print the results whenever my switch is pressed. Meanwhile, I would like to print my keypad number whenever it is pressed too.

I would like to print the results whenever my switch is pressed.

Do you want to print the results while the switch is pressed, which could print the results many times, or do you want to print them when it becomes pressed, ie once per press ?

Multiple times. On that end, it is working fine. However, the keypad is not responding well. It has a delayed response in printing onto the serial monitor.

The keypad response is slow because of the delay()s. To speed it up you need to remove them. Removing them speeds up the Serial printing too much.

So. What to do ?

The answer is to use millis() for timing instead of delay(). The delay() function stops everything else happening in the program, but by using millis() for timing we can let loop() run freely and read the keypad frequently.

How is this magic achieved, I hear you ask. Well, if you save the value of millis() in a variable when you print the result and check each time through loop() whether the current time minus the start time is equal to or greater than the required printing period you can carry on round loop() as fast as you like until the period ends. At that point you print the result again, save the time and carry on.

Look at Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE for examples.

Hi, thank you for your reply.

I have solved the issue by eliminating some of the delays at the end portion.

Thanks for your help thou!