differentiation between variable names

hi guys,
Im new to this forum and english isnt my main language so i hope you will understand me,
Ive tried to build an interface of a menu system(just a basic stuff, not too fancy)
and lets say that I have some arrays

String menu1[] = {"LED","Serial Read","Infrared Read"};
String menu2[] = {"ON" , "OFF"};
String menu3[] = {"Show in BIN","Show in HEX"};

and i want to have some kind of a for loop that will go across all of the menus
how can i differentiate between all the name?
because i cant do something like this

for(int menu(i) = 1; menu(i)<=3; menu(i)++)

i really hope you guys understood me

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

You can't do this

for(int menu(i) = 1; menu(i)<=3; menu(i)++)

because you have no variable called menu

You do have variables called menu1, menu2 and menu3. But you can't substitute for those digits in your running program because those names will have been reduced to addresses in memory by the compiler.

Perhaps you need a 2 dimensioned array and then you could iterate over it something like this

for (byte n = 0; n < numOuterElements; n++) {
  for (byte m = 0; m < numInnerElements; m++) {
     Serial.println(menu[n][m]);
  }
}

I will leave it to someone else to show you how to create the 2D array because I always get it wrong first time. I'm sure there are dozens of examples on the web.

...R

You can't if the arrays have different names if you want to address them by name.

you would need a 2 dimension array. for example study this code (note that it's a bad practice to use the String class for simple micro-controllers, you'll run into hard to debug memory issues possibly so better go for the c-strings, null terminated array of chars)

in the code below, 2D arrays need to have the same dimensions but because not all menus have the same number of elements, I need to size for the max and pad with NULL the shorter menus

const byte MaxMenu = 3;

const char * myMenus[][MaxMenu] =
{
  {"LED", "Serial Read", "Infrared Read"}, // this is the myMenus[0] array
  {"ON" , "OFF", NULL},                    // this is the myMenus[1] array
  {"Show in BIN", "Show in HEX", NULL}     // this is the myMenus[2] array
};

const byte nbMenus = sizeof(myMenus) / sizeof(myMenus[0]);

void setup() {
  Serial.begin(115200);
  Serial.print("You have "); Serial.print(nbMenus); Serial.println(" Menus");

  for (int i = 0; i < nbMenus; i++)
    for (int j = 0; j < MaxMenu; j++) {
      if (myMenus[i][j] != NULL) {    // don't show NULL strings 
        Serial.print("myMenus["); 
        Serial.print(i);
        Serial.print("]["); 
        Serial.print(j); 
        Serial.print("] = ");
        Serial.println(myMenus[i][j]);
      }
    }
}

void loop() {}

Running this code will show in the console (set at 112500 bauds)

[sub][color=blue]You have 3 Menus
myMenus[0][0] = LED
myMenus[0][1] = Serial Read
myMenus[0][2] = Infrared Read
myMenus[1][0] = ON
myMenus[1][1] = OFF
myMenus[2][0] = Show in BIN
myMenus[2][1] = Show in HEX
[/color][/sub]

or create a function and call it for every menu array

Also note that this line:

  • for(int menu(i) = 1; menu(i)<=3; menu(i)++)*
    won't work because arrays use brackets, not parentheses, for its elements.

guys, thank you so much!
really helped me

xextrude:

String menu1[] = {"LED","Serial Read","Infrared Read"};

String menu2[] = {"ON" , "OFF"};
String menu3[] = {"Show in BIN","Show in HEX"};

Let's assume you are using char arrays instead of strings.

There are two main ways to do this - a two dimensional array, and a "ragged" array.

A two dimensional array crates a block of - in this case - nine pointers to char, and initializes those pointers to point at character constants

char * menu[3][3] = {
 {"LED","Serial Read","Infrared Read"},
 {"ON" , "OFF"},
 {"Show in BIN","Show in HEX"}
};
[code]

There is a bit of a problem here, because one of your rows only has two items in it. There are a couple of ways to fix that. One is to store the count of items in each row in another array, or to use a struct.

But the usual way is to use a marker at the end of the list - something that's not a string. Specifically, you use NULL. The benefit of this is that in if and while statements, NULL evaluates to boolean false, meaning that you can use it as a loop condition.

[code]
char * menu[3][4] = {
 {"LED","Serial Read","Infrared Read", NULL},
 {"ON" , "OFF", NULL},
 {"Show in BIN","Show in HEX", NULL}
};

…

for(int i = 0; i < 3; i++) {
  Serial.println("-----")
  for(j = 0; j<3;j++) {
    if(!menu[i][j]) break;
    Serial.println(menu[i][j]);
  }
}

The other way to do this is with a ragged array. In this, rather than storing a 3*4 array, you store an array of pointers to arrayts of ponters to strings (arrays of char). These can each be any length.

char * m1[] = {"LED","Serial Read","Infrared Read",NULL};
char *m2[] =  {"ON" , "OFF",NULL};
char *m3[] = {"Show in BIN","Show in HEX",NULL};
char **menu[] = {m1, m2, m3, NULL};

You can address these exactly the same as with the code above, using i and j. However, the use of ragged arrays allows you to use pointers to step through the arrays.

  for(char ***m = menu; *m; m++) {
    for(char **i = *m; *i; i++) {
      Serial.print(*i);
    }
  }

The ragged arrays is a technic to know indeed

There is a bit of a problem here, because one of your rows only has two items in it

In my code above I chose not to go for the ragged arrays as the difference in number of entries in each array was too small to justify adding a NULL at end of each array and creating a new one. You are better off to save memory and simplify the data access to use the [3][3] array and formally (if you want to improve code readability because the compiler will do that for you for a global variable) pad the shorter sub-arrays with NULL as I did. For his case that leads to very easy to understand code for beginners rather than dealing with pointer to pointer to pointer of char :slight_smile:

Of course to really save RAM, best would be to use PROGMEM