Go Down

Topic: Multidimensional Arrays (Read 557 times) previous topic - next topic

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: [Select]
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?

fdufnews

Quote
but I keep getting errors

If you don't say what errors it will be difficult to help ;)

robtillaart


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
Rob Tillaart

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

Magician

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

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy