nested switch case

Hi! I am new to arduino and currently I'm going to make my first project.
My project involves a status menu in where, whenever the user changes the data through the keypad,
my system should also update and display it in lcd.

I'm going to make a charging station in where a user can select a specific brand of their phone and he/she has the option to choose whether it has security or not in addition if the user chooses the one with security, the user can choose the time to charge his phone.

For this project I'm making two lockers (apple and android)
and I decided to use nested Switch but I'm having trouble at the beginning
sooif anyone willing to give me a small push it'll be much appreciated.

Here's my code

#include <Keypad.h>
#include<LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7,3,POSITIVE); 
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char customkey;
char customkey2=0;
char customkey3 = 0;
char password[5];
String statusString1="okay"; // STATUS FOR DISPLAY
String statusString2="okay"; // STATUS FOR DISPLAY
int x1 = 0; // STORE OF RANDOM PASSWORD
int x2 = 0; // STORE OF RANDOM PASSWORD 2
int i=0; //Character OF password input in keypad
int j=0;
int led = 13; // LED at PIN 13
int data1 = 0; //
int data2 = 0; //
int code = 0;
int code2 = 1;
int checker1 =0;//
int theint = 0;

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 10, 11, 12}; //connect to the column pinouts of the keypad


//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);


//******************************************SETUP******************************
void setup() 
{
 Serial.begin(9600);
 pinMode(led, OUTPUT);
 randomSeed(analogRead(A0));
 lcd.begin(16,2);
 stat();
}

//******************************************LOOP******************************
void loop() 
{
 

  customkey = customKeypad.getKey();

while(customkey==NO_KEY)
{customkey=customKeypad.getKey();}
  

  switch(customkey)
    { case '1' :
        
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("A. With PS");
        lcd.setCursor(0,1);
        lcd.print("B. Without PS");
        if (customkey=='1'){
        switch(customkey){
        case 'A':
           lcd.clear();
           lcd.setCursor(4,0);
           lcd.print("TIME");
           lcd.setCursor(0,1);
           lcd.print("1.30S");
           lcd.setCursor(7,1);
           lcd.print("2.60S");
        break;}}            
      break;}
      
    
}




void stat() {
  lcd.setCursor(0,0);
  lcd.print("1.Brand1");
  lcd.setCursor (9,0);
  lcd.print(statusString1);
  lcd.setCursor(0,1);
  lcd.print("2.Brand2");
  lcd.setCursor (9,1);
  lcd.print(statusString2);}

Stripped version with comments

 switch (customkey)
  {
    case '1' :
      ...
      ...
      // do you think that customkey has changed ??
      // this is currently redundant
      if (customkey == '1')
      {
        // because customkey did not change
        // case 'A' will never happen
        // you will have to read the keypad again here till you have a keypress
        switch (customkey)
        {
          case 'A':
            ...
            ...
            break;
        }
      }
      break;
  }

And please get rid of String (with capital S) while you're not too deep into it; forget that it exists and use nul terminated character arrays.

Looks like you should be using a "State Machine" (or "Finite State Machine") design for this sketch.

There is a library to help: Arduino Playground - FiniteStateMachine Library

Hi everyone thank you for replying I tried something like this

#include <Keypad.h>
#include<LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7,3,POSITIVE); 
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char customkey;
char customkey2=0;
char customkey3 = 0;
char password[5];
int statuscheck=0;
String statusString1="okay"; // STATUS FOR DISPLAY
String statusString2="okay"; // STATUS FOR DISPLAY
int x1 = 0; // STORE OF RANDOM PASSWORD
int x2 = 0; // STORE OF RANDOM PASSWORD 2
int i=0; //Character OF password input in keypad
int j=0;
int led = 13; // LED at PIN 13
int data1 = 0; //
int data2 = 0; //
int code = 0;
int code2 = 1;
int checker1 =0;//
int theint = 0;

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 10, 11, 12}; //connect to the column pinouts of the keypad


//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);


//******************************************SETUP******************************
void setup() 
{
 Serial.begin(9600);
 pinMode(led, OUTPUT);
 randomSeed(analogRead(A0));
 lcd.begin(16,2);
 stat();
}

