Dont know what to ask and dont even know if its possible

#include <SPI.h>
#include <Arduino.h>
int menuiwant=3,lo;
char buf[20];

const char a1[] PROGMEM = "car/0";
const char a2[] PROGMEM = "bus/0"; 
const char a3[] PROGMEM = "plane/0";
PGM_P const menu1[] PROGMEM = { a1, a2, a3 };

const char b1[] PROGMEM = "john/0";
const char b2[] PROGMEM = "peter/0";
const char b3[] PROGMEM = "sam/0";
PGM_P const menu2[] PROGMEM = { b1, b2, b3 };

const char c1[] PROGMEM = "apple/0";
const char c2[] PROGMEM = "pear/0";
const char c3[] PROGMEM = "strawberry/0";
PGM_P const menu3[] PROGMEM = { c1, c2, c3};
  


void setup() {
Serial.begin(9600);
  
}

void loop() {

switch(menuiwant){
case 1:
    for (lo = 0; lo < 3; lo++) {strcpy(buf, menu1[lo]);Serial.println(buf);}
    break;
case 2:
    for (lo = 0; lo < 3; lo++) {strcpy(buf, menu2[lo]);Serial.println(buf);}
    break;
    case 3:
    for (lo = 0; lo < 3; lo++) {strcpy(buf, menu3[lo]);Serial.println(buf);}
    break;
  } 
while(1){}
}

I have made a handheld unit using esp32 and lcd which basically functions as a fault finding database. The program listed is not the actual program but outlines the current process i use. the variable "menuiwant" is passed to a function which selects the relevant array using "case:" . The database i need has at least 40 menus and what I was wondering is if there is a way to incorporate the variable "menuiwant" into the array name (menu) so i end up with a few lines of code as opposed to 40 repetitions. Please forgive me if this is hard to understand as I dont even know how to describe it. Many thanks

Your code i the case statement refers to menu1, menu2, menu3 but there is no menu 1 or 2, i think what you mean is menu3[menuiwant] but rename menu3 to menu for clarity.

OOPS I didn't scroll, there is menu1 and 2. The solution is a 2 dimension array, something like
menu[0]submenu[0] = a1;
menu[0]submenu[1] = a2;
etc
then you copy from menu[menuiwant]submenu[lo] etc
Is that any help?
there are probably some iprovements to my code but it should work.

Regardless of your question, the string termination character is '\0' not '/0'.

  • Anything is possible :thinking:

  • Place each line of code on a line by itself, much easier for others to read and costs you nothing to do.

  • Place { and } on lines by themselves.

  • Google Arduino F Macro

This is probably what you want.

#include <Arduino.h>

int menuiwant = 0;
int lo = 0;

char Menus[4][3][12] = {
  {"Car", "bus", "plane"},
  {"John", "Peter", "Sam"},
  {"Apple", "Pear", "Strawberry"},
  {"Red", "Green", "Blue"}
};

void setup() {
  Serial.begin(115200);
}

void loop() {

  Serial.print("Menu ");
  Serial.println(menuiwant);
  for (lo = 0; lo < 3; lo++) {
    Serial.println(Menus[menuiwant][lo]);
  }
  Serial.println(" ");

  delay(2000);
  menuiwant++;
  if (menuiwant > 3) menuiwant = 0;
}

Here is a cleaned-up version. You can add the memory location macros if you think they are needed.

#include <Arduino.h>

int Menu = 0;
int menuItem = 0;

char Menus[4][3][12] = {
  {"Car", "bus", "plane"},
  {"John", "Peter", "Sam"},
  {"Apple", "Pear", "Strawberry"},
  {"Red", "Green", "Blue"}
};

void setup() {
  Serial.begin(115200);
}

void loop() {

  Serial.print("Menu ");
  Serial.println(Menu);
  for (menuItem = 0; menuItem < 3; menuItem++) {
    Serial.println(Menus[Menu][menuItem]);
  }
  Serial.println(" ");

  delay(2000);
  Menu = (Menu + 1) % 4;
}

