Changing Values in an LCD Menu

I’m building an aquarium light and I’m stuck in the coding. I’ve created a menu, you hit select to choose the option, and now I want to be able to change a value (in this case the light level).

#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;
int b_light_value = 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 ("Blues");
        break;
      case 2:
        clearPrintTitle();
        lcd.print ("Whites");
        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("12:00PM");
  lcd.setCursor(0,1); 
}
 
//Show the selection on Screen.
void selectMenu(int x) {
int y = analogRead(0);
int state_b = 0;

if (y < 100){
  state_b = 1;
}else if (y < 600){
  state_b = 2;
}
   switch (x) {
      case 1:
        clearPrintTitle();
        lcd.print("Blue   ");
        lcd.print(b_light_value);
        break;
      case 2:
        clearPrintTitle();
        lcd.print ("White   ");
        break;
       case 3:
        clearPrintTitle();
        lcd.print ("Selected Opt 3");
        break;
      case 4:
        clearPrintTitle();
        lcd.print ("Selected Opt 4");
        break;
    }
}

I’m not sure where to go from there though. Do I create a b_light_value function that reads the button and then prints in selectMenu or do I read the button and return the value in the switch(x)?

liquidis54:
I’m building an aquarium light and I’m stuck in the coding. I’ve created a menu, you hit select to choose the option, and now I want to be able to change a value (in this case the light level).

#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;
int b_light_value = 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 (“Blues”);
        break;
      case 2:
        clearPrintTitle();
        lcd.print (“Whites”);
        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(“12:00PM”);
  lcd.setCursor(0,1);
}

//Show the selection on Screen.
void selectMenu(int x) {
int y = analogRead(0);
int state_b = 0;

if (y < 100){
  state_b = 1;
}else if (y < 600){
  state_b = 2;
}
  switch (x) {
      case 1:
        clearPrintTitle();
        lcd.print("Blue  ");
        lcd.print(b_light_value);
        break;
      case 2:
        clearPrintTitle();
        lcd.print ("White  ");
        break;
      case 3:
        clearPrintTitle();
        lcd.print (“Selected Opt 3”);
        break;
      case 4:
        clearPrintTitle();
        lcd.print (“Selected Opt 4”);
        break;
    }
}



I'm not sure where to go from there though. Do I create a b_light_value function that reads the button and then prints in selectMenu or do I read the button and return the value in the switch(x)?

Surely you can try one method and then the other to test your program.

Paul