switch case always gives the default case output

Hi,
I am using a 4x3 keypad and trying to create password program. Here I am using a switch case structure that asks to press * for getting the menu. After entering the menu, it is required to choose option from the list using keypad. But instead of choosing option it always prints "invalid key" which is default option. Can you please help me to properly write the code? I have attached the code and the supporting files for further reference to the code.
Thanking You

[//*Key pad interfacing using 4x3 keypad*/
#include"myKey.h"
#include"Functions.h"

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
Serial.begin(9600);
Serial.println("Welcome...");
Serial.println("Press * to enter menu");
}

void loop() {
  // put your main code here, to run repeatedly:
char key=kpd.getKey();
if(key!=NO_KEY)
{
  switch(key)
  {
    case '*': 
              {
              showNextMenu();
              chooseOption();
              }
              break;
    default:
            Serial.println("Invalid Key"); 
            exit;
  }
}
}
void showNextMenu(void)
{
  Serial.println("1:open box");
  Serial.println("2:change passwd");
  Serial.println("Any:exit menu");
/*
  char key=kpd.getKey();
  if(key!=NO_KEY)
  { 
   delay(2000);
   switch(key)
   {
    case '1': 
              openBox();
              break;
    case '2': 
              changePwd(); 
              break;
    default : 
              exit;
             
   }
  }*/
}
void chooseOption(void)
{
  Serial.println("Enter choice..");
  delay(1000);
  char key=kpd.getKey();
  if(key!=NO_KEY)
  { 
   delay(100);
   switch(key)
   {
    case '1': 
              openBox();
              break;
    case '2': 
              changePwd(); 
              break;
    default : 
             Serial.println(" see you next time");;
             
   }
  }
}







]

pwd.h (64 Bytes)

myKey.h (770 Bytes)

Functions.h (239 Bytes)

Functions.cpp (1.36 KB)

Arduino_Keypad_with_LCD.ino (1.35 KB)

Have you thought of trying to print the value of "key"?
It may provide a hint as to where you are going wrong.

sumanta_777:
Hi,
I am using a 4x3 keypad...

Keypad with 7 wires? Keypad setup is in myKey.h. Are you using the pins as stated in that file?
I find this tutorial good.

exit;

That is about as useful as

47;

Calling a function REQUIRES parentheses.

  Serial.println("Enter choice..");
  delay(1000);
  char key=kpd.getKey();
  if(key!=NO_KEY)

The getKey() function does NOT wait for a key to be pressed. The delay() is useless.

There IS a method in the Keypad library that DOES wait.

hi,
I have get rid of this problem by using waitForKey() function. Thanks for the help.