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