Pages: [1]   Go Down
Author Topic: Making LCD menu's: multidimensional char arrays.  (Read 1817 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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;
 }
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 610
Posts: 49037
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 610
Posts: 49037
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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) smiley Thanks again!
Logged

Pages: [1]   Go Up
Jump to: