Problem with using matrix keypad

Hello friends,

Currently I am working with a project where I am using arduino Mega2560, LCD and 4X3 Matrix keypad. What I am trying to do is I want to control 20 relays connected to Mega2560 using keypad and display its respective number number on LCD. Pressing '*' should allow me to select from one of the first 10 set of relays and pressing '#' should allow me to select relays from remaining 10 sets of relays.

My problem is whenever I press '*' it jumps to the called function but does not read any input from keypad in that function. Rather the program rolls back to reading just the keys in main loop. here is the code I am using

#include <Keypad.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd (30,31,32,33,34,35)

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] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

int pin = 9;

for (int k = 0; k < 21; k++)
{
pinMode(pin,OUTPUT);
pin++;
}
void setup()
{
lcd.begin(16,2);
lcd.clear();
lcd.print("Welcome");
delay(1000);

}

void loop()
{
char key = keypad.getKey();

if (key != NO_KEY)
{
lcd.clear();
lcd.print(key);

switch(key)
{
case'*' :
star();
break;

case'#' :
hash();
break;
}
}
}

void star()
{
lcd.clear();
lcd.print("Select Lights");
key = keypad.getKey();

if (key != NO_KEY)
{
lcd.clear();
lcd.print(key); //from here the program rolls back to loop
switch(key)
{
case '1' :
//turn ON or OFF device 1
break;
}

}

It would read key presses in the star() function IF there were any keys being pressed at the time the function was called.

You seem to expect star() to wait for a key to be pressed. There is a different method in the Keypad class for that.

It is not very likely you manage to push a number within the few microseconds it takes for your star function to complete.
You could use while to actively wait for a number, or rethink your input code.
Have a look at this. Arduino Tutorials – Chapter 42 – Numeric Keypads | tronixstuff.com

You seem to expect star() to wait for a key to be pressed. There is a different method in the Keypad class for that.
[/quote]

Can please guide me with it please paulS

Count:
You seem to expect star() to wait for a key to be pressed. There is a different method in the Keypad class for that.

Can please guide me with it please paulS

Did you look at the methods in the Keypad class? Doesn't the "waitFor" in a method name suggest anything to you?

PaulS:
Did you look at the methods in the Keypad class? Doesn't the "waitFor" in a method name suggest anything to you?

Yeah I read about waitforkey() but I am unable to understand where and how to use it in my code can you guide me with that

I am unable to understand where and how to use it in my code

The where is easy. Use it anywhere where you want to wait for a key to be pressed.

The how is easy, too. Use it just like getKey().

PaulS:
The where is easy. Use it anywhere where you want to wait for a key to be pressed.

The how is easy, too. Use it just like getKey().

#include <Keypad.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd (30,31,32,33,34,35)

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] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

int pin = 9;

for (int k = 0; k < 21; k++)
{
pinMode(pin,OUTPUT);
pin++;
}
void setup()
{
lcd.begin(16,2);
lcd.clear();
lcd.print("Welcome");
delay(1000);

}

void loop()
{
char key = keypad.getKey();

if (key != NO_KEY)
{
lcd.clear();
lcd.print(key);

switch(key)
{
case'*' :
star();
break;

case'#' :
hash();
break;
}
}
}

void star()
{
lcd.clear();
lcd.print("Select Lights");
char key = keypad.waitForKey();
key = keypad.getKey();
if (key)
{
lcd.clear();
lcd.print(key); //from here the program rolls back to loop
switch(key)
{
case '1' :
//turn ON or OFF device 1
break;
}

}

Is that right, but now what all I am getting is just "select lights" on the lcd and any key presses gurther are not responded.

Did I used it in a right place and manner...?

Thanks PaulS it solved my problem , I just needed to remove the if statement. But some how the code stick further

After:

char key = keypad.waitForKey();
key = keypad.getKey();
lcd.print(key)

problem is the lcd now shows some weird charachter. Please hwlp me to sort this out

Gabriel_swe:
Have a look at this. Arduino Tutorials – Chapter 42 – Numeric Keypads | tronixstuff.com

Thanks for the reply gabriel, this link uses if(key!=NO_KEY), which I tried but didn't work for me. Can you suggest some alternative

Will using waitForKey() will not allow any lcd updates because lcd shows some weird charachter

char  key = keypad.waitForKey();
key = keypad.getKey();

Wait until you get a key press, and store that value in key.

Then, immediately overwrite the value in key with whatever key is currently being pressed, including no key being pressed. Why on earth would you do THAT?

Will using waitForKey() will not allow any lcd updates because lcd shows some weird charachter

Well, gee, I wonder why?