waitForKey() function

This program compiles, loads, and prints the loop count on the LCD display without waiting
for a key to be pressed.
I inserted 'char waitForKey()' function in the Hello World keypad program and it works as expected.

// This reads a character from the keypad and prints it on the I2C display
//
// Start by including the I2C Display
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Initialize the I2C LCD
LiquidCrystal_I2C lcd(0x20,16,2); // set the LCD address to 0x20 for a 16 chars and 2 line display
// Then we initialize the keypad
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {9,8,7,6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {12,11,10}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int loopcount = 0 ; //counts times thru loop

void setup()
{
lcd.init(); // initialize the lcd
Serial.begin(9600); // and the serial port
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.clear();
lcd.print("Was'sup, Dude!!??");
delay(1000);
}

void loop(){
char waitForKey();
char key = keypad.getKey();
delay(1000);
Serial.println(key);
delay(1000);
lcd.clear();
lcd.print(key); //Print key on LCD display
// delay(1000);
lcd.clear();
lcd.print(loopcount);
loopcount = loopcount + 1;
// delay(1000);
}

// End of program

Was there a question?
Why is there a function prototype for "waitForKey" inside "loop"?

Thanks for the reply.
The question is "Am I using waitForKey() correctly?" Where should it be if not inside the loop()? I moved it to the bottom of the Setup() section but it didn't change anything. The program went buzzing around the loop and printing the loop count as it went.

I am new to Arduino and programming in C/C++. How should the "wait ForKey()" function be used? Does it return any parameters??

How should the "wait ForKey()" function be used?

char keyPressed = waitForKey(); // Wherever you think you need to wait for a key

Does it return any parameters??

Yes.

Personally, I think you are barking up the wrong tree here. You should be performing some action when a key is entered, rather than doing nothing until a key is entered.

Thanks for your response, PaulS.

I'm working on a number guessing game so waiting for a keystroke is a primary function of the process and pretty important to the sucess of the game. What else would you suggest I do???? I plan to display feedback to the player but, without a keystroke (guess), what feedback could I give????

What parameter does the 'waitForKey() return??? The key that was pressed????

What parameter does the 'waitForKey() return??? The key that was pressed????

How the heck should we know? It's a function you are planning to develop. You tell us what you intend to return.

What else would you suggest I do???? I plan to display feedback to the player but, without a keystroke (guess), what feedback could I give????

You operate a PC, right? What "feedback" does it give if you don't press a key?

The question is "Am I using waitForKey() correctly?"

In the code you posted originally?
No.
See reply #1, second question.

Hi if we want to listen till a key is pressed and read that pressed value, we dont wanna use the getKey() anymore. Instead use like this

char kpress= keypad.waitForKey();

keypad here refer to the keypad object name you have given earlier.
This usage will wait for keypad press event and read the value to kpress char variable.

Bineesh