Pages: [1]   Go Down
Author Topic: Multidimensional Arrays  (Read 437 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys,
I've built a 5*5*5 LED cube, and am trying to write code to control it.
Right now, my code goes:
Code:
int values[151] =
{
  0,
  1,0,0,0,1,
  0,1,0,1,0,
  0,0,1,0,0,
  0,1,0,1,0,
  1,0,0,0,1,
  //End of first layer.
  1,0,0,0,1,
  0,1,0,1,0,
  0,0,1,0,0,
  0,1,0,1,0,
  1,0,0,0,1,
  //end of second layer
  1,0,0,0,1,
  0,1,0,1,0,
  0,0,1,0,0,
  0,1,0,1,0,
  1,0,0,0,1,
  //end of third layer
  1,0,0,0,1,
  0,1,0,1,0,
  0,0,1,0,0,
  0,1,0,1,0,
  1,0,0,0,1,
  //end of fourth layer
  1,0,0,0,1,
  0,1,0,1,0,
  0,0,1,0,0,
  0,1,0,1,0,
  1,0,0,0,1,
  //end of fifth layer
  1,0,0,0,1,
  0,1,0,1,0,
  0,0,1,0,0,
  0,1,0,1,0,
  1,0,0,0,1
};

void setup()
{

  for (int i=21; i <= 52; i++)
  {
    pinMode(i,OUTPUT);
  }
}



void loop()
{

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


    for (int i = 48; i <=52; i++)
    {
      digitalWrite(i,HIGH);
    }
    digitalWrite(47+z,LOW);


    for (int i=0; i <= 25; i++)
    {

      if (values[i+z*25]== 1)
      {
        digitalWrite(i+21,HIGH);


      }
      if (values[i+z*25]==0)
      {
        digitalWrite(i+21,LOW);

      }

    }
    delay(1);



  }
}

Basically, it loads a huge array of on/off values, then uses them to control the LED's.

I want to modify the code to take animations, but I keep getting errors - help?
Logged

France
Offline Offline
Faraday Member
**
Karma: 36
Posts: 3430
There is an Arduino for that
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but I keep getting errors
If you don't say what errors it will be difficult to help smiley-wink
Logged

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


int values[151]   defines 6 layers for a 5x5x5 cube why?


Why don't you define the array itself multidimensional: 
int  values[5][5][5];  // uses 250 bytes


as you only store one bit in every int better make the type uint8_t (byte)
uint8_t  values[5][5][5] ;   // uses 125 bytes

or even use one byte as an array [0..7]
uint8_t values[5][5];   // uses 25 bytes

OK, you need bitoperators to get/set but these are available - http://www.arduino.cc/en/Reference/BitSet
Logged

Rob Tillaart

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

Montreal
Offline Offline
Faraday Member
**
Karma: 27
Posts: 2565
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It'd be helpful to look in playground first:
http://arduino.cc/playground/Main/InterfacingWithHardware#Output 
"cube"
Logged

Pages: [1]   Go Up
Jump to: