Go Down

### Topic: Problem with nested for loops for setting values in a 2D Array (Read 619 times)previous topic - next topic

#### Tolster

##### Sep 17, 2011, 12:40 am
Hey All,

First post, but I'll contribute if/when i see the opportunity.

So I bought a couple(4) of 8x8 led matrixes and 2 74ls154 4 to 16 decoders thats all hooked up and working fine, but now my focus turns to what to do with the display, so in wanting to section it out to display different digits, etc but the problem im having is when I attempt to set the values of an array to one that i defined(using a for loop each to select the row and column of the cell that should be written), i get an error no matter how i order it, here's my prototype code

...
int VirtArray1[5][3] = {
{1,1,1},
{1,0,1},
{1,0,1},
{1,0,1},
{1,1,1}};

...
#define zero[5][3] { \
{1,1,1}, \
{1,0,1}, \
{1,0,1},\
{1,0,1},\
{1,1,1}\
}

...

void test(){

for(int a = 0; a < 5; a++)
{

for(int b = 0; b < 3; b++)
{
if( h0 == 0){        //h0 is just an incrementer for a clock
VirtArray1[a] = zero[a];
}
}
}
}

and heres the problem i've been seeing, no matter how i reorder the loops or putting the if on the outside,

ArraynoTimer.cpp: In function 'void test()':
ArraynoTimer:208: error: 'set' was not declared in this scope
ArraynoTimer:208: error: expected `;' before 'VirtArray1'

can anyone advise on what i might be running into here? i can post the full code if need be.

Thanks!

#### Nick Gammon

#1
##### Sep 17, 2011, 02:27 amLast Edit: Sep 17, 2011, 02:50 am by Nick Gammon Reason: 1
I just don't see how that could work. A #define just does text substitution, so this line:

Code: [Select]
`            VirtArray1[a] = zero[a];`

is expanded out to:

Code: [Select]
`             VirtArray1[a] = [5][3] {        {1,1,1},        {1,0,1},        {1,0,1},        {1,0,1},        {1,1,1}} [a];`

That doesn't make sense, right?

This compiles, at least:

Code: [Select]
`int VirtArray1[5][3] = {        {1,1,1},        {1,0,1},        {1,0,1},        {1,0,1},        {1,1,1}};const int zero[5][3] = {        {1,1,1},        {1,0,1},        {1,0,1},        {1,0,1},        {1,1,1}};int h0;void test(){  for(int a = 0; a < 5; a++)  {    for(int b = 0; b < 3; b++)    {      if( h0 == 0){        //h0 is just an incrementer for a clock        memcpy (VirtArray1, zero, sizeof VirtArray1);      }    }  }}void setup () {}void loop () {}`

There could be other ways of doing it, this method should be fast enough, if slightly obscure.
Please post technical questions on the forum, not by personal message. Thanks!

http://www.gammon.com.au/electronics

#### johnwasser

#2
##### Sep 17, 2011, 02:36 am
When you #define a macro you are setting up a text substitution.  Your code:

Code: [Select]
`#define zero[5][3] = {{1,1,1},{1,0,1},{1,0,1},{1,0,1},{1,1,1}}; void test() {     for(int a = 0; a < 5; a++) {          for(int b = 0; b < 3; b++) {              VirtArray1[a][b] = zero[a][b];          }     }}`

gets expanded when the macro is processed into:
Code: [Select]
`void test() {     for(int a = 0; a < 5; a++) {          for(int b = 0; b < 3; b++) {              VirtArray1[a][b] = [5][3] = {{1,1,1},{1,0,1},{1,0,1},{1,0,1},{1,1,1}};[a][b];          }     }}`

As you can see, after the macro substitution this make no syntactic sense.  You can't use a #define to create an array.  You best bet it to define a const array.  Since your values are 0 and 1 you might be able to save a lot of space by using a boolean (0/1, false/true) array.  At least use byte rather than storing each bit in a 16-bit integer!

Code: [Select]
`const boolean zero[5][3] = {{1,1,1},{1,0,1},{1,0,1},{1,0,1},{1,1,1}}; void test() {     for(int a = 0; a < 5; a++) {          for(int b = 0; b < 3; b++) {              VirtArray1[a][b] = zero[a][b];          }     }}`
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

#### Tolster

#3
##### Sep 17, 2011, 03:26 am
Hey Guys,

thanks so much, I pulled the #define from an example I saw using an un-multiplexed 8x8 screen using all the pins, didnt even cross my mind before but makes complete sense now.

I know there are probably more elegant ways of going about this but its been a few years since school and I just wanted to dive in and get my sea legs back, thanks again for your help!

#### robtillaart

#4
##### Sep 17, 2011, 09:15 am

Because it seems you only want zero's and ones it might be worth to use ints and use some bit - magic

- http://arduino.cc/en/Reference/Bit -

give it a try
Code: [Select]
`const uint8_t zero[5] = { B00000111, B00000101, B00000101, B00000101, B00000111};  // only the last 3 bits are used so zero can be seen as an array[8][5]; void test() {     for(int a = 0; a < 5; a++) {          for(int b = 0; b < 3; b++) {              VirtArray1[a][b] = bitRead(zero[a], b);          }     }}`
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up

Please enter a valid email to subscribe