Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is most likely no translated for the post!





Share:
Notifications
Clear all

Arrays


(@ac5ff)
New Member
Joined: 4 years ago
Posts: 1
Topic starter  

First off, I have been reading/watching YouTube/etc etc etc for a while now.  Not sure why, but today was the first time I have run across your website.  I've already bookmarked a dozen pages I think! HAHA!    Thank you SO much for all this information.  You've put things together in a way that makes things easy to understand and has cleared up a TON of questions I have had.

I am looking forward to getting my LEDs hooked up to my Arduino and playing around with some of the code you've layed out here.

I've tried to browse around some but have not located a good walkthrough on arrays.  I would like to set up a 12 x 40 array.  I've seen software that will aid in setup/programming/etc, but those mainly use a seperate controller (i.e. T1000s).  I would like to see if I can do the same thing with the Arduino.

Is there anything on your site using an Arduino to control an array like this?

Thank You


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1767
 

Hi Ac5ff,

sorry for the late reply, and thank you so much for the very nice compliments! Definitely a motivator to keep going ...

I have not done much with arrays and LEDs, but maybe I can help you get started.
What are you trying to accomplish with the 12x40 LED array?

If you'd like to address individual LEDs, then this most certainly is possible.
I'd think about something like this:

1) Use on long LED strand of 240 LEDs (12x40).
2) Cut it in pieces of 40 LEDs, so we get 12 strands.
3) Connect the end of strand 1 to the beginning of strand 2, the end of strand 2 to the beginning of strand 3, etc ...
4) Now connect this "reformatted"strand to your Arduino, so we can do a basic test if all LEDs work, see also this article.
5) We define a 12x40 array in our sketch, so it helps us "translate" the array coordinates to actual LED positions.

Keep in mind, that we in essence still use one large strand of LEDs, so the positions are 0, 1, 2, 3, ... ,238, 239.
(we start counting at zero so the last LED will be number 239)

Next we need to setup an array by defining it. Since we use an X and Y position on our array, we would need to define a multi-dimensional array (see also this article).

int LedArray[12,40];

Now we need to add our "translation" to the table, which we could enter manually of have our Arduino for it for us, since it's a lot of manual work.
Either one by one (inefficient) in the setup() function:

LedArray[0][0] = 0;
LedArray[0][1] = 1;
LedArray[0][2] = 2;
...
LedArray[0][8] = 8;
LedArray[0][9] = 9;
LedArray[0][10] = 10;
LedArray[0][11] = 11;
LedArray[1][0] = 12;
LedArray[1][1] = 13;
LedArray[1][2] = 14;
LedArray[1][3] = 14;
...
// etc

or right away define it as (before the void setup() function):

int LedArray[12][40] = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 },
                         { 12, 13, 14, 15, 16 ,17, 18, 19, 20, 21, 22, 23 }, ....
                         { 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239 } };

Either way is taking up a lot of manual typing work ...
So I'd do that in the "void setup()" function with two for-loops that count X and Y:

void setup() {
  int counter = 0;
  for(int x=0; x<12; x++) {
    for(int y=0; y<40; y++) {
      LedArray[x][y] = counter;
      counter++;
    }
  } 
}

So now we should have an array, filled with the correct positions.

For example if we want the LED at (5,12) then this would be the (4x40) + 12 - 1 = 171th LED in our strand. 
Keep in mind; X = 5 means that we have 4 full 40 LED columns, and the for the 5th column we only use 12 of the LEDs down.
Since we start counting with zero, we need to subtract 1 from the total.
(I hope I calculated that right hahaha)

After having done this (not sure how fast the Arduino would do this, but I'm pretty sure very fast), we can now set LEDs individually.
For example with NeoPixel, to set the pixel at X=5 and Y=12:

without array:

strip.setPixelColor(171, strip.Color(255, 0, 0)); // we calculated the position

or

strip.setPixelColor( ( (x-1)*40 ) + y - 1, strip.Color(255, 0, 0)); // the Arduino calculates the position

or with the array where "LedArray[5][12]" holds the number 171 (I hope):

strip.setPixelColor(LedArray[5][12], strip.Color(255, 0, 0)); // we pull the number from our array

Don't forget to make the LED visible with:

strip.show();

As you can see, the calculation of the LED position is not that hard, so an array is not really needed.
We could even glue that into a function (to keep things readable - more info on functions here):

int GetLEDPosition(int x, y) {
  return ( (x-1)*40 ) + y - 1;
}

So we could do the following and not even need an array:

strip.setPixelColor(GetLEDPosition(5,12), strip.Color(255, 0, 0));

Is this what you're looking for?
Note that I haven't had a chance to any of the code, so I could have made a typo or calculation mistake - it's still early in the morning here, and I could use some more cofffee hahah ... 

p.s. the numbers 12 and 40 I'd normally would make constants with a define (see this article). Something like:

#define maxX 12
#define maxY 40

And replace all occurrences of 12 with "maxX" and all occurrences of 40 with "maxY" - which would make it easier in the future ro change the dimensions of your array if you'd need to.


ReplyQuote

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: