arduino 'hanging' after 30 secs. code problem?

I'm looking for some help:
I put together a 3x3x3 led cube, which I hooked up to the arduino. It seems to work fine, except for showing one quirk: after 33 seconds (give or take 1s), it kind of hangs. It doesn't really hang, but I'll have to tell a bit more before I can explain that.

I use two shift registers, to shift out 12 bits (9 for the anodes of each led in a plane, and 3 for pulling each plane to ground via a transistor). The 12 bits are stored in 2 separate bytes in an array, and I use multiplexing by pulling the planes to ground one after another, so I have 6 bytes in the array for one configuration (describing status of all leds in the cube). For an 'animation', I have a multiple of six bytes in the array that I loop through. The code I use is based on the shift out tutorial:

//**************************************************************//
//  Name    : shiftOutCode, Predefined Array Style              //
//  Author  : Carlyn Maw, Tom Igoe                           //


/Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;

//holders for infromation you're going to pass to shifting function
byte data, data1;
byte dataArray[167];

int number = 28;

void setup() {
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  Serial.begin(9600);

  //Arduino doesn't seem to have a way to write binary straight into the code 
  //so these values are in HEX.  Decimal would have been fine, too. 
dataArray[0] = 0x2a; //00101010
dataArray[1] = 0x40; //01000000
dataArray[2] = 0x2a; //00101010
dataArray[3] = 0x20; //00100000
dataArray[4] = 0x2a; //00101010
dataArray[5] = 0x12; //00010000
...
dataArray[165] = 0x20; //00100000
dataArray[166] = 0x1c; //00011100
dataArray[167] = 0x12; //00010000
}

void loop() {  
  for (int j = 0; j < (number+1); j++) {
    int time = millis();
    while(millis() < (time + 250)){
      for (int k = 0; k<3; k++) {
        //load the light sequence you want from array
        data = dataArray[j*6+2*k];
        data1 = dataArray[j*6+2*k+1];
        //ground latchPin and hold low for as long as you are transmitting
        digitalWrite(latchPin, 0);
        //move 'em out
        shiftOut(dataPin, clockPin, data1);   
        shiftOut(dataPin, clockPin, data);   
        //return the latch pin high to signal chip that it 
        //no longer needs to listen for information
        digitalWrite(latchPin, 1);
        delay(5);
      }
    }
  }
}



// the heart of the program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  // This shifts 8 bits out MSB first, 
  //on the rising edge of the clock,
  //clock idles low

  //internal function setup
  int i=0;
  int pinState;

  //clear everything out just in case to
  //prepare shift register for bit shifting
  //digitalWrite(myDataPin, 0);
  //digitalWrite(myClockPin, 0);

  for (i=0; i<=7; i++)  {
    digitalWrite(myClockPin, 0);
    if ( myDataOut & (1<<i) ) {
      pinState= 1;
    }
    else {      
      pinState= 0;
    }

    //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
    //register shifts bits on upstroke of clock pin  
    digitalWrite(myClockPin, 1);
    //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);
  }

  digitalWrite(myClockPin, 0);
}

(note that I cut out part of the array)

Now the reason I say that it's not really 'hanging' is that the led cube displays a full configuration, not just one plane. This means that the arduino keeps on looping through k-values for a fixed j-value in the main loop(). The funny thing is that it is really time related: if I change the speed at which the animation runs (changing the 250 in "(time + 250)"), it still 'hangs' around 33 secs, and in a different configuration. But I really cannot find anything in the code that can explain this. Anybody else can? Or can it be a hardware related thing? Any other reason?

Thanks!

I think the problem comes from the line:

int time = millis();

According to the reference manual, millis() returns a long type, rather than an integer (due to its size).

The manual also states "Note that the parameter for millis is an unsigned long, errors may be generated if a programmer tries to do math with other datatypes such as ints."

I think the problem comes from the integer variable overflowing, as its to small to hold the true value returned from millis()

Change it to a long, and check that your other data types are the correct ones, and see how it goes.

Wow programmer, that's a great first post of yours! :slight_smile:
Now it works like a charm...
That's something I wouldn't easily have found myself. Thanks!!

No problem, it's an easy mistake to make!

Glad it's working now :slight_smile: