Sub-menu not working correctly

In the "Menu1" sub-menu, not able to rotate the knob to select different options and press the knob to confirm my selection, "case 0;" is start working as we inter the "Menu1" sub-menu"

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

LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize the I2C LCD with the address 0x27 and size 16x2

const int encoderCLK = 7; // CLK pin of the rotary encoder
const int encoderDT = 6;  // DT pin of the rotary encoder
const int encoderSW = 5;  // SW pin of the rotary encoder (push button)
const int ledPin = 13;     // Pin for the LED

int lastEncoderCLKState = HIGH;
int currentEncoderCLKState;
int menuIndex = 0;
int subMenuIndex = 0;
bool inSubMenu = false; // To track whether in sub-menu A or not

// Define the menu structure
const char mainMenuItems[3][10] = {"Menu1", "Menu2", "Menu3"};
const char subMenuAItems[4][10] = {"SubMenu1", "SubMenu2", "SubMenu3", "SubMenu4"};

void setup() {
  lcd.init();
  lcd.backlight();  // Turn on the backlight
  lcd.print("Main Menu:");
  lcd.setCursor(0, 1);
  lcd.print(mainMenuItems[menuIndex]); // Display the current main menu item

  pinMode(encoderCLK, INPUT_PULLUP); // Set CLK pin as input with internal pull-up resistor
  pinMode(encoderDT, INPUT_PULLUP);  // Set DT pin as input with internal pull-up resistor
  pinMode(encoderSW, INPUT_PULLUP);  // Set SW pin as input with internal pull-up resistor

  pinMode(ledPin, OUTPUT); // Set LED pin as output
}

void loop() {
  // Read the state of the rotary encoder
  currentEncoderCLKState = digitalRead(encoderCLK);

  // Check if the encoder has been rotated
  if (currentEncoderCLKState != lastEncoderCLKState) {
    if (digitalRead(encoderDT) != currentEncoderCLKState) {
      menuIndex = (menuIndex - 1 + 3) % 3; // Move to the previous main menu item
    } else {
      menuIndex = (menuIndex + 1) % 3; // Move to the next main menu item
//      
    }
    displayMainMenu();
  }

  // Check if the push button on the rotary encoder is pressed
  if (digitalRead(encoderSW) == LOW) {
    if (!inSubMenu) {
      if (menuIndex == 0) {
        inSubMenu = true;
        displaySubMenuA();
      } else if (menuIndex == 1) {
        lcd.clear();
        lcd.print("Menu2");
        delay(1000);
        lcd.clear();
        lcd.print("Main Menu:");
        lcd.setCursor(0, 1);
        lcd.print(mainMenuItems[menuIndex]); // Display the current main menu item
      } else if (menuIndex == 2) {
        lcd.clear();
        lcd.print("Menu3");
        delay(1000);
        lcd.clear();
        lcd.print("Main Menu:");
        lcd.setCursor(0, 1);
        lcd.print(mainMenuItems[menuIndex]); // Display the current main menu item
      }
    } 
}}

void displayMainMenu() {
  lcd.setCursor(0, 1);
  lcd.print("                "); // Clear the current line
  lcd.setCursor(0, 1);
  lcd.print(mainMenuItems[menuIndex]); // Display the current main menu item
}

void displaySubMenuA() {
  lcd.clear();
  lcd.print("Menu1:");
  lcd.setCursor(0, 1);
  lcd.print(subMenuAItems[subMenuIndex]); // Display the current sub-menu A item

  while (inSubMenu) {
    currentEncoderCLKState = digitalRead(encoderCLK);
    if (currentEncoderCLKState != lastEncoderCLKState) {
      if (digitalRead(encoderDT) != currentEncoderCLKState) {
        subMenuIndex = (subMenuIndex + 1) % 4; // Move to the next sub-menu A item
      } else {
         subMenuIndex = (subMenuIndex - 1 + 4) % 4; // Move to the previous sub-menu A item
      }
      lcd.setCursor(0, 1);
      lcd.print("                "); // Clear the current line
      lcd.setCursor(0, 1);
      lcd.print(subMenuAItems[subMenuIndex]); // Display the current sub-menu A item
    }

    if (digitalRead(encoderSW) == LOW) {

      switch (subMenuIndex) {
        case 0:
         lcd.clear();
          lcd.print("5");
          inSubMenu = false;
          subMenuIndex = 0; 
      delay(5000);
      lcd.clear();
      lcd.print("Main Menu:");
      lcd.setCursor(0, 1);
      lcd.print(mainMenuItems[menuIndex]); // Display the current main menu item
        
        break;



        
        case 1:
          lcd.clear();
          lcd.print("15");
          inSubMenu = false;
          subMenuIndex = 0; 
      delay(5000);
      lcd.clear();
      lcd.print("Main Menu:");
      lcd.setCursor(0, 1);
      lcd.print(mainMenuItems[menuIndex]); // Display the current main menu item
      break;
        case 2:
          lcd.clear();
          lcd.print("20");
          inSubMenu = false;
          subMenuIndex = 0; 
      delay(5000);
      lcd.clear();
      lcd.print("Main Menu:");
      lcd.setCursor(0, 1);
      lcd.print(mainMenuItems[menuIndex]); // Display the current main menu item
      break;
        case 3:
          lcd.clear();
          lcd.print("30");
          inSubMenu = false;
          subMenuIndex = 0; 
      delay(5000);
      lcd.clear();
      lcd.print("Main Menu:");
      lcd.setCursor(0, 1);
      lcd.print(mainMenuItems[menuIndex]); // Display the current main menu item
      break;
      }

    
    lastEncoderCLKState = currentEncoderCLKState;
    }
}}

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

1 Like

Hello abhixs

If you expect a realtime behaiviour of the program you have to get rid of the delay() function. This function blocks the execution.

	Line  59:         delay(1000);
	Line  67:         delay(1000);
	Line 111:       delay(5000);
	Line 127:       delay(5000);
	Line 138:       delay(5000);
	Line 149:       delay(5000);

Design and code your own none-blocking timer function by using the BlinkWithoutOut example of the IDE.

Have a nice day and enjoy coding in C++.

i removed delay but its not working, my only issue is that "case 0;" is start working as we inter the "Menu1" sub-menu" it should work after pressing the knob.

Next step for debugging:

Use a logic analyzer to see what happens.

Insert Serial.println()´s at points of interrest and analyze the test results.

Please post your revised code as a new message.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.