multi-dimensional arrays

Hello, I'm trying to write a program to control a small (4x4) LED matrix for a wearable electronics project using Leah Buchley's Lilypad arduino (http://www.cs.colorado.edu/~buechley/LilyPad/index.html)

I'm having some trouble with my code:

const int size = 4; //4x4 LED array
const int rows[size] = {1, 2, 3, 4};   //rows to pin mapping
const int cols[size] = {5, 6, 7, 8};   //cols to pin mapping
const int numPatterns = 2;   //Number of patterns programmed in
boolean patterns[numPatterns][size][size]; 
int time;

void setup() {   // run once, when the sketch starts
  for (int i = 0; i < size; i++) {
    pinMode(rows[i], OUTPUT);
    pinMode(cols[i], OUTPUT);
  }
   
  //Patterns 
  patterns[0] = {
    {1, 1, 1, 1},
    {1, 1, 1, 1},
    {1, 1, 1, 1},
    {1, 1, 1, 1}
  };
  
  patterns[1] = {
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0}
  };

  time = 1;   //start time at 1 so as not to skip first pattern
}

void loop() {   // run over and over again
  time = millis()%1000;
  int currPattern = 0;
  while (currPattern < numPatterns) {
    for (int currRow = 0; currRow < size; currRow++) {
      runRow(currRow, currPattern);
    }
    if (time == 0) {
      currPattern++;
    }
  }
}

void runRow(int currRow, int currPattern) {
  digitalWrite(rows[currRow], HIGH);   //Assert row to allow LEDs in it to turn on
  for (int col = 0; col < size; col++) {
    if (patterns[currPattern][currRow][col]) {
      digitalWrite(cols[col], LOW);   //Deassert column to turn on LED
    }
    else {
      digitalWrite(cols[col], HIGH);   //Keep column high to turn off LED
    }
  }
  digitalWrite(rows[currRow], LOW);   //Deassert row to prepare for next row run
}

The goal is to cycle through a fixed number of pre-programmed patterns, defined in the "setup" function. Every second ("time = millis()%1000" == 0) the currPattern variable increments, and the next pattern gets displayed.

To display the pattern, each LED is accessed one at a time, and either turned on or off, depending if the corresponding coordinates in the pattern contain a 1 or 0. This happens very fast, so it looks like more than one LED is on (because of persistance of vision).

Anyway, the problem I'm having is in the setup function where I define my patterns (simple test patterns for now, all 1's or all 0's). I get the following error:

In function 'void setup()':
error: expected primary-expression before '{' token In function 'void loop()':
In function 'void runRow(int)':

Which is totally meaningless to me. Any hints?

edit: fixed some other problems. If I use for loops to define the patterns in the setup function, I can get it to compile, but for loops are only useful for simple patterns like all 1's or all 0's, not for more complex patterns that I plan to implement.

Zenaida,

In C you can't assign "array constants" like this:

 patterns[0] = {
    {1, 1, 1, 1},
    {1, 1, 1, 1},
    {1, 1, 1, 1},
    {1, 1, 1, 1}
  };

However, you can do something similar, i.e. initialize an array. Try this at your declaration of patterns:

boolean patterns[numPatterns][size][size] = 
{
  {
    {1, 1, 1, 1},
    {1, 1, 1, 1},
    {1, 1, 1, 1},
    {1, 1, 1, 1}
  },
  {
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0}
  }
};

Then remove the assignment.

Regards,

Mikal

Thanks, that seems to work, although it'll require more comments to be clear I think. Is there no other way to define an array (after initialization) than individually assigning each element? Seems a bit strange, does anyone know the reason?

C just has never supported that kind of syntax. I don't know why.

You can sort of emulate the behavior of assigning one array to another by using the memcpy() function to copy all the bytes, but this is very prone to error

Mikal