Go Down

Topic: Making LCD menu's: multidimensional char arrays. (Read 2080 times) previous topic - next topic

MG2R

I'm trying to make a multidimensional character array to hold menu strings in an LCD project.
The idea is that I have a maximum of MAX menu items. Every menu item has ROW rows. Every row contains a maximum of CHAR characters. After reading for a bit, I found out that 2D arrays need to be declared via a pointer. The problem is that it doesn't work for a 3D array!

The following code displays empty lines, but I cannot find my mistake.

Code: [Select]

#define CHAR 16   //Number of characters per LCD line
#define MAX  100  //Max number of menu items
#define ROW  2    //Number of LCD rows

char firstrun[MAX][ROW][CHAR]=
      {  {  "This is menu 01", "This is menu 02"  },
         {  "This is menu 11", "This is menu 12"  }  };

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

void loop()
{
 while(Serial.available()==0){}
 int i=Serial.read()-'0';
   
 lcdWrite(firstrun[i][0], firstrun[i][1]);
}

//commented some lines out for debugging via serial monitor
void lcdWrite(char line1[], char line2[])
{
  //lcdPosition(0,0);
  Serial.println(line1);
  //lcdPosition(1,0);
  Serial.println(line2);
  return;
}

PaulS

Quote
After reading for a bit, I found out that 2D arrays need to be declared via a pointer.

You are reading in the wrong place, then.

However, even before you begin to initialize the array, you need to consider this:
Code: [Select]
#define CHAR 16   //Number of characters per LCD line
#define MAX  100  //Max number of menu items
#define ROW  2    //Number of LCD rows

16 * 100 * 2 = 3200 bytes of the 2K that you have available, if you are using a 328-based board.

Try making MAX a (much) more reasonable value.

MG2R


You are reading in the wrong place, then.


http://arduino.cc/en/Reference/String looked quite reliable...

Apparantly, the code works fine if I use char firstrun[][ROW][CHAR] instead of char firstrun[MAX][ROW][CHAR]. Probably because of the memory limitation. Thanks!

PaulS

Quote
Apparantly, the code works fine if I use char firstrun[][ROW][CHAR] instead of char firstrun[MAX][ROW][CHAR]. Probably because of the memory limitation. Thanks!

The [] on the first declaration tells the compiler to count the initializers. Since you are only providing 2, not 100, the array only takes 2 * 2 * 16 bytes, or 64 bytes. Much less than the 3200 that the [MAX] version needs.

MG2R


The [] on the first declaration tells the compiler to count the initializers. Since you are only providing 2, not 100, the array only takes 2 * 2 * 16 bytes, or 64 bytes. Much less than the 3200 that the [MAX] version needs.

Interesting! That's actually ideal, because the array won't be changed, ever (once the code is final) :) Thanks again!

Go Up