Explaining Arrays

I'm trying to make a 3x3 grid of LED's that have various different sequences to light up. With the LED's in a grid like:

1|2|3
4|5|6
7|8|9

One sequence (for example) will be led's 1,2,3,6,9,8,7,4 repeating. However, these LED's are not connected to pins 1,2,3... of the Ardunio. I'm trying to use a 2D array to reference the LED positions and the LED pins like so:

int ref[][] = {1,2,3,4,5,6,7,8,9},
{4,5,6,7,8,9,10,11,12}

Will doing this work, and how do I go about turning the LED's on using this method?

I currently have each sequence defined separately like so:

int cwcircle_step = 8; //number of steps
int cwcircle[] = {4,5,6,9,12,11,10,7}; //led's that light in this sequence.

and then calling them like this:

for (cwcircle_step = 0; cwcircle_step <= 7; cwcircle_step++)
          {
             digitalWrite(cwcircle[cwcircle_step], HIGH); 
             delay(100);
             digitalWrite(cwcircle[cwcircle_step], LOW); 
             delay(100);
          }

however if i ever want to change what pins the LED's are on, I will have to change each sequence separately. I am trying to create a "master reference table" so I only have to change it once

I'm trying to use a 2D array to reference the LED positions and the LED pins like so:

What do the two rows represent? Why is the type int? Do you have an Arduino with more than 255 pins?

It appears that the value in the first row is i+1, where i is the position. That doesn't warrant using an array.

If you want to make a 2D array it should be like this

byte ledPin[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
  • obviously with the correct pin numbers rather than my numbers.

Then you can address an element as ledPin[2][3] for example.

...R

I think you can simplify things by doing away with the need for a rank 2 array. If you want the value 1 to map to pin 4, 2 to map to pin 5, and so on, just use an offset. Since you seem to prefer starting with 1 for the array, you could use something like:

#define PINOFFSET   3

  // NOTE: first element is 0 and not used.
int ref[] = {0, 1 + PINOFFSET,2 + PINOFFSET,3 + PINOFFSET,4 + PINOFFSET,
             5 + PINOFFSET,6 + PINOFFSET,7 + PINOFFSET,8 + PINOFFSET,
             9 + PINOFFSET};
#define ARRAYSIZE(x) (sizeof(x) / sizeof(x[0]))

void setup() {

  int i;

  for (i = 1; i < ARRAYSIZE(ref); i++) {    // March through the array.
    digitalWrite(ref[i], HIGH); 
    delay(100);
    digitalWrite(ref[i], LOW); 
    delay(100);
  }
  ShowCCWCircle(ref);
}

void ShowCCWCircle(int x[]) // Light counter-clockwise, starting at position 1 
{
  digitalWrite(x[1], HIGH); 
  delay(100);
  digitalWrite(x[1], LOW); 
  delay(100);
  digitalWrite(x[4], HIGH); 
  delay(100);
  digitalWrite(x[4], LOW); 
  delay(100);
  digitalWrite(x[7], HIGH); 
  delay(100);
  digitalWrite(x[7], LOW); 
  delay(100);
  digitalWrite(x[8], HIGH); 
  delay(100);
  digitalWrite(x[8], LOW); 
  delay(100);
  digitalWrite(x[9], HIGH); 
  delay(100);
  digitalWrite(x[9], LOW); 
  delay(100);
  digitalWrite(x[6], HIGH); 
  delay(100);
  digitalWrite(x[6], LOW); 
  delay(100);
  digitalWrite(x[3], HIGH); 
  delay(100);
  digitalWrite(x[3], LOW); 
  delay(100);
  digitalWrite(x[2], HIGH); 
  delay(100);
  digitalWrite(x[2], LOW); 
  delay(100);
 
}
void loop() {


}

Not very elegant, but should work. (Polite-speak for: "I didn't test it".)

Is this by any chance related to How to turn on x number of LEDs at once, for 3x3 array? - Programming Questions - Arduino Forum ?