Pages: [1]   Go Down
Author Topic: Waiting for Key pressed (keypad)  (Read 1951 times)
0 Members and 1 Guest are viewing this topic.
Brazil
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 167
Safety first.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
boolean NO_KEY = true;

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

Brazil
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Its like this:

Code:
    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.  smiley-razz

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

Brazil
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Anybody?
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
int key = KeypadX.getKey();
while (key == NO_KEY) key = KeypadX.getKey();
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 167
Safety first.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
    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);
Logged

Phillipsburg, NJ
Offline Offline
Full Member
***
Karma: 6
Posts: 184
Author: Matrix Keypad Library
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
if (key)
{
     // This code in here will only run if you press a key.
}

-Mark Stanley
Logged

Brazil
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley

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

Thank you again!
Logged

Pages: [1]   Go Up
Jump to: