[Solved] Trouble with 2dim array - 32xLED-matrix

Hi,

I’ve bought a 32x32 RGB-LED-matrix and I’m using the Adafruit libraries for it.
The example codes works perfectly and the I’ve made some basic stuff myself with it.

My problem is that I want my Arduino to iterate trough a 2dim array and for each value different from zero it should draw a pixel.
This does not work properly and also if I tell it to send me the array over serial I get something weird.

I have tried different workarounds and the code I have at the moment should draw a pixel for each zero in my array and nothing happens.
Also, it works just fine with a 1dim array.

#include <gamma.h>
#include <RGBmatrixPanel.h>

#include <Adafruit_GFX.h>



#define CLK 11  // MUST be on PORTB!
#define OE  9
#define LAT 10
#define A   A0
#define B   A1
#define C   A2
#define D   A3

RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false);
int X = 16; //cursor values
int Y = 16;

int Xb = 0; //cursor buffervalues
int Yb = 0;

int xa = 0; //coordinates from array
int ya = 0;

byte matval = 0;

byte matrixarr[32][32] = { //Array representing the Matrix
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};

void setup()
{
  Serial.begin(9600);
  matrix.begin();
  matrix.drawPixel(3, 15, matrix.Color333(1, 0, 0));
}

void loop()
{
      for (ya = 0; ya < 32; ya++)
      {
        //delay(10);
        for (xa = 0; xa < 32; xa++)
        {
        matval = matrixarr[ya][xa];
        Serial.print(matval);
        pindraw();
        }
                Serial.println();
      }
 

}

void pindraw()
{
    if (matval == 0)
      {
        matrix.drawPixel(xa, ya, matrix.Color333(1, 0, 0));
      }
    else
      {
        matrix.drawPixel(xa, ya, matrix.Color333(1, 1, 0));
      }
}

Any suggestions? The serial print of the array looks like this: And I don’t understand where the 1 at the end comes from?

00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
0000000000000000000000000000
0001

Which arduino are you using? My first guess here would be that you're out of RAM.

Hello and welcome,

I too think it may be memory problem. Are you sure you have enough memory to store an array of 1KB ?

See this post for how to greatly reducing memory usage with this kind of arrays. You can reduce it to 128 bytes.

Your matrix would fit beautifully in an array of 32 longs if you used the bits as values rather than bytes.

Ok, that could be the problem. I'm using an Uno. I didn't get any out of memory warnings and when uploading it states "Estimated used SRAM memory: 1,341 bytes (of a 2048 byte maximum)"

I'll check it out thank you.

Ok, so I’ve changed my code a bit.
Also I made an array out of the long-datatype and used the bitRead-function.
Works fine when I have no values in the array, but when I try to fill it with values I quickly get this error:
“integer constant is too large for ‘long’ type” - This makes sense, but is there any workaround of this somehow?

Here’s my code:

#include <gamma.h>
#include <RGBmatrixPanel.h>

#include <Adafruit_GFX.h>



#define CLK 11  // MUST be on PORTB!
#define OE  9
#define LAT 10
#define A   A0
#define B   A1
#define C   A2
#define D   A3

RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false);
int X = 16; //cursor values
int Y = 16;

int Xb = 0; //cursor buffervalues
int Yb = 0;

int row = 0; //coordinates from array
int column = 0;

unsigned long matrixarr[] = { //Array representing the Matrix
00000000000000000100010000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
00000000000000000000000000000000,
};

void setup()
{
  Serial.begin(9600);
  matrix.begin();
  matrix.drawPixel(3, 15, matrix.Color333(1, 0, 0));


 
}

void loop()
{

  
if (Xb != X || Yb != Y) //if cursor moves execute code
  {
    
    for(row = 0; row < 32; row++)
    {
      for(column = 0; column < 32; column++)
      {
        boolean pixel = bitRead(matrixarr[row],column);
//        Serial.print(row, column);
        if(pixel == 1)
        {
//         matrix.drawPixel(row, column, matrix.Color333(1, 0, 0));
//        }
//        else
//        {
        matrix.drawPixel(row, column, matrix.Color333(0, 1, 1));
        }
      }
      Serial.println();
    }


      matrix.drawLine(Y, 0, Y, 31, matrix.Color444(1, 1, 0));
      matrix.drawLine(0, X, 31, X, matrix.Color444(1, 1, 0));
//      if (matrixarr[Y][X] > 0)
//        {
          matrix.drawPixel(X, Y, matrix.Color444(5, 1, 0)); 
//        }
//        else
//        {
//           matrix.drawPixel(X, Y, matrix.Color333(1, 1, 1));
//        }
    
      Xb = X;
      Yb = Y;  
  }


}

The compiler thinks that your sequence of 1s and 0s is a gigantic number. Try

unsigned long matrixarr[] = { //Array representing the Matrix
0b00000000000000000100010000000000,
etc

Thanks alot, the array worked perfectly with the “0b” syntax.

Now I’ve got to a new problem with my if-sentence.
I’ve debugged it with Serial.print() and found out that the if-sentence works properly, but it won’t give me any output on my led-matrix.

If I remove the if-sentence and only keeps this part “matrix.drawPixel(row, column, matrix.Color333(1, 0, 0));” it fills my matrix with the specified color as it should, so there seems to be something fishy going on…

int X = 16; //cursor values
int Y = 16;

int Xb = 0; //cursor buffervalues
int Yb = 0;

int row = 0; //coordinates from array
int column = 0;


void loop()
{

  
if (Xb != X || Yb != Y) //if cursor moves execute code
  {
    
    for(row = 0; row < 32; row++)
    {
      for(column = 0; column < 32; column++)
      {
        boolean pixel = bitRead(matrixarr[row],column);
        Serial.print(pixel);

        if(pixel == 1)
        {
         matrix.drawPixel(row, column, matrix.Color333(1, 0, 0));
        }
        else
        {
        matrix.drawPixel(row, column, matrix.Color333(0, 1, 1));
        }
      }
    }

    
      Xb = X;
      Yb = Y;  
  }
if (Xb != X || Yb != Y) //if cursor moves execute code

Where do the values for the variables in this line coming from ? I cannot see where they change in the program.

The plan is to add two rotary encoders to change that value. In the end the plan is to control switching between 32 in and output signals and visualize it all on the LED-matrix and update it each time the encoder value is changed.

But at the moment it will only run trough the cursor-part of the code one time...

Could it still be an out-of-RAM error maybe? Estimated SRAM usage is 447 bytes so it should be plenty of RAM left, but I don't know if you can thrust that...

Edit: Definitive out-of-RAM error, I made my array smaller and it worked like a dream. Have to get a Mega or 1284p...