Help with 9 digit keypad code

Im working on a project and I dont now enough about code to do what I want

I just need my program to wait for the user to either press 1 or 2 on the 9 digit keypad. But if they dont press anything i just want it to wait. I have read all around about switch or while loops but cant seem to get anything to work

This is the portion of code im working on

void selection()
{
curs10();
Serial.print("1 For AUTO");
curs00();
Serial.print("2 For MANUAL");

char key = mykeypad.getKey();

//Wait for keypad input
//if user presses 1 then do something
// if user presses 2 do something
// if user hasnt pressed anything just wait until they do.

Maybe try using a while() loop to keep checking whether something has been pressed. Use a boolean variable set to true to keep the while loop loping.

Then within the loop, check which button has been pressed. If its the button 1 or 2, change this boolean variable to false (Which will cause the while loop to stop looping), and store the pressed button in your key for use straight after the while loop.......maybe coding it would be better :stuck_out_tongue:

Try replacing this line:

char key = mykeypad.getKey();

with this:

char key;
boolean keeplooping = true;

while(keeplooping){
key = mykeypad.getKey()
// If key's value is 1 or 2...
if((key == 1)||(key == 2)){
keeplooping = false;
}
}

// Now use the key variable. If the code has gotten this far, the key variable must contain 1 or 2.

Keep it simple

char key; 
while (key != '1' && key != '2')
{
  key = mykeypad.getKey();
}
//continue here after user enters 1 or 2

Note that the while loop will block the code at that point and no other code will run until the while condition is satisfied

This code

char key = mykeypad.getKey();
if (key != '1' || key != '2')
{
  //code here to run if user enter 1 or 2
}
//code here will be run whatever the user enters

would allow the keypress entered by the user to be checked periodically and for other code to be run in the meantime.

if (key != '1' || key != '2')
{
  //code here to run if user enter 1 or 2
}

Say what?

if (key == '1' || key == '2')
{
//code here to run if user enter 1 or 2
}

Bummer !
Copy/Paste/Distraction/Forget to change pasted text strikes again.