//******************************************LOOP******************************
void loop() 
{
 

  customkey = customKeypad.getKey();

while(customkey==NO_KEY)
{customkey=customKeypad.getKey();}



  switch(customkey)
    { case '1' :       
      statuscheck=2;
      customkey=customkey2;
        if (customkey2==1){
        switch(customkey2){
          case'A':
           statuscheck=3;
           break;}}

      
      break;}


if(statuscheck==1)
{stat();}

else if(statuscheck==2){
  password_Update();   
}
else if(statuscheck==3){
  time_Update();
}
}


void stat() {
  lcd.setCursor(0,0);
  lcd.print("1.Brand1");
  lcd.setCursor (9,0);
  lcd.print(statusString1);
  lcd.setCursor(0,1);
  lcd.print("2.Brand2");
  lcd.setCursor (9,1);
  lcd.print(statusString2);}

void password_Update(){
   lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("A. With PS");
        lcd.setCursor(0,1);
        lcd.print("B. Without PS");
}

void time_Update(){
  lcd.clear();
           lcd.setCursor(4,0);
           lcd.print("TIME");
           lcd.setCursor(0,1);
           lcd.print("1.30S");
           lcd.setCursor(7,1);
           lcd.print("2.60S");
}

But still I'm stuck on how to update the value of customkey when entering a new switch statement I'm a bit confused on how to do it, do I need to decalre "customkey2=customKeypad.getKey();" again inside the case 1 of the first switch?

anybody?

Reading the key code if it’s available at the top of loop is ok, but if you read it again, you’ll lose the key that you captured...

first, lose

while(customkey==NO_KEY)
{customkey=customKeypad.getKey();}

then put NO_KEY as a case insidevthe switch() block.

That should get you heading in the right direction.

lastchancename:
first, lose

while(customkey==NO_KEY)

{customkey=customKeypad.getKey();}



then put NO_KEY as a case insidevthe switch() block.

Mmm like this?

#include <Keypad.h>
#include<LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7,3,POSITIVE); 
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char customkey;
char customkey2=0;
char customkey3 = 0;
char password[5];
int statuscheck=0;
String statusString1="okay"; // STATUS FOR DISPLAY
String statusString2="okay"; // STATUS FOR DISPLAY
int x1 = 0; // STORE OF RANDOM PASSWORD
int x2 = 0; // STORE OF RANDOM PASSWORD 2
int i=0; //Character OF password input in keypad
int j=0;
int led = 13; // LED at PIN 13
int data1 = 0; //
int data2 = 0; //
int code = 0;
int code2 = 1;
int checker1 =0;//
int theint = 0;

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 10, 11, 12}; //connect to the column pinouts of the keypad


//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);


//******************************************SETUP******************************
void setup() 
{
 Serial.begin(9600);
 pinMode(led, OUTPUT);
 randomSeed(analogRead(A0));
 lcd.begin(16,2);
 stat();
}

//******************************************LOOP******************************
void loop() 
{
 

  customkey = customKeypad.getKey();
  
  switch(customkey)
    { case '1' :       //FIRST SWITCH CASE 
      statuscheck=2;
      customkey2=customkey;
        if(customkey2=='1'){
          switch(customkey2) //SECOND SWITCH TO DISPLAY PASSWORD MENU
          {
          case'A':
           statuscheck=3;
           break;}}
      break;
      case NO_KEY:
      customkey=customKeypad.getKey();
      break;}


if(statuscheck==1)
{stat();}

else if(statuscheck==2){
  password_Update();   
}
else if(statuscheck==3){
  time_Update();
}
}


void stat() {
  lcd.setCursor(0,0);
  lcd.print("1.Brand1");
  lcd.setCursor (9,0);
  lcd.print(statusString1);
  lcd.setCursor(0,1);
  lcd.print("2.Brand2");
  lcd.setCursor (9,1);
  lcd.print(statusString2);}

void password_Update(){
   lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("A. With PS");
        lcd.setCursor(0,1);
        lcd.print("B. Without PS");
}

