LEDs wrongly lit in matrix - incorrect allocation?

I wrote the following code for displaying a bitmap (small size) on a LED matrix (formed using APA102 led strip):

//for 24-bit bitmaps

// the header files for the RGB data

 #include <FastLED.h>
 #include "redarray.h"   
 #include "greenarray.h"   //g data header file (hex or dec)
 #include "bluearray.h"   //b data header file (hex or dec)*/

//matrix parameters

#define MatrixWidth 36 //columns
#define MatrixHeight 7 //rows
#define numled (MatrixWidth * MatrixHeight)

//pin details

#define datapin 7

#define clockpin 8

CRGB leds[MatrixWidth * MatrixHeight];

const bool  MatrixSerpentineLayout = true;


 void setup() {

  FastLED.addLeds<APA102, datapin, clockpin>(leds,numled);
  FastLED.clear();
   }

void loop() {


  for(uint8_t x=0 ; x<MatrixWidth ; x++){

    for(uint8_t y=0; y < 3; y++){

   leds[XY (x, y)].setRGB(redarray[x][y],greenarray[x][y],bluearray[x] [y]);

    FastLED.show();

    delay(1);    
  }
  }
 }

  uint16_t XY( uint8_t x, uint8_t y)   //the matrix coordinate system starts    from (0,0) - i.e. x=0:matrixwidth and y=0:matrixheight
 {
   uint16_t i;

     if( MatrixSerpentineLayout == true) {
      if( y & 0x01) {
       // Odd rows run backwards
       uint8_t reverseX = (MatrixWidth - 1) - x;
       i = (y * MatrixWidth) + reverseX;
     } else {
      // Even rows run forwards
       i = (y * MatrixWidth) + x;
  }
  }

  return i;
}

The following is the data in one of the header files - its of same type for the other two colors.

const unsigned char redarray[36][3] PROGMEM = { 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0xff, 0xff, 0xff, 0xff, 0xff, 0x20, 0x20, 0x20, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x20, 0x20, 0x20, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x20, 0x20, 0x20, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, };

The matrix is in a serpentine form (evident from the XY function in code). I am using an Arduino Due. The problem is that the code compiles and runs...but the bitmap is incorrect. Although I am sure, I am somewhat along the correct path because some LEDs do light up as they should as per the bitmap. But most of them are wrongly lit.

Could you somebody plz guide where I am going wrong with the coding? I fear there is some mistake in reading the color data from array and allocating it to leds[XY(x,y)].I do not know how a 2D array is read or dealt with by Arduino, I asked someone "experienced" with Arduino and he told me to declare it like I have done in the header file...

Thanks!

It may help you to organise your data better if you declare a structure that contains the rgb values as separate fields/variables. Then you can have a one dimensional array of structures and it is more obvious what is going onas you don't have data split over 3 files.

Look it up if you have not done this before, it is a useful thing to have up your sleeve and simplifies many aspects of data grouping and management.

A taste of what it looks like:

struct RGB_t 
{
  uint8_t r, g, b;
};

RGB_t rgbTable[36] = { {0, 0, 0}, {255, 255, 255 }, etc for 36 values };

...

for (uint8_t i=0; i<36; i++)
  doSomething(rgbTable[i].r, rgbTable[i].g, rgbTable[i].b);

@ Marco..thanks for replying.

The problem is that..the color data generated is so large that its difficult to manually declar like you have done. I extracted this data using Matlab and imported it and its in the form I have written in my first post.

Isn't there a way to put these brackets around individual elements? And I will try with struct.

Ok.

Couple of suggestions:

  1. You can group the data by letting the Arduino do the work for you. Simply write a sketch, including your header file(s), and write the data out using Serial.print(), in the format required, with brackets, etc. Then cut and paste from the serial monitor to your current sketch. Less work to write the sketch with a couple of loops than it is to edit the data manually, especially in bulk.

  2. I don't understand how you are indexing in the current code. The definition of the CRGB array uses (MatrixWidth * MatrixHeight) or (36*7), so the arrays need to be height and width to match. You have not used these definitions and have MatrixWidth and the magic number 3 instead. This will automatically result in a mismatch in the mapping between CRGB and your arrays, unless I have missed something.

Either you have (WidthHeight) elements in each colour array, or you have (WidthHeight)*3 (for RGB) in a single array (or the array of struct, which is the same thing). Arrays of [36][3] don't logically map to anything that I can see.