0

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)   
 {
   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.

Thanks!

4
  • Your code is hard to read. Please try to indent is better.
    – st2000
    May 10, 2016 at 1:03
  • Why is your LED matrix 36 by 7 but your array is 36 by 3?
    – st2000
    May 10, 2016 at 1:11
  • @st2000 Insuffcient power supply. On taking all LEDs into use, it doesnt work, I am working on this power supply thing but for testing took 3 rows of 36 LEDs each. sorry for the improper code. Will edit it. May 10, 2016 at 6:42
  • It might help if you created a test array that only lit 1 LED. Then walked that LED through all positions. Knowing when the lit LED does not appear in the expected location may provide hints as to where to look for problems in the array or your code.
    – st2000
    May 10, 2016 at 12:40

1 Answer 1

1

Initialize your arrays like this:

int a[3][4] = {  
   {0, 1, 2, 3} ,   /*  initializers for row indexed by 0 */
   {4, 5, 6, 7} ,   /*  initializers for row indexed by 1 */
   {8, 9, 10, 11}   /*  initializers for row indexed by 2 */
};

...you can read more about it here.

Added later...

The following explanation came from here:

Arrays that are not explicitly divided into rows:

int M[4][5] = {10, 5, -3, 9, 0, 0, 32, 20, 1, 0, 0, 8};

...are filled like this:

int M[4][5] =
{
  {10,  5, -3, 9, 0},
  { 0, 32, 20, 1, 0},
  { 0,  8,  0, 0, 0},
  { 0,  0,  0, 0, 0}
};

As only you understand your data you will have to verify if your data is populating the array as you expect.

2
  • the problem is that - the color data I generated is not in the format arduino wants 2D array to be declared. Its tedious to put everything in brackets like this and I somehow realised that this is not the way c++ reads arrays. I can just declare the array like charlie[] [] = { }; With no individual bracketing on the elements and it reads it correctly. And also when I did like you told me, I am getting this error - expected unqualified-id before .... and it took me 20 minutes to arrange data like this.....Is the reading of the color data and consequent allocation to LEDs correct???? May 10, 2016 at 6:45
  • ..Thanks bro..As per your links of declaring an array, I managed to get it working (after resolving some array indexing problem - here it is - arduino.stackexchange.com/questions/24103/… - I got it working now...the pattern is displayed....Thanks for links :) May 11, 2016 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.