0

I am trying to find a "vanilla" way of writing several arrays into one. I spent a lot of time searching for how to do this but everything that comes up is either a tutorial that doesn't cover concatenation or someones question that ended up being an unrelated syntax problem.

I put several strands of WS2812B LEDs wired in series so the Arduino sees them as one continuous strand. I have been able to run some very cool patterns as one strand using the Pololu library but I would like to program the strands separately. The number of lights I have is very resource intensive and I like to keep things simple so I'm looking for advice on the best way to go about combining arrays without importing more libraries.

Because the patterns I have used so far write to an array and the output is an array, my first thought was that I could write one pattern to an array made up of sections 1, 4, and 5, and another pattern that would be mapped to sections 2, 3, and 6 and then concatenate it all together in order. However, adding arrays seems to be somewhat complicated without worrying about this or that persons favorite library or looping through one value at a time and I am trying to work on my C skills. Is there a simple way to concatenate arrays or a better way for me to think about this? Any best practice or organizational advice is welcome. I want to write clean code.

Bonus Round: I use several different languages at work and I cant figure out why I have it in my head that this would work...

array1 = [a, b, c]
array2 = [x, y]
array3 = array1 + array2

// array3 = [a, b, c, x, y]

Any idea if that is possible or what language I am thinking of?

  • A 2 dimensional array is technically saved serialized as a long 1 dimensional array. It should be possible to read/write each subarray and also the whole array with 1 dimension using pointer. Maybe I have time to write a whole answer to this later this day. – chrisl Dec 3 '18 at 10:12
4

C(++) does not natively let you "add" arrays like that. Your code looks like maybe Python.

There are several ways to do what you want to do. You could make your code which writes the pattern to the array accept an offset - so rather than always starting at position 0, you could tell it where to start.

You could write a function to "concatenate" arrays, or use one someone else has written, if you can find one. It's pretty trivial to do.

However the method I'd use in your situation, probably just a matter of preference really, is this:

Let's say (for example) you have 6 strips of 10 LEDs each. You need 60 elements to represent them all. (I'm assuming each element is an int; I can't remember if it is.) Create one big array for the whole lot:

int allElements[60];

You might think of the variable allElements as an array, but it is really an int* (a pointer to an int), with that memory address happening to have 59 other memory addresses after it reserved. C knows that when you say allElements[7] it needs to looks 7 ints on from the address pointed to by allElements. What this means is that you can create new variables which point to parts of that array, and use those variables as if they were their own arrays. For example:

int *firstStrip = &(allElements[0]);
int *secondStrip = &(allElements[10]);
int *thirdStrip = &(allElements[20]);
/* ... and so on */

(There is a more idiomatic way to write this which is exactly equivalent, but I think the above is more explicit if you're new to it.)

As an extra improvement you might now notice that you have a load of similarly-named variables with similar functions -- and you might want to put them into an array.

int* strips[] = { &(allElements[0]), &(allElements[1]), &(allElements[2]) /* and so on */};

Now you can use strips[3] to give you the fourth strip.

There are several other ways too, but I think this will get you going.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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