You don't use PROGMEM with ESP32.
I think this is all you need


  const char* Menus[4] =
  { "Menu1",
    "Menu2",
    "Menu3",
    "Menu4"
  };

  // Print menus
  for (int i = 0; i < 4; i++)
    Serial.println(Menus[i]);

what about something more generic


char s [90];

// -----------------------------------------------------------------------------
struct M {
    const char *name;
    M          *next;
};

// -------------------------------------
M mammals [] = {
    { "apes", 0 },
    { "dolphons", 0 },
    { },
};

// ---------------------------
M reptiles [] = {
    { "turtles", 0 },
    { "lizards", 0 },
    { },
};

// ---------------------------
M animal [] = {
    { "birds", 0 },
    { "fish", 0 },
    { "mammals",  mammals },
    { "reptiles", reptiles },
    { },
};

// -------------------------------------
M gems [] = {
    { "quarts", 0 },
    { "saphire", 0 },
    { },
};

// ---------------------------
M sedimentary [] = {
    { "limestone", 0 },
    { },
};

// ---------------------------
M rock [] = {
    { "granit", 0 },
    { },
};

// ---------------------------
M mineral [] = {
    { "aggregate",   0 },
    { "biogenic",    0 },
    { "gems",        gems },
    { "sedimentary", sedimentary },
    { "rock",        rock },
    { },
};

// -------------------------------------
M bush [] = {
    { "beans", 0 },
    { "peas", 0 },
    { "tomators", 0 },
    { },
};

// ---------------------------
M grains [] = {
    { "corn",  0 },
    { "wheat", 0 },
    { },
};

// ---------------------------
M tubors [] = {
    { "carrots",  0 },
    { "potatoes", 0 },
    { "turnips",  0 },
    { },
};

// ---------------------------
M vegetable [] = {
    { "bush",   bush },
    { "grains", grains },
    { "tubors", tubors },
    { },
};

// -------------------------------------
M top [] = {
    { "animal",    animal },
    { "mineral",   mineral },
    { "vegetable", vegetable },
    { },
};

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

const int Nlvl        = 10;
int       lvl         = 0;;
M        *menu [Nlvl] = { top };
M        *m           = top;

// -------------------------------------
void
menuAdv ()
{
    m++;
    if (0 == m->name)
        m = menu [lvl];
    Serial.println (m->name);
}

// -------------------------------------
void
menuExit ()
{
    if (0 < lvl)
        m = menu [--lvl];
    else
        Serial.println (" ^");
    Serial.println (m->name);
}

// -------------------------------------
void
menuSel ()
{
#if 0
    sprintf (s, "sel: name %s, next %p, next-name %s",
                    m->name, m->next, m->next->name);
    Serial.println (s);
#endif

    if (m->next && lvl < Nlvl-1)  {
        menu [++lvl] = m = m->next;
        Serial.println (m->name);
    }
    else
        Serial.println (" .");
}

// -----------------------------------------------------------------------------
byte pinsBut [] = { A1, A2, A3 };
#define N_BUT   sizeof(pinsBut)

byte butState [N_BUT];

enum { Adv, Sel, Exit };

// -------------------------------------
int
chkButtons ()
{
    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        byte but = digitalRead (pinsBut [n]);

        if (butState [n] != but)  {
            butState [n] = but;

            delay (10);     // debounce

            if (LOW == but)
                return n;
        }
    }
    return -1;
}

// -----------------------------------------------------------------------------
void
loop ()
{
    switch (chkButtons ())  {
    case Adv:
        menuAdv ();
        break;

    case Sel:
        menuSel ();
        break;

    case Exit:
        menuExit ();
        break;
    }
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        pinMode (pinsBut [n], INPUT_PULLUP);
        butState [n] = digitalRead (pinsBut [n]);
    }

    Serial.println (m->name);
}