4x4x4 LED Cube easier programming

I have built a 4x4x4 led cube that uses every pin on the Arduino Uno and need a little help and insight as to how to make it easier to program. In the program below you can see that the current system takes a number into either the LED_ON or LED_OFF program and then translates that into the two pins that would need to be on in order to power that specific led, the layers are 1-16 17-32 33-48 and 39-64. This works great except that for large graphics there are a ton of LED_ON(1) LED_ON(2)... for each row then a delay and the same for LED_OFF and that can get kind of very annoying when you just want to test the graphic. To combat this I was trying to come up with a way to load up an array with the LEDs that you would want on and then just cycle through those for each level so there is only 1 LED_ON and 1 LED_OFF for each level. I thought that this would work the way i have it set up but as of right now only LED 1 comes on and stays on and there is no movement of anything else from there on out. Please if anyone knows of a better way to throw large groups of numbers into a system like this or has a different style that I could use, i went for the ultra simplistic way, Let me know and modify the code as you see fit. If you have any questions about my logic or what I would like to see happen feel free to reply or message me.

TLDR: trying to use arrays as a program input and it is not working the program in question is all() all the way at the bottom of my code

Thanks,

void setup()
{
  for (int i=0; i<20; i++)
  {
    pinMode(i, OUTPUT);
  }
}

void loop()
{
all(100);
}

void LED_ON( int LED)
{
  int level;
  int pin;
  
  //Levels
  if (LED>=0 && LED<=16)       {level=16;}
  else if (LED>16 && LED<=32)  {level=17;}
  else if (LED>32 && LED<=48)  {level=18;}
  else if (LED>48 && LED<=64)  {level=19;}
  
  //LED on each level
  if (LED==1 || LED%16==1){pin=0;}
  else if (LED==2 || LED%16==2)    {pin=1;}
  else if (LED==3 || LED%16==3)    {pin=2;}
  else if (LED==4 || LED%16==4)    {pin=3;}
  else if (LED==5 || LED%16==5)    {pin=4;}
  else if (LED==6 || LED%16==6)    {pin=5;}
  else if (LED==7 || LED%16==7)    {pin=6;}
  else if (LED==8 || LED%16==8)    {pin=7;}
  else if (LED==9 || LED%16==9)    {pin=8;}
  else if (LED==10 || LED%16==10)  {pin=9;}
  else if (LED==11 || LED%16==11)  {pin=10;}
  else if (LED==12 || LED%16==12)  {pin=11;}
  else if (LED==13 || LED%16==13)  {pin=13;}  //Mixed up wiring these two
  else if (LED==14 || LED%16==14)  {pin=12;}  //Mixed up wiring these two
  else if (LED==15 || LED%16==15)  {pin=14;}
  else if (LED==16 || LED%16==0)   {pin=15;}
  
  digitalWrite(pin, HIGH);
  digitalWrite(level, HIGH);
}

void LED_OFF( int LED)
{
  int level;
  int pin;
  
  //Levels
  if (LED>=0 && LED<=16)       {level=16;}
  else if (LED>16 && LED<=32)  {level=17;}
  else if (LED>32 && LED<=48)  {level=18;}
  else if (LED>48 && LED<=64)  {level=19;}
  
  //LED on each level
  if (LED==1 || LED%16==1){pin=0;}
  else if (LED==2 || LED%16==2)    {pin=1;}
  else if (LED==3 || LED%16==3)    {pin=2;}
  else if (LED==4 || LED%16==4)    {pin=3;}
  else if (LED==5 || LED%16==5)    {pin=4;}
  else if (LED==6 || LED%16==6)    {pin=5;}
  else if (LED==7 || LED%16==7)    {pin=6;}
  else if (LED==8 || LED%16==8)    {pin=7;}
  else if (LED==9 || LED%16==9)    {pin=8;}
  else if (LED==10 || LED%16==10)  {pin=9;}
  else if (LED==11 || LED%16==11)  {pin=10;}
  else if (LED==12 || LED%16==12)  {pin=11;}
  else if (LED==13 || LED%16==13)  {pin=13;}  //Mixed up wiring these two
  else if (LED==14 || LED%16==14)  {pin=12;}  //Mixed up wiring these two
  else if (LED==15 || LED%16==15)  {pin=14;}
  else if (LED==16 || LED%16==0)   {pin=15;}
  
  
  digitalWrite(pin, LOW);
  digitalWrite(level, LOW);
}
void all(int r)
{
  //create array
 int led[64];
 int i=0;
 for(i=0; i<64; i++)
 {
   int j=1;
   led[i]=j;
   j++;   
 }
 for(i=0; i<r; i++)
 {
  for(i=0; i<16; i++){LED_ON(led[i]);}
  delay(2);
  for(i=0; i<16; i++){LED_OFF(led[i]);}
  delay(1);
  
  for(i=16; i<32; i++){LED_ON(led[i]);}
  delay(2);
  for(i=16; i<32; i++){LED_OFF(led[i]);}
  delay(1);
 
  for(i=32; i<48; i++){LED_ON(led[i]);}
  delay(2);
  for(i=32; i<48; i++){LED_OFF(led[i]);}
  delay(1);
 
  for(i=48; i<64; i++){LED_ON(led[i]);}
  delay(2);
  for(i=48; i<64; i++){LED_OFF(led[i]);}
  delay(1);
 }
}

Shell_4x4x4.ino (15.8 KB)

I'd have 2 arrays - a "level" array, and a "pin" array - or a single array containing a struct of "level" and "pin".

Those arrays (that array) would contain the pin combinations for each LED. Then you just reference the arrays to find out how to light the requested LED:

unsigned char layers[4] = {16,17,18,19};
unsigned char pins[16] = {0,1,2,3,4,5,6,7,8,9,10,11,13,12,14,15};

void led_on(unsigned char led)
{
    digitalWrite(pins[led & 0x0F], HIGH);
    digitalWrite(layers[led >> 4], LOW);
}

void led_off(unsigned char led)
{
    digitalWrite(pins[led & 0x0F], LOW);
    digitalWrite(layers[led >> 4], LOW);
}

I stress this is untested, and might not even compile, but it'll give you some pointers :wink:

I'm dumb sometimes in the all program where I initialize the array inside the for loop j will always stay 1 because it is initialized within the loop so every part in the array will be one. i have corrected it since and now it works so that all 64 leds are on. Thanks for the input!