Case switch menu system question

Hi

I have a mega2560 with a 4x4 keypad and 20,4 lcd

I'm trying to implement a menu system as described below, but I'm encountering some problems....

the code is hellishly long so I'll type out an overview for readabilit'ys sake :

Loop : 

int mode = A;
if(keypad  !=  NO_KEY) {
mode = keypad;
}
switch case : 
case B
    do function blahblah
    break
Case C
    do function yapyap
    break
case D
    Do Menu-function1
    break
Case A
   Do the "normal stuff"
  
end loop


void menufunction
Switch case, case = char 

case A
    do even another function 
    break
Case B
    do yet another function 
    break
case C
    Do Menu-function2 (more of the same, it goes 5 or so levels deep)
    break
Case D
   End this function, retun to main loop
  
end menufunction1

NOW : when you enter B in main loop, that works perfectly, when you enter D, you get the submenu on screen... But how do I get it to accept a new input of A B C D without the MAIN LOOP switch case changing ? When i press B in teh submenu, it loads the main switch B function and forgets about the earlyer D case....

Now obviously this isn't the best way to do this, but I can't think of a better or cleaner way... I've been staring too long at it to get fresh ideas, I think...

Oh and I know of the scrolling menu library out there, but that uses "arrow" keys instead of dedicated buttons... could that be adapted into accepting button input instead of the scrolling

I’d put a fresh input and switch/case in each sub-menu function, but without seeing your actual code, it is difficult to say.

That's what I'm doing now.

But the original case runs in a WHILE loop that scans for a keypress... so when a button is presssed while in submenue, it also registers for original switch....

maybe there's another way to scan for a keypress while the program is running ?

(updating main code example to represent this.. sorry I forgot, don't have my code with me to upload it)

But the original case runs in a WHILE loop that scans for a keypress... so when a button is presssed while in submenue, it also registers for original switch....

I don't understand this - are you saying there's some sort of callback from the keyboard input?

Have a variable that keeps track of the menu level your are in, and use that to determine what to do.

I don't understand. I guess I'll leave it for a while untill my brain resets and then try the variable option mentioned... or rewrite it entireley

all i know is, after testing it, when in mode D, buttons pressed result in case switches in the original, NOT in the new sub-switch...

if perchance anyone knows of a menu-sketch out there that I can look at please do post a link...

thanks

AWOL: I don't understand this - are you saying there's some sort of callback from the keyboard input?

Loop : 

int mode = A;
if(keypad  !=  NO_KEY) {
mode = keypad;
}
switch case : 
case B
    do function blahblah
    break
Case C
    do function yapyap
    break
case D
    Do Menu-function1
    break
Case A
   Do the "normal stuff"
  
end loop


void menufunction

while keypad == NO_KEY {wait a minute for the input and if none entered break back to the loop;}

if(keypad  !=  NO_KEY) {
mode2 = keypad;
}

mode2 switch : 
case A
    do even another function 
    break
Case B
    do yet another function 
    break
case C
    Do Menu-function2 (more of the same, it goes 5 or so levels deep)
    break
Case D
   End this function, retun to main loop
  
end menufunction1




NOW : when you enter B in main loop, that works perfectly,  when you enter D, you get the submenu on screen... But how do I get it to accept a new input

No, until you post your actual code, we're just wasting time.

Not really… talking about it made me realise there’s a much easier way of doing it based on variables as mentioned, which should be easier to read (and adapt) then nesting switches

I should stop watching House episodes… Thanks anyway guys, but sorry for wasting your time :wink: !