Restart loop

Hello
I am not so good in this but I am taking it step by step:-)
I found this program module and it works perfect for me as an template.
Its built around 16×2 (LCD1602) LCD and 6 Button Keypad.

The problem is when i want to restart it after an menu function has been completed.

#include <LiquidCrystal.h>
 
// Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
 
//States for the menu.
int currentMenuItem = 0;
int lastState = 0;
 
void setup() {
   //Set the characters and column numbers.
   lcd.begin(16, 2);
   //Print default title.
   clearPrintTitle();
}
 
void loop() {
  //Call the main menu.
  mainMenu();
}
 
void mainMenu() {
  //State = 0 every loop cycle.
  int state = 0;
  //Refresh the button pressed.
  int x = analogRead (0);
  //Set the Row 0, Col 0 position.
  lcd.setCursor(0,0);
 
  //Check analog values from LCD Keypad Shield
  if (x < 100) {
    //Right
  } else if (x < 200) {
   //Up
    state = 1;
  } else if (x < 400){
   //Down
    state = 2;
  } else if (x < 600){
    //Left
  } else if (x < 800){
    //Select
    state = 3;
  }
 
  //If we are out of bounds on th menu then reset it.
  if (currentMenuItem < 0 || currentMenuItem > 4) {
   currentMenuItem = 0; 
  }
 
   //If we have changed Index, saves re-draws.
   if (state != lastState) {
      if (state == 1) {
         //If Up
          currentMenuItem = currentMenuItem - 1; 
          displayMenu(currentMenuItem);
      } else if (state == 2) {
         //If Down
          currentMenuItem = currentMenuItem + 1;  
          displayMenu(currentMenuItem);
      } else if (state == 3) {
         //If Selected
         selectMenu(currentMenuItem); 
      }
      //Save the last State to compare.
      lastState = state;
   } 
   //Small delay
  delay(5);
}
 
//Display Menu Option based on Index.
void displayMenu(int x) {
     switch (x) {
      case 1:
        clearPrintTitle();
        lcd.print ("-> Menu Option 1");
        break;
      case 2:
        clearPrintTitle();
        lcd.print ("-> Menu Option 2");
        break;
       case 3:
        clearPrintTitle();
        lcd.print ("-> Menu Option 3");
        break;
      case 4:
        clearPrintTitle();
        lcd.print ("-> Menu Option 4");
        break;
    }
}
 
//Print a basic header on Row 1.
void clearPrintTitle() {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(" HACKSHED.CO.UK ");
  lcd.setCursor(0,1); 
}
 
//Show the selection on Screen.
void selectMenu(int x) {
   switch (x) {
      case 1:
        clearPrintTitle();
        lcd.print ("Selected Opt 1");
        //Call the function that belongs to Option 1
        break;
      case 2:
        clearPrintTitle();
        lcd.print ("Selected Opt 2");
        //Call the function that belongs to Option 2
        break;
       case 3:
        clearPrintTitle();
        lcd.print ("Selected Opt 3");
        //Call the function that belongs to Option 3
        break;
      case 4:
        clearPrintTitle();
        lcd.print ("Selected Opt 4");
        //Call the function that belongs to Option 4
        break;
    }
}

Moderator edit: CODE TAGS. Why is it so hard to read a few simple instructions before posting?

The problem is when i want to restart it after an menu function has been completed.

What is "it" that you want to start? How do you know that a "menu function is completed"?

Having a function, like loop(), do nothing more than call one function (mainMenu()) is pretty stupid. You are just wasting time and memory making a function call.

As soon as mainMenu() ends, loop() will be called again, so it isn't clear why you think you need to restart loop().

Thank you for your response

What i meant is
I choose menu option 1 and ends up in the code below.
After that the function connected to option 1 is finished.
Program come back to case 1, right?
After that i want the menusystem to "restart" from start.

case 1:
clearPrintTitle();
lcd.print ("Selected Opt 1");
//Call the function that belongs to Option 1
break;

After that the function connected to option 1 is finished.
Program come back to case 1, right?

No. The break statement says to exit the switch statement, which means that displayMenu() ends, and returns to mainMenu(). There is nothing else to do in mainMenu(), so it ends and returns. There is nothing else to do in loop(), so loop() returns, and gets called again. That calls mainMenu() again. state is set to 0, and the process starts over again.

Sorry for not understanding :slight_smile:
When i run the code and push down to -> Menu Option 1 and press select
now the display shows Selected Opt 1
In this situation i want the menu to restart from the beginning,

When i run the code and push down to -> Menu Option 1 and press select

I'm going to assume that English is not your native language. I do not understand that this means.

"push down"? Specifically, what are you doing?
"press select"? Specifically, what are you doing?

You realize, I hope, that there is nothing blocking in your code. Nothing waits for events to happen. It seems that you might be assuming that mainMenu() waits for you to press a switch. It does not.

Sorry from sweden !

"push down" means press the down key once
"press select" means press the select key once

Some things could make your code easier to understand.

#define UP 1
#define DOWN 2
#define SELECT 3
   if (state != lastState)
   {
      if (state == UP)
      {
          currentMenuItem = currentMenuItem - 1; 
          displayMenu(currentMenuItem);
       }
       else if (state == DOWN)
       {
          currentMenuItem = currentMenuItem + 1;  
          displayMenu(currentMenuItem);
       }
       else if (state == SELECT)
       {
          selectMenu(currentMenuItem); 
       }

       //Save the last State to compare.
       lastState = state;
   }

Select documenting code is much better than extraneous comments.

The analog values that indicate which switch is pressed could be in an array. Which switch is pressed, if any, would be the index of the array, so a simple for loop, consisting of 5 lines (counting the { and the } as separate lines), instead of the 14 lines you have now.

Anyway, the issue seems to be that after the select key is pressed, selectMenu() is called and ends, causing mainMenu() to end, causing loop() to end, resulting in loop() and mainMenu() being called again. And that either isn't what you want, or what happens when mainMenu() is called again isn't what you want.

But, it isn't clear what, exactly, you want to have happen after "selecting" an option. Nor is it exactly clear what does happen.

Thanks a lot!!