Pages: [1]   Go Down
Author Topic: Problem with nested for loops for setting values in a 2D Array  (Read 386 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just don't see how that could work. A #define just does text substitution, so this line:

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

is expanded out to:

Code:
         
   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:
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.
« Last Edit: September 16, 2011, 07:50:53 pm by Nick Gammon » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When you #define a macro you are setting up a text substitution.  Your code:

Code:
#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:
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:
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];
          }
     }
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12487
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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:
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);
          }
     }
}
Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: