Very strange LED cube error

I recently built a 4x4x4 single-color LED cube driven by an arduino uno. It is running smoothly but I am encountering a very weird error that I believe is programming related. Basically, I copy and paste a array called “l” into my sketch which I generate from a processing GUI I created. This array contains an integer value with each bit corresponding to an led in one of the four planes for each frame. Pretty straightforward and it runs totally smoothly. However, SOMETIMES, when I light up the whole cube (meaning all the values for the frame are the maximum unsigned int size), the program craps the bed. Instead of the first frame that should be totally lit up, it restarts the current animation just for a few frames, and then it switches over to the last played “l” array (I comment these out each time so this makes no sense!! unless it has to do with the internal memory or something?), and plays that array until the length of the original matrix is reached. Any suggestions? I have tried just turning off one LED, so the other 63 are lit, and it works totally fine. Adding the 64th makes everything go haywire…but the second “l” matrix i have commented out runs totally fine…I am so confused…any help would be appreciated, please let me know if I should clarify anything. And please don’t just rag on my code, I know it’s not perfect but I am new to this. Thanks.

I am uploading the code with only one array, but I have attached the actual code with all of them. This is the array that does work…why do none of the others???

int row[] = {A5,A4,A3,A2};
int col[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,A0,A1};

void setup(){
  for(int i=0;i<16;i++)
  pinMode(col[i],OUTPUT);
  //Serial.begin(9600);
 
}
//100 and 350
int time = 1;
int mtime = 300;
int counter = 5;

int l[] = {65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632};



void loop(){
for(int i=0; i< sizeof(l) /2;i = i+4){
  for(int q = 0;q<counter;q++){
 conv(l[i],i % 4); 
 conv(l[i+1],(i+1) % 4);
 conv(l[i+2],(i+2) % 4);
 conv(l[i+3],(i+3) % 4);
 delay(time);
  }
}


}

void conv(unsigned int num, int k){
  //word w=word(num);
  unsigned int leder;
  for(int i=15;i>=0;i--){
    if(bitRead(num,i) != 0){
    leder = (bitRead(num,i) * i + (16 * k));
    //Serial.print(bitRead(num,i));
    
      led(leder);
      //leder << 1;
    }
    else
    led(-1);
  }
  
}
/*void reset(){
    for(int i=0;i<16;i++){
      digitalWrite(col[i],LOW);
    }
    for(int i=0;i<4;i++){ 
      pinMode(row[i],INPUT);
    }
}
*/
void led(int leds){
 //delayMicroseconds(mtime / 100);
 if(leds != -1){
  for(int i=0;i<4;i++){
  if(16*i <= leds && leds < 16*(i+1)){
  pinMode(row[i],OUTPUT);
  digitalWrite(row[i],LOW);
  }
  }

 digitalWrite(col[leds % 16],HIGH);
 }
 delayMicroseconds(mtime);
 //delay(1);
  if(leds != -1){ 
  for(int i=0;i<4;i++)
  if(16*i <= leds && leds < 16*(i+1)){
    pinMode(row[i],INPUT);
  }
  digitalWrite(col[leds % 16],LOW);
  }
}

ledcube.ino (2.19 KB)

Here’s your problem, you want unsigned int

int l = {65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632};

Edit: counter → q
Added: you could also do this:

for(int q = 0; q < counter; q++){ /* make counter = 4, and not 5 */
 conv(l[i + q],(i + q) % 4); 
 delay(time); // is this really needed?
  }

Thanks for the reply :) Unfortunately changing it to an unsigned int did not fix the issue :/

EDIT: Could it have something to do with having outputs connected to pins 0 and 1? I'm going to involve shift registers soon so this won't be the case but I know wires in these pins can produce strange problems, I just don't know why everything works except this bizarre error...Also I think the error only occurs when there is more than 12 or so of the totally lit cubes in a row. Just getting more and more confused, but I guess that would explain why the array I listed works (it's only 8 in a row).

