need help with a switch in switch statement reading a keypad

Greetings all.

i hav a problim with reading a keypad in a switch / case statement ( the first is working but when i want to use a second one it won't work )
It is a simple YES NO question where an LCD shows the Question and you press de * for Yes or the # for NO

how do i get to the second switch case statement ( it is not working )
And if it is corrected and working, how do i get back to the main loop and go on the run the code further on at the end of the switch case statements

basic senario.

  1. question to be answerd with YES or NO
    2a. When answer is YES execute code after Case # ( from first switch case statement)
    2b. When answer is NO execute code after Case* ( from first switch case statement and enter next switch case statement )
    3a. When answer to second question is YES execute code after Case # ( from second switch case statement and return to main loop and run code further on beyound switch statements)
    3b. When answer to second question is NO execute code after Case * ( from second switch case statement and return to main loop and run code further on beyond switch statements)

i hope someone can help me with this.
i am working on it for quite some time now and don't seem to get it clear.

Void loop();
{
  BLA BLA BLA
    lcd.print("# = YES / * = NO");
  char keyone = customKeypad.getKey();
  switch (keyone)
  {    
  case '*':
    {
      BLA BLA
        lcd.print("Next questiont");
      lcd.print("# = YES / * = NO");
      char subkey = customKeypad.getKey();
      switch  (subkey)
      {
      case '*':
        {
          lcd.print("Next question");
          should i use break or return here
        }
      case '#':
        {
          lcd.print("Some text for #");
          should i use break or return here what does a return do her and what does a break do here
          what sould i use to break out of the second switch loop to go back to the main loop ( so out of the first swicht loop too)
        }
      default:
        {
          return;
        }           
      }
    }	
  case '#':
    {
      lcd.print("some text2 ");
      break;  
    }
  default:
    return;
  }

  some default code to run further on	 

}

Greetings Adriaan

Void loop();
{
  BLA BLA BLA

Post your code, then maybe you can get help.

      char subkey = customKeypad.getKey();
      switch  (subkey)
      {
      case '*':

You are aware, I presume, that getKey() is NOT a blocking function. That is, it doesn't wait for you to press a key.

Just added keypad to my free open source cpp Arduino simulator.
https://github.com/Pualware/ArduinoSimulator

AWOL:

Void loop();

{
  BLA BLA BLA



Post your code, then maybe you can get help.

Hi,

It is not important to display the used code that is replaced by the BLA BLA line ( its only lcd.print stuff)
it is not important for the question.

@ PaulS
How do i get it to work in a blocking way ?

Greetings Adriaan

Void loop();

It is important that "void" is not capitalised, and it is important there is no semicolon after "loop()".

How do i get it to work in a blocking way ?

char subkey = customKeypad.getKey();
// Wait until a key IS pressed
while(subkey == NO_KEY)
{
subkey = customKeypad.getKey();
}

switch (subkey)
{
case '*':

PaulS:

How do i get it to work in a blocking way ?

char subkey = customKeypad.getKey();
// Wait until a key IS pressed
while(subkey == NO_KEY)
{
subkey = customKeypad.getKey();
}

switch (subkey)
{
case '*':

Hi PaulS,

This did the trick.
It is working now,

One question, how do i break from the second switch case statement directly to the main loop.
when i now break from the second switch case statement i still have to push the # key to end the first switch case statement.

Geetings Adriaan

One question, how do i break from the second switch case statement directly to the main loop.

Don't break. Return, instead.

If you have many menus that you would like to traverse I suggest you look into the PSTR macro. It stores the string data in program memory rather than RAM which is limited.

Here is a video I made of my simulator with keypad and 20x4 lcd display

I'm also available on fiverr to help:

Here is the example sketch code.
#include <Keypad.h>
#include <LiquidCrystal.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup()
{
bool d10;
Serial.begin (115200);
lcd.begin(20, 4); // Specify how many columns and rows in the LCD unit
lcd.clear();
lcd.print("LCD Ready ");
Serial.println ("Project: KeypadDisplay");
Serial.println ("Press '*' or '#' to see a message");
}

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

switch (key)
{
case '*':
lcd.clear();
lcd.print ("A star was pressed");
break;

case '#':
lcd.clear();
lcd.print ("A pound was pressed");
break;

default:
if (key)
lcd.print (key);
break;
}
}