Go Down

Topic: Multidimensional array and a memory problem. (Read 1 time) previous topic - next topic

kapelan

stack with multidimensional  array and memory problem.
looking for a pretty big for Arduino 2 dimensional array of integers.

Tried to do like this
int My_array[20][10] = {....}
... and got a memory problem from Mega2560 (TFT3.2) :(

Tried from http://arduino.cc/en/Reference/PROGMEM
prog_int16_t my_array[20][7] PROGMEM =
{
  {10,40,50,60,2,1}
  ,{10,70,50,170,1,2}
.... } 
   ... and got a memory problem

Probably explanation of the memory problem is here: http://www.nongnu.org/avr-libc/user-manual/pgmspace.html
On the same site an example with string exists, that provides 2 dimensions. But the second dimension is a string.
In this case it will require a parser to int.
Does anybody have an example with multidimensional integer?

PaulS

A 20 * 10 array of ints is 400 bytes, of the 8096 available on the Mega. What makes you think that this array is causing memory problems?

Quote
... and got a memory problem

What problem was that? Are there larger values in the parts of the array that you did not post? Why is the array type int, rather than byte?

kapelan


A 20 * 10 array of ints is 400 bytes, of the 8096 available on the Mega. What makes you think that this array is causing memory problems?

Quote
... and got a memory problem

What problem was that? Are there larger values in the parts of the array that you did not post? Why is the array type int, rather than byte?

one of them:
http://arduino.cc/forum/index.php/topic,147243.msg1107000.html#msg1107000
another one: it reads a data that was not defined from the array.It is actually a very ugly problem: you never know if the data is correct until release some memory and get a correct result.
Byte cannot be used because I need a bigger then a byte:
prog_int16_t my_array[20][7] PROGMEM =
{
  {1000,4000,500,600,2,1}
  ,{1000,700,500,1700,1,2}

PeterH


another one: it reads a data that was not defined from the array.It is actually a very ugly problem: you never know if the data is correct until release some memory and get a correct result.


I can't figure out what you mean by that. Do you have an example?
I only provide help via the forum - please do not contact me for private consultancy.

UKHeliBob

I think it is about time for you to upload the whole script that causes the memory problem.  Perhaps someone else with the same Arduino could try compiling it and report results.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

kapelan

just create a couple of pretty big arrays, initialize them and then display them - you will be surprised.
and include some library and some fonts ...
#include <UTFT.h>
#include <UTouch.h>
#include <Time.h>
#include <avr/pgmspace.h>

PaulS

Quote
just create a couple of pretty big arrays, initialize them and then display them - you will be surprised.

You are assuming that you are accessing the data in progmem correctly. I'm not willing to make that assumption. You need to post your code.

UKHeliBob


just create a couple of pretty big arrays, initialize them and then display them - you will be surprised.
and include some library and some fonts ...
#include <UTFT.h>
#include <UTouch.h>
#include <Time.h>
#include <avr/pgmspace.h>

That is the first mention of those libraries and fonts in this thread. 
If only you had posted your whole code .....
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

AWOL

Code: [Select]
10,40,50,60,2,1}
  ,{10,70,50,170,1,2}

Code: [Select]
1000,4000,500,600,2,1}
  ,{1000,700,500,1700,1,2}


Based on that small sample, you could possibly save memory by using byte variables, and scaling the values.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PeterH


just create a couple of pretty big arrays, initialize them and then display them - you will be surprised.


I don't know what you think will surprise me. If you're hit a problem and want help solving it, you need to actually show us the problem.
I only provide help via the forum - please do not contact me for private consultancy.

kapelan

sorry, that is right: the code to reproduce the problem has to be posted.
Unfortunately the code is not with me today,
I've seen a some other posts, people complain about getting from array unexpected results, so thought it is well known.
will post the code tomorrow.

kapelan

Thanks everybody.
The problem was: trying to get non existing element from array the result will be unpredictable.
Here is the test case:

Code: [Select]

/*

*/

#include <UTFT.h>
#include <UTouch.h>
#include <Time.h>
#include <avr/pgmspace.h>

// Uncomment the next two lines for the Arduino Mega
UTFT        myGLCD(ITDB32S, 38,39,40,41);   
UTouch      myTouch(6,5,4,3,2);

// Declare which fonts we will be using
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];
extern uint8_t SmallFont[];


int x,y,x1,x2,y1,y2;


prog_int16_t Array_02[][7] PROGMEM =
{
  {11,12,13,14,15,16}
  ,{12,70,50,170,1,2}
  ,{13,180,50,210,3,3}
  ,{14,40,120,60,2,1}
  ,{15,70,120,170,1,2}
  ,{16,180,120,210,3,3}
  ,{17,40,200,60,2,1}
  ,{18,70,200,170,1,2}
  ,{19,180,200,210,3,3}
  ,{20,40,280,60,2,1}
  ,{21,70,290,170,1,2}
  ,{22,180,320,210,3,3}
  ,{23,180,200,210,3,3}
  ,{24,40,250,60,2,1}
  ,{25,70,500,170,1,2}
  ,{26,180,250,210,3,3}
  ,{27,180,250,210,3,3}
  ,{28,180,250,210,3,3}
  ,{29,180,250,210,3,3}
  ,{30,180,250,210,3,3}
  ,{31,180,250,210,3,3}
  ,{32,180,250,210,3,3}
  ,{33,180,250,210,3,3}
  ,{34,180,250,210,3,3}
  ,{35,180,250,210,3,3}
  ,{36,180,250,210,3,3}
  ,{37,180,250,210,3,3}
  ,{38,180,250,210,3,3}
  ,{39,180,250,210,3,3}
  ,{40,180,250,210,3,3}
  ,{41,180,250,210,3,3}
  ,{42,180,250,210,3,3}
  ,{43,180,250,210,3,3}
  ,{44,180,250,210,3,3}
  ,{45,180,250,210,3,3}
  ,{46,180,250,210,3,3}
  ,{47,180,250,210,3,3}
};

prog_int16_t Array_01 [][7] PROGMEM =
{
   {111,40,35,200,1,1}
  ,{112,40,75,200,1,2}
  ,{113,40,115,200,1,2}
  ,{114,40,155,200,1,2}
  ,{115,40,195,200,1,2}
  ,{116,40,235,200,1,2}
  ,{117,40,275,200,1,2}
  ,{118,40,315,200,1,2}
   ,{119,40,35,200,1,1}
  ,{120,40,75,200,1,2}
  ,{121,40,115,200,1,2}
  ,{122,40,155,200,1,2}
  ,{123,40,195,200,1,2}
  ,{124,40,235,200,1,2}
  ,{125,40,275,200,1,2}
  ,{126,40,315,200,1,2}
   ,{127,40,35,200,1,1}
  ,{128,40,75,200,1,2}
  ,{129,40,115,200,1,2}
  ,{130,40,155,200,1,2}
  ,{131,40,195,200,1,2}
  ,{132,40,235,200,1,2}
  ,{133,40,275,200,1,2}
  ,{134,40,315,200,1,2}
   ,{135,40,35,200,1,1}
  ,{136,40,75,200,1,2}
  ,{137,40,115,200,1,2}
  ,{138,40,155,200,1,2}
  ,{139,40,195,200,1,2}
  ,{140,40,235,200,1,2}
  ,{141,40,275,200,1,2}
  ,{142,40,315,200,1,2}
   ,{143,40,35,200,1,1}
  ,{144,40,75,200,1,2}
  ,{145,40,115,200,1,2}
  ,{146,40,155,200,1,2}
  ,{147,40,195,200,1,2}
  ,{148,40,235,200,1,2}
  ,{149,40,275,200,1,2}
  ,{150,40,315,200,1,2}
};

String ActiveScreen ="Array_01";
int ArrayMemory_03[30][7] ;
int Array_Test[7] ;



void setup()
{
  // Initial setup
  myGLCD.InitLCD();
//  myGLCD.clrScr();
  myGLCD.fillScr(159,182,205);

  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);

  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(0, 0, 0);

//Time & Alarm:
  setTime(18,29,0,1,1,11); // set time to Saturday 18:29:00 Jan 1 2011

Object_SetStatus();
  GetArray ("Array_01",50,Array_Test);
}

void  loop(){ 
  delay(100);
  if (myTouch.dataAvailable())
      {
        myTouch.read();
        x=myTouch.getX();
        y=myTouch.getY();

        myGLCD.print("              ",1,5);
        myGLCD.print("              ",1,20);
        myGLCD.print("x:"+String(x),1,5);
        myGLCD.print("y:"+String(y),1,20);
        int ObjectDetails[7];
        if ( ActiveScreen =="Array_01")
          {
           GetArray ("Array_02",50,Array_Test)  ;
          }
         else if (ActiveScreen=="Array_02"){GetArrayMem();}
         else {GetArray ("Array_01",50,Array_Test);}
      }
}


