Menu selection question

Hi,

I ran into a problem trying to make a nested menu.
The code below is a subset of a larger program but this is the only part that I cant figure out.

Basically its for manually controlling relays to start a pump for as long as you hold a button down.
I haven't added that code because I cant get it to just display a screen with the cursor number.

The only part that works is the cursor right. I can move it right and it properly wraps around to the beginning.
I also cant figure out why the back button doesn’t seem to do anything.

Any ideas where im going wrong?

Thanks,
Ian

#include <ButtonDebounce.h>
#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>
LiquidCrystal_PCF8574 lcd(0x3F); // set the LCD address to 0x27 for a 16 chars and 2 line display

//Define Buttons
#define startButton 4
#define nextButton 3
#define backButton 2
unsigned int i = 0;



void setup() {
  // put your setup code here, to run once:
  Wire.begin();
  Wire.beginTransmission(0x27);
  lcd.begin(16, 2); // initialize the lcd

  pinMode(startButton, INPUT_PULLUP); //SEL
  pinMode(nextButton, INPUT_PULLUP); //Next
  pinMode(backButton, INPUT_PULLUP); //Before

  digitalWrite(startButton, HIGH);
  digitalWrite(nextButton, HIGH);
  digitalWrite(backButton, HIGH);

}

void loop() {
  // put your main code here, to run repeatedly:
  {
    lcd.setBacklight(255);
    lcd.home();
    lcd.clear();
    lcd.print("Manual Control");
    delay(1000);

    lcd.setBacklight(255);
    lcd.home();
    lcd.clear();
    lcd.print("Pumps");
    lcd.setCursor(0, 1);
    lcd.print(" 1 2 3 4 5 6 E");
  }

  lcd.cursor();
  lcd.setCursor(0, 1);
  //  boolean state = false;
  while (1) {

    if (digitalRead(nextButton) == LOW) //Move cursor Right
    {
      if (i >= 16)
      {
        lcd.setCursor(0, 1);
        i = 0;
      }
      else if (i >= 0)
      {
        lcd.setCursor(i++, 1);
        delay(250);
      }
      else if (digitalRead(backButton) == LOW) //Move Cursor Left
      {
        if (i >= 16)
        {
          lcd.setCursor(0, 1);
          i = 0;
        }
        else if (i >= 0)
        {
          lcd.setCursor(i--, 1);
          delay(250);
        }
      }

      else if (digitalRead(startButton) == LOW) //Select based on cursor position and display that number
      {
        lcd.home();
        lcd.clear();
        lcd.print(i);
        lcd.setCursor(0, 0);
        }
    }
  }
}

Thanks! My logic is generally off, but that’s another topic :slight_smile:

Edit: I figured out where I went wrong.
I needed to switch the if for the else if. The back button call should be above the next button.
Works now! Thanks!