LCD Menu system and button detection using switch case 101 request


I recently wrote some code using Phi Prompt library to run a PID controller. However, I was having problems as the bulk of my code for the calculations was bogged down within a while loop and was taking excessively long.

I would like to go back to scratch and write a menu system using switch case without using too many loops for button detection etc...

I have looked through the forums and there are a lot of posts solving particular problems.

Does anyone know of a decent 101 style tutorial on use of switch case for making multilevel menu systems to work with an LCD and buttons?

Does anyone know of a decent 101 style tutorial on use of switch case for making multilevel menu systems to work with an LCD and buttons?

What makes the switch statement so attractive? The switch statement selects one block of code to execute, based on some constant (at the time of selection). How would that constant be defined in such a way that it is relevant for every level of the mysterious menu that you have/need?

To be perfectly honest, I am not sure what options I have available for implementing a menu system.

Basically I want to able to control 2 devices with the same board; Sous Vide and Vacuum Chamber Sealer like this:

Sous Vide Menu Vacuum Chamber Sealer Menu | |

| | | | | | Run Setpoint Tuning Menu Run Set Vacuum Set Mode


| | | Set Kp Set Ki Set Kd

However when I select 'Run' in Sous Vide I want it to run 'on top' and as quickly as possible. At the moment the system I use buries it and a while loop and it takes 4 secs to run each reading, calculation % LCD update

There should be a way to get which button was pressed without using the while statement.

Are you using one of the phi panels?

Also put the code you want to run a LOT in the main loop and put other code in a different function that returns a lot to the main loop.

That’s over simplified.

No, I am using the phi shield 2 pinout as I couldn’t actually get a phi shield over here. The while loop is to keep the code running but allow a button press to escape if I needed to get back. Code is attached: (5.99 KB)

I think this question has come from another Thread and I can't remember if I referred to the demo several things at a time in the other Thread. The approach used in that demo and in planning and implementing a program is the key to non-blocking code execution. Treat the program as a series of small functions and it becomes much easier to manage.


PS it probably would have been easier for everyone if you had stayed with your original Thread.

Thanks, I was trying to start from scratch though and was just after a good tutorial to learn a generic process from rather than fix the specific issue

In the phi-prompt documentation; there is an example that waits for a keypress for 50ms (adjustable)

Would that modification allow you to use your program "as-is"?

Although your source code is well commented and appears to me to be well organized; I'm not super familiar with the phi-prompt library. Have you reached out to Dr. Liu? He might be able to help you, he offers his programming services at, I believe.

while(true) // This code is from page 27 of a PDF document "Phi_prompt documentation 20110523.pdf"
switch (temp1)
case 0: // Nothing happened in 50ms of delay.
case 1: // Up is pressed. Do some up stuff
case 2: // Down is pressed. Do some down stuff
case 3: // Left is pressed. Do some left stuff
case 4: // Right is pressed. Do some right stuff
case 5: // Enter is pressed. Do some enter stuff
case 6: // Escape is pressed. Let’s just quit.

I may try this inside the PID_Run void. the problem I was having is that without using a while loop I either got stuck in the void and couldn't escape or the code would only run once then escape back to the menu

the problem I was having is that without using a while loop I either got stuck in the void and couldn't escape

There are NO voids in your code. There are FUNCTIONS.

My apologies, thank you for the lesson in semantics.

However, it would have perhaps been more constructive to use your experience to suggest an answer to the problem, rather than just correcting my poor use of terminology.

For instance, 'There are NO voids in your code. There are FUNCTIONS.' should read 'There are NO voids in your code; there are FUNCTIONS.'

The use of semicolon is used to connect two independent clauses. In this case it is useful because the subject of the statements 'code' is not applied to the second sentence.

Another global variable could be used... I don't have sample code but will attempt to convey...

byte Menu_Finished = 0;

void setup() {
  // put your setup code here, to run once:


void loop() {
  // put your main code here, to run repeatedly:
if (Menu_Finished == 1) {
// check for button press
// if button pressed; reset - Menu_Finished = 0

else if (Menu_Finished == 0) {
Your_Menu ();



void Your_Menu () {

if ( Menu_Finished == 0 ) {

// Menu not done - wait 50 ms then return
// break;

// Menu done - stop checking this menu
Menu_Finished = 1;


I don't know if this is helping at all. You don't have to use Void functions and can pass variables to your functions. Because you have two top menus and some sub menus, you could pass that information to a menu handler function.

That is the beauty of the phi-prompt library though. It has some of that logic figured out for you. The down side is you are probably cutting and pasting and adding your own bits, that leads to unexpected consequences.

I can feel your pain though, in my case, I'm waiting for key presses on a 4x4 keypad and want things to continue, have interrupts going, etc.

The example post from Robin S was excellent. I had not seen that before even though it is a sticky post. If you were going to start over; it is a great place for you to look. I wish I had read that post sooner. It took a lot of coffee for me to get to the point where my code somewhat resembled what Robin is talking about but I would go back a year later and still have to rethink everything.

Having just read his post yesterday, it was inspirational for me. Doh.