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;
}
}