Go Down

Topic: Waiting for Key pressed (keypad) (Read 2 times) previous topic - next topic

RaphaelAmin

Hey people,

I'm working on a little project. It's an automatized music keyboard. I am using a 4x3 keypad and a LCD.

The doubt is... How can I make the program to wait if no key is pressed, and if it is it continues with the "if (key != NO_KEY) {...}."

I tried "while (key == NO_KEY);" before the "if" but it brings me to an infinite loop no matter if keys are pressed or not.

And if I leave just the "if" it goes on trough the program, and I dont want it to happen.

Thanks.

Catcher

Did you get an infinite loop because you didn't check the status of the keys inside the loop?
If you did then you should probably post the code.

Pseudocode:
Code: [Select]

boolean NO_KEY = true;

while(NO_KEY)
{
      if(any keys are pressed)
      {
           NO_KEY = false;
      }
}

RaphaelAmin

Its like this:

Code: [Select]

    int key = KeypadX.getKey();
    while(key == NO_KEY) {
      Serial.println("infinite while); // Just to check if it's actually in this infinite loop, and it is.
    }
    if (key != NO_KEY) {  // it works if there is a short function and I dont have to wait for the key
    switch(key) {
      case '1':
      lcd.print(key);
      break;
      }
    }


The NO_KEY is used by the library "keypad.h". I don't know how it exacly works. Actually I have no idea.  :P

Don't know if I made myself clear...

RaphaelAmin

It seems that while/for makes the keypad stop working. It seems to...

Anybody?

WizenedEE

How do you expect the key variable to change while you're looping? Put in the getKey function itself, or call it within the while loop, so that you get the current value.

Code: [Select]

int key = KeypadX.getKey();
while (key == NO_KEY) key = KeypadX.getKey();

Catcher

It seems you did not update the value of the "key" variable inside the while loop. The value is assigned before the loop and will always stay that way until you update it.

Code: [Select]

    int key = KeypadX.getKey();
    while(key == NO_KEY) {
      key = KeypadX.getKey(); //UPDATE VALUE
      Serial.println("Waiting for press"); //This will print as long as no keys are pressed
    }
   
    lcd.print(key);

mstanley

Quote
The NO_KEY is used by the library "keypad.h". I don't know how it exacly works. Actually I have no idea.


When I wrote the examples for using the keypad library I wanted people to know about NO_KEY.  That may have been a mistake.

Anyway, the code
Code: [Select]
if (key != NO_KEY)
{
     // This code in here will only run if you press a key.
}
is telling you that any code you put inside the brackets of this if statement will run only if a valid key has been pressed.

It may seem more logical to you to write it thus:
Code: [Select]

if (key)
{
     // This code in here will only run if you press a key.
}


-Mark Stanley

RaphaelAmin

Thank all of you!

It was so obvious but still for me it was a problem to understand. It's solved! the getkey attribution should be inside the while like you said. :)

So I can keep walking with my little project. I have 1 week to finish it.  :smiley-yell:

Thank you again!

Go Up