Loop issue

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

LiquidCrystal_I2C lcd(0x27, 20, 4);

int upButton = 10;
int downButton = 11;
int selectButton = 12;
int menu = 1;

void setup() {
  lcd.init();
  lcd.backlight();
  pinMode(upButton, INPUT_PULLUP);
  pinMode(downButton, INPUT_PULLUP);
  pinMode(selectButton, INPUT_PULLUP);
  updateMenu();
}

void loop() {
  if (digitalRead(downButton) == HIGH) {
    menu++;
    updateMenu();
    delay(100);
  }
  if (digitalRead(upButton) == HIGH) {
      menu--;
      updateMenu();
      delay(100);
  }
  if (digitalRead(selectButton) == HIGH) {
    executeAction();
    updateMenu();
    delay(100);
  }
}

void updateMenu() {
  switch (menu) {
  case 0:
    menu = 1;
    break;
  case 1:
    menu = 1;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(">MenuItem1");
    lcd.setCursor(0, 1);
    lcd.print(" MenuItem2");
    lcd.setCursor(0, 2);
    lcd.print(" MenuItem3");
    lcd.setCursor(0, 3);
    lcd.print(" MenuItem4");
    break;
  case 2:
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(" MenuItem1");
    lcd.setCursor(0, 1);
    lcd.print(">MenuItem2");
    lcd.setCursor(0, 2);
    lcd.print(" MenuItem3");
    lcd.setCursor(0, 3);
    lcd.print(" MenuItem4");
    break;
  case 3:
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(" MenuItem1");
    lcd.setCursor(0, 1);
    lcd.print(" MenuItem2");
    lcd.setCursor(0, 2);
    lcd.print(">MenuItem3");
    lcd.setCursor(0, 3);
    lcd.print(" MenuItem4");
    break;
  case 4:
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(" MenuItem1");
    lcd.setCursor(0, 1);
    lcd.print(" MenuItem2");
    lcd.setCursor(0, 2);
    lcd.print(" MenuItem3");
    lcd.setCursor(0, 3);
    lcd.print(">MenuItem4");
    break;
  case 5:
    menu = 4;
    break;
  }
}

void executeAction() {
  switch (menu) {
  case 1:
    action1();
    break;
  case 2:
    action2();
    break;
  case 3:
    action3();
    break;
  case 4:
    action4();
    break;
  }
}

void action1() {
  lcd.clear();
  lcd.print(">Executing #1");
  delay(1500);
}
void action2() {
  lcd.clear();
  lcd.print(">Executing #2");
  delay(1500);
}
void action3() {
  lcd.clear();
  lcd.print(">Executing #3");
  delay(1500);
}
void action4() {
  lcd.clear();
  lcd.print(">Executing #4");
  delay(1500);
}

My issue seems to be local to… void(loop)

If I switch up/downButton from HIGH to LOW the menu will scroll straight to the bottom.
If I hit the downButton to lower the menu it only drops once then redraws back to the top.

It seems that the if loop isn’t doing exactly what I thought it should.

It seems that the if loop isn't doing exactly what I thought it should.

Yet you don't print anything useful in loop(), to tell you what is happening. Why not?

Adding ANY code, until you KNOW the switches are wired correctly is pointless.

Your 'switch' statement sets 'menu' to 1 when 'menu' is 1 ?

It is possible but unusual to use a pin mode of INPUT_PULLUP in combination with looking for a HIGH. Please provide a wiring diagram or a schematic, and information about your switches.

PaulS:
Yet you don’t print anything useful in loop(), to tell you what is happening. Why not?

Adding ANY code, until you KNOW the switches are wired correctly is pointless.

I have been playing with the code adding the menu = 1 or menu = 0 inside the switch statement to see what it does with the output, but really thought this would work how i’d want, which confuses me even more and that’s why I am asking for help so I can better understand this.

Heres a pic of how my buttons are wired.

The LCD is I2C and is wire following that format as seen in the code, and the board is a mega.

Are you saying I should try to put the loops code into its own void, and call that inside the loop?

keyboardcowboy:

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);

char *pszMenuItems =
{
    “MenuItem1”,
    “MenuItem2”,
    “MenuItem3”,
    “MenuItem4”
};

