Hi,
I am beginner in Arduino programming, and i have little problem with my code. I want to navigate through my lcd display menu with 4 buttons. I already did something "working" but I don´t know how to program the function to solve this problem. If I choose subMenu "chooseFile" it will print on display subMenu of "chooseSpool".... I tried a lot of functions and watched a lot of codes but as beginner I don´t understand it yet. I want to ask if it is possible to change this or I must rewrite the code.
Here is the code without function that provides the "chooseFile" subMenu selection. If you know how to write the function I will be so glad for you.
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd (32,16,2);
bool vybrane; //choosed
bool vybrane2; //choosed2
int tlacitko_ok =11; //Enter Button
int tlacitko_spat = 10; //Back Button
int tlacitko_dole = 12; // Down Button
int tlacitko_hore = 13; //Up Button
unsigned int menu;
unsigned int subMenu;
unsigned int subMenu2;
//////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
lcd.init();
lcd.backlight();
pinMode (10, INPUT_PULLUP);
pinMode (11, INPUT_PULLUP);
pinMode (12, INPUT_PULLUP);
pinMode (13, INPUT_PULLUP);
menu= 1;
subMenu=1;
subMenu2=1;
updateMenu();
}
////////////////////////////////////////////////////////////////////////////////////////
void loop() {
if (!vybrane){
if (!digitalRead(tlacitko_dole)){
menu++;
updateMenu();
delay(100);
while (!digitalRead(tlacitko_dole));
}
if (!digitalRead(tlacitko_hore)){
menu--;
updateMenu();
delay(100);
while (!digitalRead(tlacitko_hore));
}
} else {
if (!digitalRead(tlacitko_dole)){
subMenu++;
updateSubMenu();
delay(100);
while (!digitalRead(tlacitko_dole));
}
if (!digitalRead(tlacitko_hore)){
subMenu--;
updateSubMenu();
delay(100);
while (!digitalRead(tlacitko_hore));
}
}
if(!digitalRead(tlacitko_ok)){
if (!vybrane){
subMenu = 1;
vybrane = true;
updateSubMenu();
} else{
chooseSpool();
delay(100);
while(!digitalRead(tlacitko_ok));
}
}
if (!digitalRead(tlacitko_spat)){
if (vybrane){
vybrane = false;
updateMenu();
}else {
delay(100);
updateMenu();
while (!digitalRead(tlacitko_spat));
}
}
}
///////////////////////////////////////////////////////////////////////////////////
void updateMenu(){
switch (menu){
case 1:
lcd.clear();
lcd.print(">Choose spool");
lcd.setCursor(0,1);
lcd.print("Choose file");
break;
case 2 :
lcd.clear();
lcd.print("Choose spool");
lcd.setCursor(0,1);
lcd.print(">Choose file");
break;
case 3:
menu= 1;
updateMenu();
}
}
void updateSubMenu(){
switch (subMenu){
case 1 :
lcd.clear();
lcd.print(">ABS");
lcd.setCursor(0,1);
lcd.print("PLA");
break;
case 2 :
lcd.clear();
lcd.print("ABS");
lcd.setCursor(0,1);
lcd.print(">PLA");
break;
case 3 :
lcd.clear();
lcd.print("PLA");
lcd.setCursor(0,1);
lcd.print(">PETG");
break;
}
}
void chooseSpool (){
switch (subMenu){
case 1 :
lcd.clear();
lcd.print(">ABS");
lcd.setCursor(0,1);
lcd.print("PLA");
break;
case 2 :
lcd.clear();
lcd.print("ABS");
lcd.setCursor(0,1);
lcd.print(">PLA");
break;
case 3 :
lcd.clear();
lcd.print("PLA");
lcd.setCursor(0,1);
lcd.print(">PETG");
break;
}
}
void updateSubMenu2(){
switch (subMenu2){
case 1 :
lcd.clear();
lcd.print(">From SD");
break;
}
}
void chooseFile(){
switch (subMenu2){
case 1 :
lcd.clear();
lcd.print(">From SD");
break;
}
}
Thank you for your responde, but that third level menu is working in my code but i want to help with problem : If i "choose File" on the menu it prints on the display the "choose Spool" submenu not the one what is supposed to print.
You code does call function updateMenu(); recursively.
recursievly means you are calling the function inside exact this function
function updateMenu() calls updateMenu(); this will lead to memory problems
and will have strange sideeffects because whenever the function comesback from the last
call of updateMenu() the code will go on executing at where it was before with its own set
of local variables.
void updateMenu() {
switch (menu) {
case 1:
...
break;
case 2 :
...
break;
case 3:
menu = 1;
updateMenu(); // <<< DON'T ! call the function itself from inside the function
}
}
you have to organise your code in a way that is NON-recursive
if you rebuild your project using the WOKWI-simulator
everybody can participate and test your code without registering to tinkercad
you could organise your code to have one function that does only print the menu-lines
and another one that does
print a space where the ">" momentare is (to delete the ">"-symbol
and then
prints the ">" at the other line
This printing can even be done depending on a number 1 or 2 which you use for the line and for the function that shall be executed
thats nice, but after all i still dont know ho to fix my problem
now i can see what is happening in arduino, but how to change the code to fix that issue.
well, write new sketch. try this time not remake complete display message but moving only one symbol ">". we already show enough examples to learn from.