Problems making a pattern on 8x8 Neopixel grid

Hello all! This is my first post here and I'm looking for some help getting a pattern over to my neopixel grid (WS2812B).

I've stored the pattern as a 2D array using bits (I know they are actually integers but I'm not aware of a good way of using bits in c++ yet).

My code loops through the array and assigns the leds to be either on or off in that position.

The problem is, when I upload the .ino and connect the 8x8, the pattern looks like this:

#, #, #, #, #, #, #, #
#, #, #, 0, #, 0, #, #
#, 0, #, 0, #, #, 0, 0
#, #, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0
#, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0

And here is the code:

#define data 6
#define numLED 64

/*
heart pattern as array:
0, #, #, 0, 0, #, #, 0
#, #, #, #, #, #, #, #
#, #, #, #, #, #, #, #
#, #, #, #, #, #, #, #
0, #, #, #, #, #, #, 0
0, 0, #, #, #, #, 0, 0
0, 0, 0, #, #, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0
*/

int ledPattern[8][8] = {{0, 1, 1, 0, 0, 1, 1, 0}, {1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1}, {0, 1, 1, 1, 1, 1, 1, 0}, {0, 0, 1, 1, 1, 1, 0, 0}, {0, 0, 0, 1, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}};

CRGB leds[numLED];

void setup() {
  pinMode(data, OUTPUT);

  //setup leds
  FastLED.addLeds<NEOPIXEL, data>(leds, numLED);

  //cycle through array
  for(int x = 0; x < 9; x++){
    for(int y = 0; y < 9; y++){
      if(ledPattern[x][y] == 1){
        leds[x*y] = CRGB(5, 0, 0); //if there's a 1, set LED to red
      }
    }
  }
  //turn on LED's
  FastLED.show();
}

Any help would be appriceiated, Thanks!
PS: Board is Arduino UNO

#define numLED 64
..


int ledPattern[8][8] = {{0....

CRGB leds[numLED];....

...
for(int x = 0; x < 9; x++){
    for(int y = 0; y < 9; y++){

Oops

Sorry I don't get it

Iterating 81 times over a 64 element array is not going to end well.

leds[x*y] = Even when you get the indices correct, that likely isn't going to do what you hope for.

Oh I see the problem now, if I do this:

for(int x = 1; x < 9; x++){
    for(int y = 1; y < 9; y++){

That'll fix the 81 iterations so now it's 64.

Any ideas on addressing it proerly though? It still doesn't seem consistent up to 64

for(int x = 0; x < 8; x++)

Same for y. Arrays are 0 indexed.

/*

*/

byte ledPattern[8] = { 0b01100110,
                      0b11111111,
                      0b11111111,
                      0b11111111,
                      0b01111110,
                      0b00111100,
                      0b00011000,
                      0b00000000
                    };

void setup()
{
  Serial.begin(115200);

  //cycle through array
  for (byte x = 0; x < 8; x++)
  {
    byte mask = 1;
    for (byte y = 0; y < 8; y++)
    {
      if (ledPattern[x] & mask)
      {
        //leds[x*y] = CRGB(5, 0, 0); //if there's a 1, set LED to red
        Serial.print("#");
      }
      else
      {
        Serial.print(" ");
      }
      mask *= 2;
    }
    Serial.println();
  }
}

void loop()
{
 }

Save your memory, the above does the same, just outputs to the serial monitor, you can chage it easily to use your LED matrix.

niall895:
Oh I see the problem now, if I do this:

for(int x = 1; x < 9; x++){
for(int y = 1; y < 9; y++){



That'll fix the 81 iterations so now it's 64.

Any ideas on addressing it proerly though? It still doesn't seem consistent up to 64

It's 64 iterations, sure, but still accessing outside of array boundaries.

C/C++ lets you shoot yourself in the foot like that.

leds[x*y] =

Imagine that x = 2, and y = 3.
Now, imagine that x = 3, and y = 2.

See the problem?

I do, and I think I've got the solution. If I add a count variable that I can increment every iteration I can just use that to set the array of leds, right?

How come it's still out of boundaries?

missdrew:

/*

*/

byte ledPattern[8] = { 0b01100110,
                     0b11111111,
                     0b11111111,
                     0b11111111,
                     0b01111110,
                     0b00111100,
                     0b00011000,
                     0b00000000
                   };

void setup()
{
 Serial.begin(115200);

//cycle through array
 for (byte x = 0; x < 8; x++)
 {
   byte mask = 1;
   for (byte y = 0; y < 8; y++)
   {
     if (ledPattern[x] & mask)
     {
       //leds[x*y] = CRGB(5, 0, 0); //if there's a 1, set LED to red
       Serial.print("#");
     }
     else
     {
       Serial.print(" ");
     }
     mask *= 2;
   }
   Serial.println();
 }
}

void loop()
{
}



Save your memory, the above does the same, just outputs to the serial monitor, you can chage it easily to use your LED matrix.

Thanks missdrew, once I've got it working I'll change it to that

Stepping 1 to 8 inside another loop stepping 1 to 8 is going to generate values in the range 1 to 64.
Your array indices run 0 to 63.

Yes, you could add a counter, or you could do some simple arithmetic (multiplication by a constant, and addition) on the values of y and x.

Like so?

leds[x*8+y]

niall895:
Like so?

leds[x*8+y]

Yes, you have just done what the compiler does in the background when you say

leds[x][y]

That pattern loading on the matrix is the best thing I've seen all day. Thanks for the help everyone! :smiley:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.