need help to write codes for sequential menu

Good day to all,
i am trying yo write code to display menu when a key from keypad. i am using 162 lcd display and 43 matrix keypad.
my requirements are
when '3' is pressed displaying the menu starts from point 1, and after that if '2' is pressed previous two menu points will get displayed and if '5' is pressed next two menu points will get displayed,
here is menu

  1. Set Point
  2. Dose timing
  3. Dose interval
  4. ph lower/Upper
  5. forced dose
  6. alarm-deviation
  7. alarm-dose/hr
    8 .alarm on/off
  8. calibration ph7
    10.calibration ph4
    and one thing for which i have absolutely no idea how to do is, if anywhere in the middle if '0' is pressed program must return to main loop.

i apologies if i havent use language properly of any other mistake.
thank you for help.
here is the code that i have written:

#include <Keypad.h>

#include <LiquidCrystal.h>
LiquidCrystal lcd (5,4,3,2,1,0);
const byte ROWS = 4;
const byte COLS = 3;
char mainKeys [4][3] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};

char menuKeys [4][3] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'','0','#'}
};
char level_1Keys [4][3] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'
','0','#'}
};
char level_2Keys [4][3] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'','0','#'}
};
char level_3Keys [4][3] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'
','0','#'}
};
char level_4Keys [4][3] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'','0','#'}
};
char leve_5Keys [4][3] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'
','0','#'}
};

byte rowPins [4] = {12,11,10,9};
byte colPins [3] = {8,7,6};

Keypad mainpad (makeKeymap(mainKeys),rowPins,colPins,sizeof(rowPins),sizeof(colPins));
Keypad level_1pad (makeKeymap(level_1Keys),rowPins,colPins,sizeof(rowPins),sizeof(colPins));
Keypad level_2pad (makeKeymap(level_2Keys),rowPins,colPins,sizeof(rowPins),sizeof(colPins));
Keypad level_3pad (makeKeymap(level_3Keys),rowPins,colPins,sizeof(rowPins),sizeof(colPins));
Keypad level_4pad (makeKeymap(level_4Keys),rowPins,colPins,sizeof(rowPins),sizeof(colPins));
Keypad level_5pad (makeKeymap(level_5Keys),rowPins,colPins,sizeof(rowPins),sizeof(colPins));

void setup()
{
lcd.begin (16,2);
lcd.print (" WELCOME ");
delay (3000);
lcd.clear ();

}

void loop() {
char key = mainpad.getKey ();
if ( key!= NO_KEY)
{
if (key =='3')
{
lcd.clear ();
menu ();
}
}
else {
{
lcd.setCursor (0,0);
lcd.print ("Current pH:7.0");
}
}
}

char menu ()
{
lcd.setCursor (0,0);
lcd.print ("1. Set Ppint");
delay (30);
lcd.setCursor (0,1);
lcd.print ("2. Dose Timing");
char key_1 = level_1.getKey ();
if (key_1!=NO_KEY)
{
if (key_1 == '2')
{
level_5 ();
}
else {
switch (key_1){
case 5:{
level_2 ();}
break ();
case 0:{
done ();}
break;
default:{}
break;
}
}
}
else {
/do nothing/
}
}

char level_2 ()
{
lcd.setCursor (0,0);
lcd.print ("3. Dose Interval");
delay (30);
lcd.setCursor (0,1);
lcd.print ("4. pH Lower/Upper");
char key_2 = level_2.getKey ();
if (key_2!=NO_KEY)
{
if (key_2 == '2')
{
menu ();
}
else {
switch (key_2){
case 5:{
level_3 ();}
break ();
case 0:{
done ();}
break;
default:{}
break;
}
}
}
else {
/do nothing/
}
}

char level_3 ()
{
lcd.setCursor (0,0);
lcd.print ("5. Forced Dose");
delay (30);
lcd.setCursor (0,1);
lcd.print ("6. Alarm-Deviation");
char key_3 = level_3.getKey ();
if (key_3!=NO_KEY)
{
if (key_3 == '2')
{
level_2 ();
}
else {
switch (key_3){
case 5:{
level_4 ();}
break ();
case 0:{
done ();}
break;
default:{}
break;
}
}
}
else {
/do nothing/
}
}

char level_4 ()
{
lcd.setCursor (0,0);
lcd.print ("7.Alarm- Dose/hr");
delay (30);
lcd.setCursor (0,1);
lcd.print ("8. Alarm On/Off");
char key_4 = level_4.getKey ();
if (key_4!=NO_KEY)
{
if (key_4 == '2')
{
level_3 ();
}
else {
switch (key_4){
case 5:{
level_5 ();}
break ();
case 0:{
done ();}
break;
default:{}
break;
}
}
}
else {
/do nothing/
}
}

char level_5 ()
{
lcd.setCursor (0,0);
lcd.print ("9.Calb pH7");
delay (30);
lcd.setCursor (0,1);
lcd.print ("10. Calb ph4");
char key_5 = level_5.getKey ();
if (key_5!=NO_KEY)
{
if (key_5 == '2')
{
level_4 ();
}
else {
switch (key_5){
case 5:{
menu ();}
break ();
case 0:{
done ();}
break;
default:{}
break;
}
}
}
else {
/do nothing/
}
}

void done ()
{

}
}

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

The multiple copies of exactly the same data look bad. Particularly because you've mispelled one of them (leve_5). If the data is the same and always the same, then you only need one copy. You can have many references to that one copy.

How many keypads are physically attached to this thing? Only one or five? I think it's only one, so you should only use one keypad object.

Think about the main loop() part of the program always repeating at high speed. The thing is controlling some physical device so it needs to be able to check the physical inputs several thousand times per second. Monitoring the keypad and responding to button pushes is a much lower priority.

So the thing is doing basically the same loop over and over. How does it know that you've entered the second level of the dosing menu? It needs to store a "state" variable to remember what happened in the past. Depending on what state your menu is in, there will be different stuff displayed on the screen and pressing the same button will result in different actions.

When carrying out those actions, the screen will be updated. (Don't write to the screen on every loop.)