# Making a long array from smaller arrays

Howdy folks!

I did a lot of searching, and could not find the answer. Perhaps I just didn't know the term to search. Still new to a lot of this.

I am trying to make an array out of smaller arrays. This way I can access all values in an easy 'for' loop and add some flexibility. What I am attempting:

int Arr1 = {0, 1, 2};
int Arr2 = {3, 4, 5};
int Arr3 = {Arr1, Arr2}; or something like that

resulting in:
Arr3 = {0, 1, 2, 3, 4, 5}

Tried a few simple methods, didn't work. Searched and only found people trying to do way more complex things that I am doing.

Try to describe what you actually want to achieve, not how you think you can achieve it.

Also, why do you want to do it. Why not simply define and populate the larger array in the first place?

sterretje:
Try to describe what you actually want to achieve, not how you think you can achieve it.

Sorry, thought I had. Will state is differently.

I have 2 arrays:
array1 is {0,1,2}
array2 is {3,4,5}

I want to make a new array that is all of the values from those two arrays, in order of array1 then array2:
array3 is {0,1,2,3,4,5}

And why I want to do this:

I am setting up LEDS in different zones. Each zone of LEDs is an array, as there are 3 LEDs per zone. I will want to affect different zones of LEDS, and in specific patterns. I will be doing different combos of LED zones for different effects, so can't just define them rigidly. I want flexibility.

Zone1= {0,1,2}
Zone2 = {3,4,5}
....
Zone14 = {39,40,41}

And if I decide I want to create a wave across Zone1, Zone8, and zone12, I want those values all in order in a single array so it is easier to step through in a 'for' loop of the like. And I may be doing a pattern with 2, 4, or 6 zones. So I want flexibility.

Also, I just figure it is a good thing to know how to do for future projects.

You can do what you're asking with memcpy. Given how little RAM most Arduinos have, I wouldn't waste it like this. Have you considered making a 2D array?

Or lists?

Multi-dimensional array?

Iliffe vector

wildbill:
You can do what you're asking with memcpy. Given how little RAM most Arduinos have, I wouldn't waste it like this. Have you considered making a 2D array?

I thought I couldn't even make a 2D array out of predefined 1D arrays.

Granted, now that I think about it, I could define each zone as a row in a 2D array, and define all the zones at once in a single 2D array. Then if I want to control multiple zones, I make a new array as what those zones are, use that control defining array as a method to look up which exact row I am seeking, and go from there.

That might work. I may not have explained it well, but I think I have something.

However, is there still a way to do what I was originally asking? It may be useful to know for future projects. If the answer is "no" then that is fine, it just seems like a simple operation (granted I don't know enough about how code acts to know if it is actually complicated).

magic_smoke:
I thought I couldn't even make a 2D array out of predefined 1D arrays.

In the small memory of an Arduino you should not be thinking of making arrays at runtime - there is a too great risk that the Arduino will run out of memory and crash. You should define all the arrays when you write the program.

...R

Robin2:
In the small memory of an Arduino you should not be thinking of making arrays at runtime - there is a too great risk that the Arduino will run out of memory and crash. You should define all the arrays when you write the program.

...R

Ah, gotcha. So doing such computations in just a memory hog. Drat, was hoping for a slick solution, but will go with a some predefined ways. Will go with the 2D and making in code call-outs to specific rows.

This might be a good use for macros:

``````#define Zone1 0,1,2
#define Zone2 3,4,5
....
#define Zone14 39,40,41

byte PatternA[] = {Zone1, Zone2, Zone14};
``````

The last line compiles as:

``````byte PatternA[] = {0,1,2, 3,4,5, 39,40,41};
``````

You could also use 2D arrays for your patterns:

``````#define Zone1 {0,1,2}
#define Zone2 {3,4,5}
....
#define Zone14 {39,40,41}

byte PatternA[][3] = {Zone1, Zone2, Zone14};
``````

That expands to:

``````byte PatternA[][3] = {{0,1,2}, {3,4,5}, {39,40,41}};
``````

johnwasser:
This might be a good use for macros:
......

You could also use 2D arrays for your patterns:
....

Awesome! This is what I was looking for! Will play with this and see what I get, thanks.