void time_Update(){
  lcd.clear();
           lcd.setCursor(4,0);
           lcd.print("TIME");
           lcd.setCursor(0,1);
           lcd.print("1.30S");
           lcd.setCursor(7,1);
           lcd.print("2.60S");
}

when I did that the (0,1) of lcd starts to flicker..
I can't progress with my project man, I can't seem to grasp the idea of nested switch
In my head, each time I press 1 from my keypad another switch statement should appear and
if I press 'A' the display should get updated. the thing is whenever I enter '1' it stop to
with password/ without password menu..

Thank you for replying

ALMOST...
This won't run, but there are comment to help you look in the right places.
(EDIT: DeltaG beat me to it - referring to state machines!)
Sorry the indent tabs blew up when I pasted from Notepad++ into the web page

#include <Keypad.h>
#include<LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <Wire.h>

//****************************************DECLARATIONS*************************
LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7,3,POSITIVE); 
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char customkey;
char customkey2=0;
char customkey3 = 0;
char password[5];
int statuscheck=0;

// try to avoid Strings - use char[] arrays
String statusString1="okay"; // seems kinda redundant`
String statusString2="okay"; // seems kinda redundant

//****************************************INITIALISATION***********************
int x1 = 0; // STORE OF RANDOM PASSWORD
int x2 = 0; // STORE OF RANDOM PASSWORD 2
int i=0; //Character OF password input in keypad
int j=0;
int led = 13; // LED at PIN 13
int data1 = 0; //
int data2 = 0; //
int code = 0;
int code2 = 1;
int checker1 =0;//
int theint = 0;

// --- keypad button map ---
byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 10, 11, 12}; //connect to the column pinouts of the keypad

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

//initialize an instance of keypad class
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

//******************************************SETUP******************************
void setup() {
 Serial.begin(9600);
 pinMode(led, OUTPUT);
 randomSeed(analogRead(A0));
 lcd.begin(16,2);
 stat();
}

//******************************************LOOP******************************
// EXAMPLE ONLY - WILL NOT WORK LIKE THIS
//******************************************LOOP******************************
void loop() {
 
  customkey = customKeypad.getKey();
  
 // I personally use trailing open braces { as they are easier to see
 // especially in (nested) switch() blocks
 
  switch(customkey) {
 case '1' :       //FIRST SWITCH CASE 
      statuscheck=2;

 // this is 'sort of' what you intended (I think) 
 // but you can't do it *inline* like this 
 // - as the 'duino and keypad code are way faster 
 // than you are at pressing keys!!
 
      if(customkey=='1')  { // choose an action on the second key pressed

 //---> customkey2 = customKeypad.getKey();  // not here
 
 // you could caapture the keys into an array and act on their position/value,
 // or you coud use something like a simple state-machine
 // to determine if its the first key or second/subsequent press.
 // you may also want to add a timeout if a key isn't pressed 
 // or your code will just hang until another key is pushed
 
 switch(customkey2) {  // choose an action on the second key pressed
          case'A':
 statuscheck=3;
 break;
 default:
 // no customkey2 or any other fall-through
 }
 }
      break;
 default:
 // no key or any other fall-through 
 }
 
 // use a switch here - more readable
 switch(statuscheck {
 case 1:
 stat();
 break;
 case 2:
 password_Update();
 break;
 case 3:
 time_Update();
 break;
 }
}


//****************************************************************************
void stat() {
  lcd.setCursor(0,0);
  lcd.print("1.Brand1");
  lcd.setCursor (9,0);
  lcd.print(statusString1);
  lcd.setCursor(0,1);
  lcd.print("2.Brand2");
  lcd.setCursor (9,1);
  lcd.print(statusString2);}

//****************************************************************************
void password_Update(){
  lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("A. With PS");
 lcd.setCursor(0,1);
 lcd.print("B. Without PS");
}

//****************************************************************************
void time_Update(){
  lcd.clear();
 lcd.setCursor(4,0);
 lcd.print("TIME");
 lcd.setCursor(0,1);
 lcd.print("1.30S");
 lcd.setCursor(7,1);
 lcd.print("2.60S");
}

lastchancename:
ALMOST...
This won't run, but there are comment to help you look in the right places.
(EDIT: DeltaG beat me to it - referring to state machines!)
Sorry the indent tabs blew up when I pasted from Notepad++ into the web page

#include <Keypad.h>

#include<LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <Wire.h>

//***************DECLARATIONS
LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7,3,POSITIVE);
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char customkey;
char customkey2=0;
char customkey3 = 0;
char password[5];
int statuscheck=0;

// try to avoid Strings - use char[] arrays
String statusString1="okay"; // seems kinda redundant`
String statusString2="okay"; // seems kinda redundant

//*****************INITIALISATION
int x1 = 0; // STORE OF RANDOM PASSWORD
int x2 = 0; // STORE OF RANDOM PASSWORD 2
int i=0; //Character OF password input in keypad
int j=0;
int led = 13; // LED at PIN 13
int data1 = 0; //
int data2 = 0; //
int code = 0;
int code2 = 1;
int checker1 =0;//
int theint = 0;

// --- keypad button map ---
byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 10, 11, 12}; //connect to the column pinouts of the keypad

char hexaKeys[ROWS][COLS] = {
 {'1', '2', '3', 'A'},
 {'4', '5', '6', 'B'},
 {'7', '8', '9', 'C'},
 {'*', '0', '#', 'D'}
};

//initialize an instance of keypad class
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

//************SETUP
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
randomSeed(analogRead(A0));
lcd.begin(16,2);
stat();
}

//************LOOP
// EXAMPLE ONLY - WILL NOT WORK LIKE THIS
//************LOOP
void loop() {

customkey = customKeypad.getKey();
 
// I personally use trailing open braces { as they are easier to see
// especially in (nested) switch() blocks

switch(customkey) {
case '1' :       //FIRST SWITCH CASE
     statuscheck=2;

// this is 'sort of' what you intended (I think)
// but you can't do it inline like this
// - as the 'duino and keypad code are way faster
// than you are at pressing keys!!

if(customkey=='1')  { // choose an action on the second key pressed

//---> customkey2 = customKeypad.getKey();  // not here

// you could caapture the keys into an array and act on their position/value,
// or you coud use something like a simple state-machine
// to determine if its the first key or second/subsequent press.
// you may also want to add a timeout if a key isn't pressed
// or your code will just hang until another key is pushed

switch(customkey2) {  // choose an action on the second key pressed
         case'A':
statuscheck=3;
break;
default:
// no customkey2 or any other fall-through
 break;}
}
     break;
default:
// no key or any other fall-through
break;
}

// use a switch here - more readable
switch(statuscheck) {
case 1: stat();
break;
case 2: password_Update();
break;
case 3: time_Update();
break;
}
}

//****************************************************************************
void stat() {
 lcd.setCursor(0,0);
 lcd.print("1.Brand1");
 lcd.setCursor (9,0);
 lcd.print(statusString1);
 lcd.setCursor(0,1);
 lcd.print("2.Brand2");
 lcd.setCursor (9,1);
 lcd.print(statusString2);}

//****************************************************************************
void password_Update(){
 lcd.clear();
lcd.setCursor(0,0);
lcd.print("A. With PS");
lcd.setCursor(0,1);
lcd.print("B. Without PS");
}

//****************************************************************************
void time_Update(){
 lcd.clear();
lcd.setCursor(4,0);
lcd.print("TIME");
lcd.setCursor(0,1);
lcd.print("1.30S");
lcd.setCursor(7,1);
lcd.print("2.60S");
}

Okay this is really helpful might take sometime to study and get additional way to approach my problem
THANK YOU SOO MUCH
tho if I run this code it did change to the switch case(statuscheck) but my
(0,1) part of lcd flickers
this part

lcd.print("B. Without PS");

AGAIN THANK YOUU SO MUCH might trouble you again if I have further questions after I study finite state machines

Delta_G:
Go study something called a finite state machine. Therein lies your answer. Each case should set something to tell loop what code to run next time.

I found some examples like this one I will make sure to study and try my best to understand it
http://digitaldiy.io/articles/mcu-programming/general-programming/500-finite-state-machines#.WdEEFWiCzIW

Thank you!!