tlc library, loop stops...?

hello,
since a while I try to get following code to work... but unfortunately I stuck always at the same point: loop stops after running the code 8 (or x) times....but it is a loop and I dont understand why it stops.
(i'm not experienced programmer, but I thought this would be a really easy programm;)
I use the library from metalab for the TLC5940 and I'm running 25 Ledstrings (as well the hardware was not easy build - but works now, after several burnt TLC's) I light them up in different patterns and grayscale mode. A note: my hardware inverts the output; means 255=led off and 0=led on.
In the programm, there are a lot of delays, so it takes a long time to run through the whole programm, but I want to debug the programm as it should work afterwoods, therefor I let the delays as they are.
It seems to be as I would overwrite something - but have no clue what and why???

for any help or suggestions I would be very gratefull
thanks a lot!
sibylle
and here the misterious code:

#include <TLC5940.h>
// --------------------- SETTINGS
#define BAUD_RATE   9600 // change!    115200
#define DEBUG 
//#define DEBUG2
// ---------------------- ATTRIBUTES
// Parameter 1 is number of tlc5940 chips in series, 
// parameter 2 is number of grayscale bits per led.
TLC5940 leds(2, 8);


byte ring[25];
long randNumber;
int count = 0;
  
void setup()
{
  #ifdef DEBUG
  Serial.begin(BAUD_RATE);
  #endif
  leds.init();
  leds.setGlobalDC(40);
  leds.clear(); // Clear framebuffer
  leds.display(); // leds.display framebuffer 
  randomSeed(analogRead(0));
  delay(10);
}


void loop() {
  //leds.clear();
 
 #ifdef DEBUG
 count = count + 1;
 Serial.println (count, DEC);
 #endif
 #ifdef DEBUG
  Serial.println ("lauflicht");
 #endif
 lauflicht();
  #ifdef DEBUG
  Serial.println ("lauflicht_random2");
 #endif
 lauflicht_random2();
  #ifdef DEBUG
  Serial.println ("dimm_einzel_rechts");
 #endif
 dimm_einzel_rechts();
  #ifdef DEBUG
  Serial.println ("dimm_einzel_links");
 #endif
 dimm_einzel_links(); 
  #ifdef DEBUG
  Serial.println ("dimmall_on_off();");
 #endif
 dimmall_on_off();
  #ifdef DEBUG
  Serial.println ("halbkreis");
 #endif
 halbkreis();
  #ifdef DEBUG
  Serial.println ("halbkreis_schwell_a");
 #endif
 halbkreis_schwell_a();
  #ifdef DEBUG
  Serial.println ("dimm_einzel_shift");
 #endif
 dimm_einzel_shift();
  #ifdef DEBUG
  Serial.println ("dimm_einzel_shift_prob");
 #endif

 dimm_einzel_shift_prob(); 
 #ifdef DEBUG
 Serial.println ("schwell");
 #endif
 schwell();

}
  



void lauflicht() 
{
  int a;
  for (a = 0; a < 2; a ++) { 
    int i = 25;
    for (i = 0; i < 25; i++) { 
      leds.setLED(i, 0); 
      leds.display(); 
      delay(20); 
    }

    for (i = 0; i < 25; i++) { 
      leds.setLED(i, 255); 
      leds.display(); 
      delay(20);
    }

   delay(1000);
   for (i = 24; i >= 0; i--) {
      leds.setLED(i, 0); 
      leds.display();  
      delay(20); //20
    }
  
    for (i = 24; i >= 0; i--) {
      leds.setLED(i, 255); 
      leds.display();  
      delay(20); //20
    }

  delay(2000);
  
  }
}


void lauflicht_random2() {
  
  int a;
  for (a = 0; a <= 3 ; a ++) {
    int i = 25; 
    randNumber = random (5, 500);
    for (i = 0; i < 25; i++) { 
      leds.setLED(i, 0);
      leds.display();       
      delay(randNumber);
    }
 
    randNumber = random (5, 500);
    for (i = 0; i < 25; i++) { 
      leds.setLED(i, 255); 
      leds.display(); 
      delay(randNumber);
    }

   delay(randNumber * 10); 

   randNumber = random (5, 500);
   for (i = 24; i >= 0; i--) {
      leds.setLED(i, 0); 
      leds.display();  
      delay(randNumber);
    }
    randNumber = random (5, 500);
   for (i = 24; i >= 0; i--) {
      leds.setLED(i, 255); 
      leds.display();  
      delay(randNumber);
    }

  delay(randNumber * 10); 
  
  }
}