void GetArray (String ScreenName,int s,int ObjectDetails[]) 
{

  myGLCD.setFont(SmallFont);

    int y1 =20;
        for (char i=0;i<s;i++)
         {
           y1=y1+20;
           if (y1>200){y1=20; myGLCD.fillScr(VGA_NAVY);}
            if (ScreenName == "Array_02")
            {
              ObjectDetails[0]=i;
              ObjectDetails[1]=pgm_read_word(&(Array_02[i][0]));
              ObjectDetails[2]=pgm_read_word(&(Array_02[i][1]));
              ObjectDetails[3]=pgm_read_word(&(Array_02[i][2]));
              ObjectDetails[4]=pgm_read_word(&(Array_02[i][3]));
              ObjectDetails[5]=pgm_read_word(&(Array_02[i][4]));
              ObjectDetails[6]=pgm_read_word(&(Array_02[i][5])); 
            }
            if (ScreenName == "Array_01")
            {
              ObjectDetails[0]=i;
              ObjectDetails[1]=pgm_read_word(&(Array_01[i][0]));
              ObjectDetails[2]=pgm_read_word(&(Array_01[i][1]));
              ObjectDetails[3]=pgm_read_word(&(Array_01[i][2]));
              ObjectDetails[4]=pgm_read_word(&(Array_01[i][3]));
              ObjectDetails[5]=pgm_read_word(&(Array_01[i][4]));
              ObjectDetails[6]=pgm_read_word(&(Array_01[i][5])); 
            }
        myGLCD.printNumI(ObjectDetails[0],0,y1);   
        myGLCD.printNumI(ObjectDetails[1],40,y1);   
        myGLCD.printNumI(ObjectDetails[2],80,y1);   
        myGLCD.printNumI(ObjectDetails[3],120,y1);   
        myGLCD.printNumI(ObjectDetails[4],160,y1);   
        myGLCD.printNumI(ObjectDetails[5],200,y1);   
        myGLCD.printNumI(ObjectDetails[6],240,y1);
        delay(1000);
         }
  //switch the screen to dispaly another array: 
  if (ActiveScreen == "Array_02") {ActiveScreen="Array_01";}
     else  {ActiveScreen="Array_02";}   
}



void Object_SetStatus() //just initial setup to use some memory
{
//this code for test reason
// later should be updated to get real values from an array
  for (int i=0;i<30;i++)
  {
    ArrayMemory_03[i][0]=i;
    ArrayMemory_03[i][1]=i*30;
    ArrayMemory_03[i][2]=i*40;
    ArrayMemory_03[i][3]=i*50;
  }

}

void GetArrayMem () 
{
         int ObjectDetails[7];
ActiveScreen = "Array_01";
      int y1 =20;
        for (int i=0;i<32;i++)
         {
           y1=y1+20;
           if (y1>200){y1=20; myGLCD.fillScr(VGA_NAVY);}
              ObjectDetails[0]=i;
              ObjectDetails[1]=ArrayMemory_03[i][0];
              ObjectDetails[2]=ArrayMemory_03[i][1];
              ObjectDetails[3]=ArrayMemory_03[i][2];
              ObjectDetails[4]=ArrayMemory_03[i][3];
        myGLCD.printNumI(ObjectDetails[0],0,y1);   
        myGLCD.printNumI(ObjectDetails[1],40,y1);   
        myGLCD.printNumI(ObjectDetails[2],80,y1);   
        myGLCD.printNumI(ObjectDetails[3],120,y1);   
//       myGLCD.printNumI(ObjectDetails[4],160,y1);   
   //     myGLCD.printNumI(ObjectDetails[5],200,y1);   
     //   myGLCD.printNumI(ObjectDetails[6],240,y1);
        delay(1000);
         }

}

PeterH


Here is the test case:


It doesn't look like a test case to me. I would expect a test case to show the problem (whatever it is) in the simplest way possible. That clearly isn't as simple as possible. Do you really mean that it is a sketch with a problem? If so, what's the problem?
I only provide help via the forum - please do not contact me for private consultancy.

kapelan


what's the problem?

the problem was when trying to get non existed data out of array dimension,example:
int Array[2] ={1,2};
myGLCD.printNumI (Array[3],10,10);
result is something weird.
The provided code is a proof.

liudr

I've been following your post hoping to learning a thing or two with the PROGMEM discussion but that never happened (seemed to me your syntax was correct). So if you have Array[] as an array of 2 elements, then why are you trying to access its 4th element? What do you want to achieve by doing that?

Go Up