void action1( void );
void action2( void );
void action3( void );
void action4( void );

typedef void (*myFunc)();
myFunc Functions[4] =
{
    &action1,
    &action2,
    &action3,
    &action4
};

int upButton = 10;
int downButton = 11;
int selectButton = 12;
int menu = 1;

void setup()
{
    Serial.begin(9600);
    lcd.init();
    lcd.backlight();
    pinMode(upButton, INPUT_PULLUP);
    pinMode(downButton, INPUT_PULLUP);
    pinMode(selectButton, INPUT_PULLUP);
    updateMenu();
}

void loop()
{
    static unsigned long
        timeButtonRead = 0;
    static byte
        lastupButton = 0xff,
        lastselButton = 0xff,
        lastdownButton = 0xff;
    byte
        currButton;
    unsigned long
        timeNow;

//check buttons every 75mS
    timeNow = millis();
    if( timeNow - timeButtonRead < 75 )
        return;
    timeButtonRead = timeNow;

//read the button…
    currButton = digitalRead(downButton);
    //…if it’s state is not the same as last…
    if( currButton != lastdownButton )
    {
        //…save the new state
        lastdownButton = currButton;
        //if it’s low indicating it has been pressed…
        if( currButton == LOW )
        {
            //process action; in this case, if menu is greater than 1…
            if( menu > 1 )
            {
                //…decrement it             
                menu–;
                //and update the menu
                updateMenu();
             
            }//if
             
        }//if

}//if

//same comments for downbutton apply for up and select
    currButton = digitalRead(upButton);
    if( currButton != lastupButton )
    {
        lastupButton = currButton;
        if( currButton == LOW )
        {
            if( menu < 4 )
            {
                menu++;
                updateMenu();
             
            }//if
             
        }//if

}//if

currButton = digitalRead(selectButton);
    if( currButton != lastselButton )
    {
        lastselButton = currButton;
        if( currButton == LOW )
        {
            //call the function pointed to by menu in the Functions array
            Functionsmenu-1;
            //when it returns, update the menu
            updateMenu();
             
        }//if

}//if

}//loop

void updateMenu()
{
    lcd.clear();
    Serial.print( “\n\n\n\n” );
    for( int i=0; i<4; i++ )
    {
        lcd.setCursor(0, i);
        //if menu line is selected, print a ‘>’, otherwise a space
        if( i == (menu-1) )
        {
            Serial.print( “>” );
            lcd.print( “>” );
        }
        else
        {
            Serial.print( " " ); 
            lcd.print( " " );
         
        }//else
     
        //print the text of the line item
        Serial.println( pszMenuItems[i] ); 
        lcd.print( pszMenuItems[i] );
     
    }//for
     
}//updateMenu

void action1()
{
    lcd.clear();
    lcd.print(">Executing #1");
    Serial.println(">Executing #1");
    delay(1500);
}//action1

void action2()
{
    lcd.clear();
    lcd.print(">Executing #2");
    Serial.println(">Executing #2"); 
    delay(1500);
}//action2

void action3()
{
    lcd.clear();
    lcd.print(">Executing #3");
    Serial.println(">Executing #3"); 
    delay(1500);
}//action3

void action4()
{
    lcd.clear();
    lcd.print(">Executing #4");
    Serial.println(">Executing #4"); 
    delay(1500);
}//action4




It works, I had a f'd up button. I was really running out of ideas.
The initial output is Executing #2 then it prints menu with the cursor at menuItem2.

Last night I added a 4th button to my board as a test reference just in case it was a faulty button.

So this is solved, I had previously made a screen that prints out sensor info and would like those outputs to each be in there own menu and with the switch statement void's that should be easy.

My desire is to learn the language while making something useful for someone else or myself, and it just had me confused after I really thought I had it.

Last thing I wanted to do was sign up on a forum and possibly get thrown further off track in my learning. I am really appreciative I took the risk, I thought it'd just sit around and people would be like well I understand it so you should and not offer any insight on the matter, when I am trying to learn.

This interactive programming makes me greater interested in learning as opposed to Python, it's easier for me to view what I want and have better direction at figuring out as opposed to learning just the language.

Now I just want to read through the code and see the differences and where I overlooked so I can progress. 

BlackFin was the member who resolved my issue, he truly is a God member!