void dimm_einzel_rechts() 
{   
    int i;
    int k;
    for (i = 0; i < 25; i++) {
        leds.setLED (i, 255); 
        leds.display();
    }
    delay(100);
     
    for (i = 24; i >= 0; i-=2) {
      
      for (k = 255; k >= 0; k-=2) {
        leds.setLED(i, k); 
        leds.display();
        delay(5); 
      }    
        
      for (k = 0; k < 255; k+=2) {
        leds.setLED(i, k); 
        leds.display(); 
        delay(5);
      }
    }
   delay (1000); 
}

void dimm_einzel_links() 
{
   int i;
   int k;
   for (i = 0; i < 25; i++) {
        leds.setLED (i, 255); 
        leds.display(); 
    }
    delay(300);
    for (i = 0; i < 25; i+=4) {
      for (k = 255; k >= 0; k-=2) {
        leds.setLED(i, k); 
        leds.display();
        delay(5); 
      }    
        
      for (k = 0; k < 255; k+=2) {
        leds.setLED(i, k); 
        leds.display(); 
        delay(5);
      }
    } 
    delay (2000); 
}

void dimmall_on_off() 
{
  
  int i;
  int k;
  int a;
  for (a = 0; a <= 1; a++) {
    for (k = 255; k >= 0; k--) { 
      for (i = 0; i < 25; i++) {
           leds.setLED(i, k); 
     }
    leds.display();  
    delay(30); //30
      }
  
  delay(300); //300

    for (k = 0; k < 255; k ++) { 
        for (i = 0; i < 25; i++) {
        leds.setLED(i, k); 
        }
    leds.display();  
    delay(30); //30
    }
  delay(300);  //300
  }
  delay(1000);
}


void halbkreis() {
  
  int i;
  int k;
  for (k = 255; k >= 0; k --) { 
    for (i = 0; i < 25; i++)   {     
      leds.setLED(i, k); 
      
    }
    leds.display();  
    delay(20);  //20
  }
  for (i = 0; i < 13; i++) {
    leds.setLED(i, 255); 
    leds.setLED(24-i, 255); 
    leds.display();
    delay(100);
  }
  for (i = 12; i > 0; i--) {
    leds.setLED(i, 0); 
    leds.setLED(25-i, 0); 
    leds.display();
    delay(100);
  }
  for (k = 0; k < 255; k++) { 
    for (i = 0; i < 25; i++) {
        leds.setLED(i, k);
       //delay(10); 
    }
   leds.display();  
   delay(20); // 20
    }
   delay (2000);
}


void halbkreis_schwell_a() 
{
 int i;
 int k;
 int z = 0;
 for (i = 0; i <= 1 ; i++) {
     for (k = 255; k >= 0; k--) {
          for (z = 0; z < 13; z++) {
            leds.setLED(z, k);
            leds.setLED((z + 12) % 25, 255 - k);  
          }
       leds.display();
       delay(40); //40
     }
     for (k = 255; k >= 0; k--) {
         for (z = 25; z > 12; z--) { 
            leds.setLED(z, k);
            leds.setLED((z + 12) % 25, 255 - k); 
            
          }          
       leds.display();
       delay(40); //40
     }
     
     
  }
  delay(1000);
}



void dimm_einzel_shift() 
{
 int i;
 int k = 0;
 for (i = 0; i < 25; i++) {
        leds.setLED(i, 255); 
        }
    leds.display();  
    delay(50);
 for (i = 0; i < 25; i++) {
     for (k = 255; k >= 0; k-=2) {
          leds.setLED((i+1) % 25, k); 
          leds.setLED(i, 255 - k); 
          leds.setLED((i+6) % 25, k); 
          leds.setLED((i+5) % 25, 255 - k); 
          leds.setLED((i+11) % 25, k); 
          leds.setLED((i+10) % 25, 255 - k); 
          leds.setLED((i+16) % 25, k); 
          leds.setLED((i+15) % 25, 255 - k); 
          leds.setLED((i+21) % 25, k); 
          leds.setLED((i+20) % 25, 255 - k); 
          leds.display();
          delay(50); //50
     }    
    
 }          
delay(1000);
}

void dimm_einzel_shift_prob() 
{
 int i;
 int k;
 for (i = 0; i < 25; i++) {
        leds.setLED(i, 255); 
        }
    leds.display();  
    delay(2000);//2000

 for (i = 0; i < 25; i++) {
     for (k = 255; k >= 0; k--) {
          leds.setLED((i+1) % 25, k); 
          leds.setLED(i, 255 - k); 
          leds.display();
          delay(10); //10
     }    
    
 }  
delay(1000); //1000

}




