Creating a menu using rotary encoder

So I am trying to create a menu varying on Maximilian Hentsch's menu because I like the simplicity and layout (Arduino Tutorial - 14. Displaymenü - YouTube) however I am trying to create my own menu with a rotary encoder instead of push buttons.

The issue: I have almost everything I need working somehow, however whenever I turn the encoder in EITHER direction the menu continues down and I cannot get the menu to go up again.

I tried a "switch" function but could not get that to work so now I am back at an "if" function.

here is that code

#define WITH_LCD 1


#include <ClickEncoder.h>
#include <TimerOne.h>

#ifdef WITH_LCD
#include <Wire.h>
#include <hd44780.h> // include hd44780 library header file
#include <hd44780ioClass/hd44780_I2Cexp.h> // i/o expander/backpack class
hd44780_I2Cexp lcd;
#endif

#ifdef WITH_LCD
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
#endif

ClickEncoder *encoder;
int16_t last, value;

void timerIsr() {
  encoder->service();
}


int selectButton = 8;
int menu = 1;

//------------------------------------------------------//

void setup() {
  int status; 
  status = lcd.begin(LCD_COLS, LCD_ROWS);

Serial.begin(9600);
  encoder = new ClickEncoder(0, 1, 8, 2); //SetsData pins, Button pin, and steps per notch of encoders
  
  Timer1.initialize(1000);
  Timer1.attachInterrupt(timerIsr); 
  
  last = 0; //Defining last

  
  pinMode(selectButton, INPUT_PULLUP);
  

  //--------------------------------------------//
  lcd.clear(); //clear the whole LCD
  lcd.setCursor(0,0); //Defining positon to write from first row, first column .
  lcd.print("Garden lights");
  lcd.setCursor(0,1); //Second row, first column
  lcd.print("Project 2022"); 
  lcd.setCursor(0,2); //Second row, first column
  delay(5000); //wait 5 sec
  lcd.clear(); //clear the whole LCD
  //lcd.noBacklight();//
  //--------------------------------------------//
  updateMenu(); //Updates the screen with the menu
}

void loop()
 {value += encoder->getValue();

  if ((value != last)){
   last = value;
    menu++;
    updateMenu();
    delay(100);
    Serial.println(value);
    
  }
  if ((value != last)){
   last = value;
    menu--;
    updateMenu();
    delay(100);
    
  }
  if (!digitalRead(selectButton)){
    executeAction();
    updateMenu();
    delay(100);
    
  
  }
}

void updateMenu() {
  switch (menu) {
    case 0:
      menu = 1;
      break;
    case 1:
      lcd.clear();
      lcd.print(">Lights on Timer");
      lcd.setCursor(0, 1);
      lcd.print(" All Lights On");
      break;
    case 2:
      lcd.clear();
      lcd.print(" Lights on Timer");
      lcd.setCursor(0, 1);
      lcd.print(">All Lights On");
      break;
    case 3:
      lcd.clear();
      lcd.print(">All Lights Off");
      lcd.setCursor(0, 1);
      lcd.print(" Set Brightness");
      break;
    case 4:
      lcd.clear();
      lcd.print(" All Lights Off");
      lcd.setCursor(0, 1);
      lcd.print(">Set Brightness");
      break;
    case 5:
      menu = 4;
      break;
  }
}

void executeAction() {
  switch (menu) {
    case 1:
      action1();
      //code for action 1 goes here//
      break;
    case 2:
      action2();
      //code for action 2 goes here//
      break;
    case 3:
      action3();
      //code for action 3 goes here//
      break;
    case 4:
      action4();
      //code for action 4 goes here//
      break;
  }
}

void action1() {
  lcd.clear();
  lcd.print(">Sunset + 2h");
  delay(1500);
}
void action2() {
  lcd.clear();
  lcd.print(">Lights Now On");
  delay(1500);
}
void action3() {
  lcd.clear();
  lcd.print(">Lights Now Off");
  delay(1500);
}
void action4() {
  lcd.clear();
  lcd.print("%of light dim");
  delay(1500);

  
}

What’s the difference - other than the Serial.print..?
It can never reach the second block, because
last = value within the first if block..?

I am very new to coding so how should it look instead?

I just want it to print to the serial monitor the value of the encoder for now whilst I am debugging

I originally wanted to put something like

   last < value;
    menu++;
    updateMenu();
    delay(100);
    Serial.println(value);
  }

  if ((value != last)){
   last > value;
    menu--;
    updateMenu();
    delay(100);
  }

so that when the value of the encoder si decreasing the menu would go down and vice versa but that also didn't work

You’ve got the original code that you copied from.
Go back to that and dig through how it works.

Unfortunately programming has a learning curve. You’ve just arrived.

I appreciate that, but that's what I have been doing for the past 2 days :frowning: would you be able to point me in the right direction?

That’s what I did…
Two if() blocks that test for the same condition, only the first can effect the desired action.

The second can’t possibly do anything, because the first eliminated the test condition.

Legend! again because I am new to coding I have spent two days trying various things but this solved my problem. thanks so much! :slight_smile:

Glad to help you learn… luckily it was fairly obvious, and easy to fix.
This group is great to help developers, but will rarely type out the code for you.

Enjoy your new hobby.