1

I have 4 arrays and each has different amount of entries. All these arrays are collected in another one. It's implemented like this:

Code in a c library

static const int32_t ONE_COLOR[2] = { RGB_BLACK, RGB_WHITE };


static const int32_t TWO_COLOR[4] = { RGB_WHITE, RGB_RED, RGB_GREEN, RGB_BLUE };


static const int32_t THREE_COLOR[8] = { RGB_BLACK, RGB_RED, RGB_GREEN, RGB_BLUE,
    RGB_CYAN, RGB_YELLOW, RGB_MAGENTA, RGB_WHITE };


static const int32_t FOUR_COLOR[16] = { RGB_WHITE, RGB_RED, RGB_GREEN, RGB_BLUE,
    RGB_CYAN, RGB_YELLOW, RGB_MAGENTA, RGB_DARK_RED, RGB_DARK_GREEN,
    RGB_DARK_BLUE, RGB_LIGHT_BLUE, RGB_LIGHT_GREEN, RGB_ORANGE, RGB_LIME,
    RGB_PINK, RGB_LILA };

static const int32_t* COLOR_ARRAY[4] = { ONE_COLOR, TWO_COLOR, THREE_COLOR,
    FOUR_COLOR };

How can I access COLOR_ARRAY[1][2] for example? Thanks for your help :)

Edit Example code for access:

Code for Arduino

for (i = 0; i < colorsLen; i++) {
            n = 0;
            for (j = 0; j < colorsWrite; j++) {
                    if (bitArray[i * colorsWrite + j] == 1) {
                            n |= 1 << (colorsWrite - 1 - j);

                    }
            }
            colors[i] = COLOR_ARRAY[colorsWrite - 1][n];
// testing access
//              colors[i] = ONE_COLOR[n];
//              colors[i] = n;
    }

colorsLen is the length of the array colors and colorsWrite is the amount of bits that can be transmitted with one color.

The purpose is to transmit data via light from a smartphone to an arduino and back. To get more bandwith the data is coded with colors. colorsWrite defines how big colorsLen should be to get all data transmitted.

famalgosner
  • 97
  • 1
  • 8
  • What is the problem with ``COLOR_ARRAY[1][2]`` ? – hivert Jul 16 '13 at 22:11
  • I tried it and it compiles. But if I want to access it on my arduino, it dies :( If I access one of the arrays *_COLOR directly it works. – famalgosner Jul 16 '13 at 22:13
  • Does it die giving some error message ? Are you sure that you don't get out of one of the arrays (eg as in ``COLOR_ARRAY[1][33]``) ? – hivert Jul 16 '13 at 22:16
  • There are no error messages. But if I replace the access of the array with a fixed value or some of the 'element'-arrays it works fine. There can't be an overflow inside the array itself – famalgosner Jul 16 '13 at 22:19
  • Can you show some more working code ? – hivert Jul 16 '13 at 22:22
  • I posted it here for better reading: [link to example code](http://pastebin.com/an6im9iW) – famalgosner Jul 16 '13 at 22:30
  • what is the maximum value of colorsLen and colorsWrite? – nio Jul 16 '13 at 22:38
  • it is min = 1 and max = 4 – famalgosner Jul 16 '13 at 22:38
  • Even if I access a fixed value from the array like `colors[i] = COLOR_ARRAY[colorsWrite - 1][0];` the software fails – famalgosner Jul 16 '13 at 22:55
  • I did try the code on PC and it worked, so this is maybe not a generic coding error, i don't have access to full code. Anyway this is a cool project idea, could you please send me an email? (i have an address in my profile) – nio Jul 16 '13 at 23:11
  • I build a small example and it worked on pc and on a small arduino test project. But if the arduino uses the library with the defined arrays it doesn't work. Really strange... – famalgosner Jul 16 '13 at 23:16
  • it looks like email addresses are hidden... ive put my into profile description – nio Jul 16 '13 at 23:26

2 Answers2

0

Well, judging from your question, you access COLOR_ARRAY[1][2] just like that. So the [1] in that expression is: TWO_COLOR[], and the [2] is: RGB_GREEN. So you do access an array as you did.

I.e: const COLOR clr = COLOR_ARRAY[1][2];

I might be missing some part of your question, but I hope it helps or clarifies.

Regards, Øyvind

EDIT This answer might give you a pointer Defining a static array into a C or C++ source file

Community
  • 1
  • 1
Oyvind Andersson
  • 362
  • 6
  • 22
0

this line:

n |= 1 << (colorsWrite - 1 - j);

n will be at least 1, because 1<<0 = 1 ... you are using n as index to ONE_COLOR[n] so you cant access the color at index zero which is RGB_BLACK.

nio
  • 5,141
  • 2
  • 24
  • 35
  • sorry then, maybe if you describe in your question the purpose of those colorsLen and colorsWrite variables? – nio Jul 16 '13 at 22:47