void schwell() {
  int i = 25;
  int k = 255;
  for (k = 255; k >= 0; k --) { 
    for (i = 0; i < 25; i++) {     
      leds.setLED(i, k);
      ring[i] = k; 
    }
     
    leds.display();  
    delay(20);
  }
  delay(100);
  zufall();
}

void zufall() { 
 int i;
 int k; 
 int y;
 static int x;

    
  for (i = 0; i < 25; i++) {  
    x = x + 1;
    #ifdef DEBUG2
     Serial.println("counter bis 25");
     Serial.println(x, DEC);
     #endif
       if (x > 25) { //25
         x = 0;
        #ifdef DEBUG2
        Serial.println("groesser 25");
        Serial.println(x, DEC);
        #endif
        delay(100);
        loop();
    }
    randNumber = random(25);
     #ifdef DEBUG2
     Serial.println("randNumber y");
    Serial.println(randNumber, DEC);
    #endif
    y = randNumber;
    
    if (ring[y] == 255){
      delay(100);
      #ifdef DEBUG2
      Serial.println("ringy == 255");
      #endif
      zufall();
    }
    for (k = 0; k < 255; k++) { 
      leds.setLED(y, k);
     // #ifdef DEBUG2
    //Serial.println("k");
    //Serial.println(k, DEC);
    //#endif
      leds.display();  
      delay(30);
    }
    ring[y] = k;
    #ifdef DEBUG2
     Serial.println(k,DEC);
    Serial.println("ring y");
    Serial.println(y, DEC);
    Serial.println(ring[y], DEC);
    #endif
  }

    
}

You have all those debug statements in there but don't say what the last one printed when the loop stops :wink:

Have a look in the function you call just after the last debug statement you see on the serial monitor.

BTW, you can simplify your debug staments using a macro

This:
#ifdef DEBUG
Serial.println ("lauflicht");
#endif
lauflicht();

can be replaced with

DEBUG_PRINT("lauflicht");
lauflicht();

if at the top of your sketch you declare a debug macro as follows

#ifdef DEBUG
#define DEBUG_PRINT(_string) Serial.println (_string);
#else
#define DEBUG_PRINT(_string) // do nothing
#endif

Note this macro only works with a single string parameter, you can define others if you really need to.

thanks for good tips, I just implementet this define debug, as an programmer told me to do so (for quickly aktivate debugging and not).
so sorry if its not the easiest way to read.
but concerning my problem:
it stops (mostly;) after printing 8 (for the (loop)count) and lauflicht, the first function in the loop....
and "lauflicht" is really harmless, I dont see any error in it!!!
I will try today another time without my last funktion "schwell", as "schwell" uses as only funktion an array, one of the possibilities to overwrite unconscious a variable.....?

still very hopefull to get an advise how to solve this problem;)
thanks for any tips hints suggestions....
sibylle

The symptoms you describe could be caused by overwriting memory, for example by writing data beyond the bounds of an array. Ring[] is the only array I spotted and the code looks like it doesn't write beyond ring[24] but its worth double checking this.

Also, I don't know how much ram the library you include uses but its possible that your debug strings are using up all the RAM. You could try shortening the strings just to see if that makes a difference.

The symptoms you describe could be caused by overwriting memory, for example by writing data beyond the bounds of an array. Ring[] is the only array I spotted and the code looks like it doesn't write beyond ring[24] but its worth double checking this.

I will try to check that again

Also, I don't know how much ram the library you include uses but its possible that your debug strings are using up all the RAM. You could try shortening the strings just to see if that makes a difference.

do you mean less characters in debugging?
but if i uncomment the debugging (means when I let run the programm without debugging) the same things happens, means the animation i can controll over the leds they stop as well...
is there a possibility to check how much SRAM the library uses?
thanks again for your tips in a awfull-to-read-newbi-code.
sibylle

Yes, I did mean the debug strings, but if the symptoms are the same with and without the strings then its probably not due to running out of RAM

anyway, there is a memory test here: Arduino Playground - AvailableMemory

thanks alot, will test that!
sibylle

thanks mem, for the very good tip to use the code you mentioned above (looking at the memory)
As I had bad feelings about function schwell, which uses as only function an array, I tried out my programm without this function and yes: it works!
but not the array was the problem, but that I called another function which called the function loop (instead return) - and this killed somehow the hole ram, suddenly there was 1023 free Bytes - a wonder it works for 8 times:)) yes big newbi-error;)
thanks again!
sibylle

Good to hear you have it fixed. And that kind of problem is not reserved just for newbies :wink: