## array manipulation

6 posts / 0 new
Author
Message

I have two arrays.

One is an incoming data stream stored as a single dimensional array.

```for (uint16_t i = 0; i < 300; i++) SingleArray[i]= 0;
// turn all Red LEDs ON
for (uint16_t i = 0; i < 300; i+=3) SingleArray[i]= 20;```

The Other is a multidimensional array for RGB lighting
as used by Alan Burlison in his WS2811 routine:

```typedef struct __attribute__ ((__packed__)) {
uint8_t g;
uint8_t r;
uint8_t b;
} RGB_t;
```

I want to set all the red LEDs to a known value:

```// Create RGB Arrays using the RGB_T structure
RGB_t rgb1[50];

for (int j = 0; j < 50; j++) {
rgb1[j].r = SingleArray[j];
rgb1[j].g = SingleArray[j+1];
rgb1[j].b = SingleArray[j+2];
}```

The single array is 3 times as big as the RGB array.
What I am trying to do is Set rgb1[j].r = 20 while rgb1[j].g and rgb1[j].b remain set to 0.

the results of the above is:
Pixel1 red
Pixel2 Green
Pixel3 Blue
Pixel4 red
Pixel5 Green
Pixel6 Blue
Pixel7 red
Pixel8 Green
Pixel9 Blue
etc

If I use

```for (int j = 0; j < 50; j++) {
rgb1[j].r = 20;
rgb1[j].g = 0;
rgb1[j].b = 0;
}
```

I get the result I want.
So where am i going wrong? or what is a better way of achieving the same result?
Any help will be greatly appreciated, if only so I can get some restful sleep.

Quote:
The Other is a multidimensional array
To be pedantic, no it isn't. It is an array of structs. But in any case what you want is:

```for (int j = 0; j < 50; j++) {
rgb1[j].r = SingleArray[3 * j];
rgb1[j].g = SingleArray[(3 * j) + 1];
rgb1[j].b = SingleArray[(3 * j) + 2];
}```

Regards,
Steve A.

The Board helps those that help themselves.

Steve, thanks I am up and going.

Koshchi wrote:
To be pedantic, no it isn't.

I don't think that's pedantic at all.

A multidimensional array is one in which each element of the array is itself an array - so individual items would be accessed as

`array[x][y]`

A struct is not an array - so an array of structs is not a multidimensional array

`array[x].y`

Top Tips:

1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...

But the struct as written is just three uint8_t's, therefore you could treat it as an array:

```RGB_t color;
uint8_t *pStruct = &color;
uint8_t red = pStruct[0];
uint8_t green = pStruct[1];
uint8_t blue = pStruct[2];```

This is perfectly legal to do (though not necessarily advisable).

Regards,
Steve A.

The Board helps those that help themselves.

Koshchi wrote:
you could treat it as an array

You could - but he wasn't

:wink:

Top Tips:

1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...