How to make a menu with submenus?

Classes and structs are nice
but you can also use corresponding arrays.

here is a very basic code example that works with your serial monitor.
There is one string array with categories.
and another array that forwards each category to the next.

if the first number in the "forward" array doesn't point. then the second number points to a third array that holds your actual settings.

This simple logic below can do an unlimited number of subcategories and hold an unlimited amount of settings by making the arrays larger.

Hopefully this logic helps.

String labels [11]= {"main","earth","mars","weapon","lazer","mind control","animal","fish","bird","ufo","people"};
String answers [11];  
int settings[6];
int forwards[][2] {{1,2},{6,10},{3,9},{4,5},{-1,0},{-1,1},{7,8},{-1,2},{-1,3},{-1,4},{-1,5}};
int  c = 0;
int  bc = 0;
void setup() {
Serial.begin(9600); delay(1000);
Serial.println("---- Please Select a number -------");
}

void loop() {
int i = 0;int q = -1;
Serial.println("-----------------------------------");

if(forwards[c][0]==-1){
  Serial.print("please enter new number for "+labels[c]+": ");
  Serial.println(settings[forwards[c][1]]);
 settings[forwards[c][1]]=ask();c=bc;
}
else{
while (i <2){
Serial.print((i+1)); 
Serial.println(" "+labels[forwards[c][i]]+" settings");i++;}
Serial.println("3 back");
q = ask()-1;
if(q<3){if(q==2){c=0;bc=0;}else{bc=c;c = forwards[c][q];}
}}
}

int ask(){int r = -1;
  while(r<1){
  while (Serial.available()==0){}
  r = Serial.parseInt();
 }return r;}
2 Likes