Use of a 2d Array, [solved - project video attached]

I have a grid of 6x6 neopixel LEDs, wired in sequence so that the pixelnumber runs like the squares on a snakes and ladders board...ie bottom row is 0 to 5 left to right, but second row is 6 to 11, right to left.

I want to program some effects for the grid using X and Y coordinates and was looking at a maths formula to convert the pixelnumber to XY and back again.

Then I thought...could I use a 2D array? This is my suggestion for setting up the array;

int p
int X
int Y
int pixelarray [6][6]={
    { 0,  1,  2,  3,  4,  5}, //Y=0
    {11, 10,  9,  8,  7,  6}, //Y=1
    {12, 13, 14, 15, 16, 17}, //Y=2
    {23, 22, 23, 20, 19, 18}, //Y=3
    {24, 25, 26, 27, 28, 29}, //Y=4
    {35, 34, 33, 32, 31, 30}, //Y=5
    };                 
  //X=  0   1   2   3   4   5

Am I in the right starting place? I know the array looks like the origin is top left, but I've wired my neopixels from bottom left so I know that's where the origin is .

If I'm right the value of pixelarray[X][Y] should return the pixelnumber on the strip. I'm unsure how to get the [X} and [Y] from the pixelnumber though...since all the values are unique (0 to 35) is there a way to return the array element numbers (ie, X and Y) from the element with a given pixelnumber?

OR should I just go back to the maths functions to get them ("Y" is quite easy, but "X" is hard because of every other row running backwards!)

Thanks!

The 2D lookup table is fast and easy, and you can arrange the element contents in any way you like.

However, it does not take much effort to come up with a simple formula for the same result. Note that if Y is odd, X is indexed right to left; otherwise left to right.

What do you actually want to do with the (X,Y) coordinates? A clear description will lead to better ideas.

You could make a 2D array (36 x 2) using pixel number as the index: ndx, 0 is 'X' values, ndx, 1 is 'Y' and so on.

Or, a 1-D array of structs each element of which has an X-Coord and Y-Coord member.

They both sound like ideas that would work in the opposite way; getting X and Y from "p"...but might not get "p" from X and Y? I'm hoping to find a single method that works in both directions.

What I want to do quite simply is create patterns. For instance two nested "x" and "y" loops, both stepping up incrementally, can be used to make the LEDs come on in vertical or horizontal rows, depending in what order you nest the loops. But because every other line of my panel of LEDs is reversed, it's harder...yes I could add "if y= "even" then...but it's just another step...and is problematic when y is zero, which doesn't count as either odd or even if I remember correctly.

Horizontal bars is the simplest...but there's things like x2 +y2 = 0 to make circles etc...

Depends what you're going for.

If you want absolute speed and simplicity (and have memory to burn) then look up tables (arrays) seem to be the way to go. One for (x, y) --> p and another for p --> (x, y).

If you're looking to be clever or "elegant" and don't care so much about speed, I imagine you could come up with three functions p = f(x, y) and x = g(p) and y = h(p). But, they'd still be distinct functions.

I'll stick with the array...I just checked what I need to do and I don't think I'll need to calculate p from XY...only the other way around. My equations for the patterns will generate XY and the array will output which pixels need to be lit.

I've done a quick sketch just generating random XY and it seems to work faultlessly, so for now I think I'm done - thanks all for your help...I'll return to this thread for ideas if it turns out I do need more finesse!

I passed 100 posts! No more 5 mins wait! Wheee!

when y is zero, which doesn't count as either odd or even

Zero is even.

And two is prime. Crazy world.

OK, struggling related to above…same array, which this code references;

for (x = 0; x <= 5; y++) {
    for (y = 0; y <= 5; x++) {
      strip.setPixelColor(pixelarray[x][y], RVa, 0, 0);
      strip.show();
      delay(200);
      strip.setPixelColor(pixelarray[x][y], 0, 0, 0);
      strip.show();
    }
  }

What I thought it would do is step through the pixels column by column; in my mind it should set x to 0, then step through y = 0,1,2,3,4,5 lighting each pixel as it goes.

It doesn’t. It lights them up by rows (so y=0, x = 0,1,2,3,4,5 the y = 1 x = 0,1,2,3,4,5.

I’ve even reversed the loops so that y is prior to x and it still lights the LEDs in the same order. I am thus VERY confused, lol.

Suggestions?

Thanks.

Never mind. Fixed it. Schoolboy error that I just couldn't see...

for (x = 0; x <= 5; y++) {
    for (y = 0; y <= 5; x++) {

That is certainly an unusual construction!

Yes, I think it was the fact that it worked at all that prevented me from seeing it for so long.

Video of the project that uses the array…

a bit off topic, but could become important, as the numbers in the 2D array all fit within a uint8_t aka byte

int pixelarray [6][6]={ { 0, 1, 2, 3, 4, 5}, //Y=0 {11, 10, 9, 8, 7, 6}, //Y=1 {12, 13, 14, 15, 16, 17}, //Y=2 {23, 22, 23, 20, 19, 18}, //Y=3 {24, 25, 26, 27, 28, 29}, //Y=4 {35, 34, 33, 32, 31, 30}, //Y=5 };

could be

uint8_t pixelarray [6][6]={ { 0, 1, 2, 3, 4, 5}, //Y=0 {11, 10, 9, 8, 7, 6}, //Y=1 {12, 13, 14, 15, 16, 17}, //Y=2 {23, 22, 23, 20, 19, 18}, //Y=3 {24, 25, 26, 27, 28, 29}, //Y=4 {35, 34, 33, 32, 31, 30}, //Y=5 };

==> saves 36 bytes of RAM

Cheers Rob, not necessary right now but I appreciate the advice for the future.

I only use library commands...I really must look up what that “uint” thing does...I see it a lot here and have no idea what it means...