EDIT2: The counter actually has to do with the timing. I know I can reduce the four conv() lines to a for loop, I was just experimenting at first and left it as is. The delay probably isn't necessary, I was using it for timing of each frame but I kind of replaced it with the counter. Also, I don't know if this is relevant but I occasionally get a avrdude verification error first mismatch at byte 0x....could that be part of the issue? Something to do with the progmem?

How are the LED powered, from the arduino or external source?

Can you post a picture?

Also as long as you are not using the serial monitor, you can use pins 0 and 1.

They're powered by the Arduino. Only 1 is lit at a time (due to multiplexing, the "led()" function in the sketch) so there really shouldn't be any issues. I attached a pic, it might be hard to see but the 16 wires on the breadboard go underneath to the columns (pins 0-13,A0,A1), and the rows(pins A2-A5) go to the rows.

This link should work: https://www.dropbox.com/s/g8n76bzfehmiz1l/20140726_011345.jpg

EDIT: I can make a video showing the problem if it would help?

Please do

Here's the video: http://youtu.be/Y5OsjjG5ogQ

Try this,

int row[4] = {
  A5,A4,A3,A2};
int col[16] = {
  2,2,2,3,4,5,6,7,8,9,10,11,12,13,A0,A1};
int time = 1;
int mtime = 300;
int counter = 5;

//==============================================================
unsigned int l[160] = {
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632
};
//==========================================================

void setup()
{
  for(int i=0;i<16;i++)
    pinMode(col[i],OUTPUT);
  Serial.begin(115200);
  delay(1);
  Serial.print("FreeRam remaining: ");
  Serial.println(freeRam());
}

int freeRam() {
  extern int __heap_start, *__brkval;
  int v;
  return (int) &v - (__brkval==0?(int)&__heap_start:(int)__brkval);
}

void loop()
{
  for(int i=0; i < sizeof(l) /2; i += 4)
  {
    for(int q = 0; q < 4 ; q++)
    { 
      conv(l[i+q],(i+q) % 4);
      delay(time);
    }
  }
}

void conv(unsigned int num, int k)
{
  //word w=word(num);
  unsigned int leder;
  for(int i=15; i >= 0; i--)
  {
    if(bitRead(num,i) != 0)
    {
      leder = (bitRead(num,i) * i + (16 * k));
      //Serial.println(leder);
      led(leder);
    }
    else
      led(-1);
      delay(500);
  }
}

/*void reset(){
 for(int i=0;i<16;i++){
 digitalWrite(col[i],LOW);
 }
 for(int i=0;i<4;i++){ 
 pinMode(row[i],INPUT);
 }
 }
 */
 
void led(int leds)
{
  if(leds != -1)
  {
    for(int i=0; i<4; i++)
    {
      if((16*i) <= leds && leds < (16*(i+1)) )
      {
        pinMode(row[i],OUTPUT);
        digitalWrite(row[i],LOW);
      }
    }
    digitalWrite(col[leds % 16],HIGH);
  }
  delayMicroseconds(mtime);

  if(leds != -1)
  { 
    for(int i=0; i<4; i++)
      if((16*i) <= leds && leds < (16*(i+1)) )
      {
        pinMode(row[i],INPUT);
      }
    digitalWrite(col[leds % 16],LOW);
  }
}

I don’t have the LEDs to make 4x4x4 cube, but could you send me the array that is causing the problems?

FreeRam remaining: 1453

Here is the array I used in the video that was not working:

unsigned int l[] = {1,0,0,0,3,0,0,0,7,0,0,0,15,0,0,0,143,0,0,0,2191,0,0,0,34959,0,0,0,51343,0,0,0,59535,0,0,0,63631,0,0,0,63887,0,0,0,63903,0,0,0,63935,0,0,0,63999,0,0,0,65023,0,0,0,65535,0,0,0,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535};

