2D array and arduino pins

Hello.
I built a 4x4x4 LED Cube using an arduino nano. It does have enough pins for the way I wired up my LEDs. I have 4 common negative(CN) pins, pins A2-A5, one pin per layer and 16 common positive(CP) pins, pins D0-D13 and A0-A1, one pin per collomb.
I have trouble using the arrays to specify what pin I am working with. For instance, if I want to turn on all the LEDs and light them up 1 layer at a time, my code would look like follow in theory:


int i, j, k;

int arrCP[4][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, A0, A1}};
  // 0-13 represents pins D0-D13 on the board.

Int arrCN[4] = {A2, A3, A4, A5};

void setup(){
  // Set CP pins as OUTPUT.
  for(i=0; i<=3; i++){
    for(j=0; j<=3; j++){
      pinMode(arrCP[i][j], OUTPUT);
    }
  }

  // Set CN pins as OUTPUT.
  for(j=0; j<=3; j++){
      pinMode(arrCN[i], OUTPUT);
    }
}

void loop(){
  // Turn all Layers off: Layer CN must be HIGH
  // to turn the Layer off and LOW to turn the
  // Layer on
  for(i=0; i<=3; i++){
    digitalWrite(arrCN[i], HIGH);
  }

  // Turn every collomb on.
  for(i=0; i<=3; i++){
    for(j=0; j<=3; j++){
      digitalWrite(arrCP[i][j], HIGH);
    }
  }

  // Turn Layer by Layer on then off 3 times up
  // and down.
  for(k=1; i<=3; i++){
    for(i=0; i<=3; i++){
      digitalWrite(arrCN[i], LOW);
      delay(200);
      digitalWrite(arrCN[i], HIGH);
    }
  }
}

The problem I'm haveing is that the LED Cube does not light up at all and the RX, TX, POW and L LEDs on the nano all light up after I upload the code. No errors in the code are displayed.

If I code the peogram without arrays and for loops, the LED Cube lights up as it should.

Can someone please help me?

ToxicTeddyBear

Hello

First mistake here

for(j=0; j<=3; j++){
  pinMode(arrCN[i], OUTPUT);
}

You use i instead of j

Don't use global variables as loop indexes, to avoid exactly this kind of mistake

Another strange one.

And don't use single character variable names for global variables :smiley:

Looping through array indexes for 4-element arrays is traditionally done with "i < 4" rather than "i <= 3".

const byte arrCP[4][4] =
{
  {0, 1, 2, 3},
  {4, 5, 6, 7},
  {8, 9, 10, 11},
  {12, 13, A0, A1}
};
// 0-13 represents pins D0-D13 on the board.

const byte arrCN[4] = {A2, A3, A4, A5};

void setup()
{
  // Set CP pins as OUTPUT.
  for (size_t i = 0; i < 4; i++)
  {
    for (size_t j = 0; j < 4; j++)
    {
      pinMode(arrCP[i][j], OUTPUT);
    }
  }

  // Set CN pins as OUTPUT.
  for (size_t j = 0; j < 4; j++)
  {
    pinMode(arrCN[j], OUTPUT);
  }
}

void loop()
{
  // Turn all Layers off: Layer CN must be HIGH
  // to turn the Layer off and LOW to turn the
  // Layer on
  for (size_t i = 0; i < 4; i++)
  {
    digitalWrite(arrCN[i], HIGH);
  }

  // Turn every collomb on.
  for (size_t i = 0; i < 4; i++)
  {
    for (size_t j = 0; j < 4; j++)
    {
      digitalWrite(arrCP[i][j], HIGH);
    }
  }

  // Turn Layer by Layer on then off 3 times up
  // and down.
  for (size_t k = 1; k < 4; k++)
  {
    for (size_t i = 0; i < 4; i++)
    {
      digitalWrite(arrCN[i], LOW);
      delay(200);
      digitalWrite(arrCN[i], HIGH);
    }
  }
}

I'm just used to useing i<=3 in a for loop with an array.

The const byte arr and the size_t did help a bit, but the problem now is that the cube goes through the code ones and then stops with all LEDs turned off.

If I want to loop through an array from position 0-3, the code is as follow:

for(size_t i=0; i<4; i++)
{
  digitalWrite(arrCN[i], LOW);
  delay(200);
  digitalWrite(arrCN[i], HIGH);
}

And if I want to loop backwords from position 3-0 it should be:

for(size_t i=3; i>-1; i--)
{
  digitalWrite(arrCN[i], LOW);
  delay(200);
  digitalWrite(arrCN[i], HIGH);
}

But the code only runs from 0-3 and skips the whole for loop from 3-0.

If I use the 3-0 for loop as;

for(size_t i=3; i>=0; i--)
{
  digitalWrite(arrCN[i], LOW);
  delay(200);
  digitalWrite(arrCN[i], HIGH);
}

The cube lights up from bottom to top and then from top to bottom once and stops working.

'size_t' is unsigned so 'i' will ALWAYS be greater than or equal to 0. You should have gotten a warning about that from the compiler. Make sure you have "Compiler warnings:" set to "All".

One way to make it work:

for(size_t i=4; i>0; i--)
{
  digitalWrite(arrCN[i-1], LOW);
  delay(200);
  digitalWrite(arrCN[i-1], HIGH);
}

I've made my original code work. I still used global variables, but the bigest problem was rhe data type of rhe array. I set it to const byte and now everything works.

Thank you to everyone for helping me.
ToxicTeddyBear

4x4x4 = 64 LEDs and just as much can control 1 chip MAX7219 and you will need only 3-4 pins from Arduino. You will even be able to control the brightness of all LEDs in total. Because MAX7219 will take care of the multiplexing of LEDs, your program will be simpler and you will be able to make more interesting animations.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.