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;
}
}}