EDIT: So the way I am multiplexing I am only turning on ONE led at a time...in theory could I run an 8x8x8 using the same technique times 8 (lighting 1 led for each of the 8 rows of 64 leds) without any kind of transistors or anything? I can just daisy chain some shift registers together and only 8 leds will be on at once which should be totally fine for the arduino right?

Other than using more RAM, I don’t see anything wrong with the code. You said you were powering the LEDs from the arduino, and that only one was on at a time, right? What about when the cube is fully lit? I still think its the amount of current being drawn which could be the probable issue. That or you have a memory leak somewhere that I don’t see.

int row[4] = {
  A5,A4,A3,A2};
int col[16] = {
  0,1,2,3,4,5,6,7,8,9,10,11,12,13,A0,A1};
int time = 1;
int mtime = 300;
int counter = 5;
#define DEBUG false

//==============================================================
#if false
unsigned int l[160] = {
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632
};
#endif
unsigned int l[444] = {1,0,0,0,3,0,0,0,7,0,0,0,15,0,0,0,143,0,0,0,2191,0,0,0,34959,0,0,0,51343,0,0,0,59535,0,0,0,63631,0,0,0,63887,0,0,0,63903,0,0,0,63935,0,0,0,63999,0,0,0,65023,0,0,0,65535,0,0,0,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535};

//==========================================================

void setup()
{
  for(int i=0;i<16;i++)
    pinMode(col[i],OUTPUT);
#if DEBUG
  Serial.begin(115200);
  delay(1);
  Serial.print("FreeRam remaining: ");
  Serial.println(freeRam());
  Serial.println(sizeof(l) /2);
#endif
}

int freeRam() {
  extern int __heap_start, *__brkval;
  int v;
  return (int) &v - (__brkval==0?(int)&__heap_start:(int)__brkval);
}

void loop()
{
  for(int i=0; i < sizeof(l)/2; i += 4)
  {
    for(int q = 0; q < 4 ; q++)
    { 
      conv(l[i+q],(i+q) % 4);
      delay(time);
    }
    delay(100);
  }
}

void conv(unsigned int num, int k)
{
  //word w=word(num);
  static unsigned int leder;
  for(int i=15; i >= 0; i--)
  {
    if(bitRead(num,i) != 0)
    {
      leder = (bitRead(num,i) * i + (16 * k));
      #if DEBUG
      bitRead(num,i)? Serial.print("1") : Serial.print("0");
      #endif
      led(leder);
    }
    else
      led(-1);
  }
  #if DEBUG
  Serial.println();
  #endif
}

/*void reset(){
 for(int i=0;i<16;i++){
 digitalWrite(col[i],LOW);
 }
 for(int i=0;i<4;i++){ 
 pinMode(row[i],INPUT);
 }
 }
 */
 
void led(int leds)
{
  if(leds != -1)
  {
    for(int i=0; i<4; i++)
    {
      if((16*i) <= leds && leds < (16*(i+1)) )
      {
        pinMode(row[i],OUTPUT);
        digitalWrite(row[i],LOW);
      }
    }
    digitalWrite(col[leds % 16],HIGH);
  }
  delayMicroseconds(mtime);

  if(leds != -1)
  { 
    for(int i=0; i<4; i++)
      if((16*i) <= leds && leds < (16*(i+1)) )
      {
        pinMode(row[i],INPUT);
      }
    digitalWrite(col[leds % 16],LOW);
  }
}

There is never more than 1 led lit. They are turned on and off so fast it appears that they're all on. What makes no sense to me is that in the video it works fine when 63 were lit, but as soon as 64 were lit it did the error.

Hi, just a thought, if you are using arrays, then element 0 to 63 will be 64 elements. The 64th will be array number 63.

Sorry but I have not had enough time this morning to read your sketch, coffee just taking effect, but it sounds like you are trying to turn on a LED that is not there.

Just a thought.

Tom...... :)