TLC 5940 Fading Loop Issue

I am fairly new to the arduino, and so far I love it. However, I am having an issue with trying to get a chunk of code to loop once the program has finished. Here is the code:

#include "Tlc5940.h"
#include "tlc_fades.h"

void  setup ()
{

  Tlc.init ();
  
  {
    tlc_addFade (0, 0, 500, 1000, 3000); tlc_addFade (0, 500, 0, 3001, 4000); 
    tlc_addFade (3, 0, 500, 1000, 3000); tlc_addFade (3, 500, 0, 3001, 4000);
    tlc_addFade (6, 0, 500, 1000, 3000); tlc_addFade (6, 500, 0, 3001, 4000);
    tlc_addFade (9, 0, 500, 1000, 3000); tlc_addFade (9, 500, 0, 3001, 4000); 
    tlc_addFade (12, 0, 500, 1000, 3000); tlc_addFade (12, 500, 0, 3001, 4000); 
    tlc_addFade (15, 0, 500, 1000, 3000); tlc_addFade (15, 500, 0, 3001, 4000); 
  }  
  
}

void  loop ()
{
 tlc_updateFades ();
 Tlc.update();

}

Now from what I understand the tlc_updateFades is looping in order to get the fading effect(?). What is happening is that I am getting one full cycle of fading, then it ends. I've tried moving the addFade into the loop, and that just breaks things. I have also scoured the internet looking for a solution, and came up empty handed. I feel like the solution is really simple, but I cannot figure out what it is. Any ideas?

I am using the "Tlc5940.h" and the "tlc_fades.h" found here: https://code.google.com/p/tlc5940arduino/source/browse/trunk/Tlc5940/?r=43

What is happening is that I am getting one full cycle of fading, then it ends

Yes that is because you only call the fading functions in the setup, that will be done only once.

To make them repeat move them into the loop function.

When I move the add_Fades into the looping block, it glitches out. When it fades down, instead of turning off, 4 of the leds jump to a random brightness, and stay on. It will not loop if it is in the loop(); block.

it glitches out.

Not a term I recognise.

It will not loop if it is in the loop(); block.

Rubbish.

The loop function will repeat unless something stops it. You are not using the fade and update functions correctly. Read the documentation again on how to use them and look at the examples.

I’m sorry for my loose / slang terms. I didn’t know a word that would be better for when the LED’s start to fade, then jump to a random brightness and stay there. I will look into it again. As for the examples that are provided, there isn’t one for making a single color fade up and down continuously. I tried many times to modify the example that makes the led’s fade randomly, but I didn’t have any luck. I don’t understand what would be stopping the loop if the tlc_addFade commands are in the loop block.

I didn't know a word that would be better for when the LED's start to fade, then jump to a random brightness and stay there.

I would use:- The LED's starts to fade, then jump to a random brightness and stay there.

You are not understanding how fade works. Take the fade example and work from there. Use serial print commands to see where about in the code you are and what value key variables are.

The tlc_addFade calls take times relative to the millis timer not absolute numbers, again see the example. Look at the reference page for the library http://alex.kathack.com/codes/tlc5940arduino/html_r012/tlc__fades_8h.html

Grumpy_Mike: I would use:- The LED's starts to fade, then jump to a random brightness and stay there.

You are not understanding how fade works. Take the fade example and work from there. Use serial print commands to see where about in the code you are and what value key variables are.

The tlc_addFade calls take times relative to the millis timer not absolute numbers, again see the example. Look at the reference page for the library http://alex.kathack.com/codes/tlc5940arduino/html_r012/tlc__fades_8h.html

Huh, I cannot believe I haven't come across that page yet. Though I was able to get my LED's to fade up and down, and loop! Yay!

Now my next question is, is there a way to make the leds fade up and down while cycling the colors(red to green to blue)? I tried making an if/if statement, but that didn't work. I almost need a delay in the code, but wherever I add one it changes how it fades, not when it fades. I'm still learning the basics, and sorry if I overlook something that is staring me in the face :/

Well done. It would be good to post your working code so that others can learn how it is done.

Adding a delay is a bad thing because it stops the processor doing anything useful.

You need to look at the blink without delay sketch in the IDE and look up state machines like described here:- http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

Ah, yes. I will for sure look into it! Here is the code that makes the LED’s fade up and down continuously:

#include "Tlc5940.h"
#include "tlc_fades.h"

TLC_CHANNEL_TYPE channel;
int analogValue;
int duration;
int maxValue = 4000;

void setup()
{
  Tlc.init();
}

void loop() {

  if (tlc_fadeBufferSize < TLC_FADE_BUFFER_LENGTH - 2) {
       
     if (!tlc_isFading(0)){ //blue LED
      duration = 2000;
      tlc_addFade(0, 28, maxValue, millis(), millis()+ duration );
      tlc_addFade(0, 4000, 28, millis()+ duration, millis()+ duration + duration);}
      
      if (!tlc_isFading(4)){//green LED
      duration = 2000;
      tlc_addFade(4, 28, maxValue, millis(), millis()+ duration );
      tlc_addFade(4, 4000, 28, millis()+ duration, millis()+ duration + duration);}
      
      if (!tlc_isFading(8)){ //red LED
      duration = 2000;
      tlc_addFade(8, 28, maxValue, millis(), millis()+ duration );
      tlc_addFade(8, 4000, 28, millis()+ duration, millis()+ duration + duration);}
      
      
    if (channel++ == NUM_TLCS * 16) {
      channel = 0;
      tlc_updateFades();
    }
  }
}

Alright so I looked at the state machine tutorial and came up with this:

#include "Tlc5940.h"
#include "tlc_fades.h"

int nextTime = 1000;  // Do this every second or 1000 milliseconds
long int goTime;
int count = 0;
int upperLimit = 2;
int lowerLimit = 0;

void setup(){
Tlc.init();
goTime = millis();

}

void loop(){
if(millis() >= goTime) functionGo();
tlc_updateFades();
Tlc.update();
}
void functionGo(){
  if (count == 0) {
      tlc_addFade(0, 0, 4000, millis(), 1000);
      tlc_addFade(0, 4000, 0, 1001, 2000); 
        count = count + 2;
    }

else if (count > lowerLimit) {     
      tlc_addFade(0, 0, 4000, millis(), 1000);
      tlc_addFade(0, 4000, 0, 1001, 2000);  
        count = count - 2;
}
goTime = millis() + nextTime;
}

The purpose of the count is to make it loop as I couldn't find another way to make it loop on its own. However, it doesn't appear to be counting. It will fade once, and then stay off. If I change the times in the else statement, it will fade twice then stay off. If there is a way (and I'm sure there is) to make the count portion work, and if so, will that method of looping be practical for 15 leds..?

As the lower limit is 2 and you only change the counter when count equals zero the else if will always return false because count can never be greater than lower limit only equal to it.

As written you don't need the else if anyway, just else.

Grumpy_Mike: As the lower limit is 2 and you only change the counter when count equals zero the else if will always return false because count can never be greater than lower limit only equal to it.

As written you don't need the else if anyway, just else.

Do you mean the upper limit is 2...? I'm not quite grasping what you mean. When I read your reply, it makes sense, but when I look at the code, it does not.

I changed the code a little bit and got it to fade twice. From what I wrote, I'm seeing if count = lowerLimit, fade then add 2 to count. else if count does not equal 0, fade again and make count equal the lowerLimit. The if else seem to be working, but it strikes me that it might be a time issue because if I change the time under the else statement to the same as the times under the if statement, it will only fade once.

void functionGo(){
  if (count == lowerLimit){
      tlc_addFade(0, 0, 4000, 0, 1000); //command that tells how to fade (channel on chip, startbrightness, end brightness, start time, end time)
      tlc_addFade(0, 4000, 0, 1001, 2000); 
      count + 2;  
  }  
  else (count != 0); {    
      tlc_addFade(0, 0, 4000, 2001, 3000);
      tlc_addFade(0, 4000, 0, 3001, 4000);  
      count == lowerLimit;
}
goTime = millis() + nextTime;
}

Now when I said remove the if it means remove the conditions, the brackets for the if as well. As you had it the ; terminated the else condition and what was the else clause always got done.

void functionGo(){
  if (count == lowerLimit){
      tlc_addFade(0, 0, 4000, 0, 1000); //command that tells how to fade (channel on chip, startbrightness, end brightness, start time, end time)
      tlc_addFade(0, 4000, 0, 1001, 2000); 
      count + 2;  
  }  
  else {    
      tlc_addFade(0, 0, 4000, 2001, 3000);
      tlc_addFade(0, 4000, 0, 3001, 4000);  
      count = 0;
}
goTime = millis() + nextTime;
}

Also :- count == lowerLimit is a condition not an assignment

HOWEVER you are STILL not getting the times you have to put into the fade call. These are not relative times they are absolute times. So when you do:- tlc_addFade(0, 4000, 0, 3001, 4000) That starts 3001 mS after the sketch starts running and terminates 4000 mS after the sketch starts running. Therefore once the sketch has been running for 4 seconds calling that line will have no further effect. You must specify the time as it is now. So to get that fade working you will have to use:- tlc_addFade(0, 4000, 0, millis(), milis() + 1000 ) to get a fade that will work at any time.

Whenever I remove the condition, it will not fade twice, only once. I know you you suggested that I should remove it, though I cannot see how. I tried replacing the int count with (!tlc_isFading) and still I could not get it to work right. I was able to get it to fade twice. However when I added your change: tlc_addFade(0, 0, 4000, millis(), millis() + 1000); it gets stuck in a fading up loop. I tried everything to figure out where the millis's should go and the correct adding of times to it, and I can get it to fade twice, then it gets stuck in a fading up loop again. I've messed with it so much that I don't remember how I got it to do that. As of right now the code makes it fade twice and stay off. I do understand that I need to use millis() to get the relative time, however, I don't understand why it it won't work when the code takes the current time and adds 1000mS to it every time as shown here:

tlc_addFade(0, 0, 4000, millis(), millis() + 1000); 
tlc_addFade(0, 4000, 0, millis() + 1000, millis() + 1000);

Here is my current chunk of code that fades twice and stays off.

if (count == lowerLimit){
      tlc_addFade(0, 0, 4000, millis(), 1000); 
      tlc_addFade(0, 4000, 0, 1001, 2000); 
      count + 2;  
  }  
   else (count != 0);{    
      tlc_addFade(0, 0, 4000, millis() + 1000, 3000); 
      tlc_addFade(0, 4000, 0, millis() + 3001, 4000);  
      count = 0;

Why do you not read what I said? That last piece of code is just so rubbish and I have told you why. EVERY fade command must have absoloute times in it. That means a millis then a millis plus something.

Also if you want two fades then the second fade must start after the first one has finished, that needs to be reflected in the millis time you give it. So the second fade starts at the time milis plus the time the last one finished faiding.

Unless you change what you are writing in your code you will never get it working. If you stick to code that appeares to do something we are all wasting our time here.

Make changes, report how it works and post the new code. That is the only way I can help you. You keep going round in circles.

Want new code? I gotcha some freshly redesigned (kinda) code. I removed the whole count part of it and replaced it with (!tlc_isFading). As of right now it will fade 1 color up down, then another color up down. I had to look at the whole goTime and nextTime and understand how that works, which was part of the problem I was having. The int nextTime needs to equal the end time of the last fade or it will not work right. Either the colors will fade into each other or if it is more than the end time it will not execute the else part of the code. The fun part will be getting more colors to fade at different times, which again has to do with the int nextTime and that part of the state machine. Anyways, here is the code:

#include "Tlc5940.h"
#include "tlc_fades.h"

int nextTime = 2000;  // Do this every 2 seconds or 2000 milliseconds
long int goTime;

void setup(){
Tlc.init();
goTime = millis();

}

void loop(){
if(millis() >= goTime) functionGo();
tlc_updateFades();
Tlc.update();

}
void functionGo(){
  if (!tlc_isFading(0)){
      tlc_addFade(0, 0, 4000, millis(), millis() + 1000);
      tlc_addFade(0, 4000, 0, millis()+1001, millis() + 2000);
  }  
   else { 
      tlc_addFade(2, 0, 4000, millis(), millis() + 1000);   
      tlc_addFade(2, 4000, 0, millis()+1001, millis() + 2000);   
   }
goTime = millis() + nextTime;
}

Try this:-

#include "Tlc5940.h"
#include "tlc_fades.h"

int stage = 0;
int maxstage = 3;

void setup(){
Tlc.init();
}

void loop(){
if (!tlc_isFading(0)) functionGo();
tlc_updateFades();
Tlc.update();

}
void functionGo(){
    stage += 1;
    if(stage > maxstage) stage = 0;
      switch(stage) {
        case 0:
               tlc_addFade(0, 0, 4000, millis(), millis() + 1000);
               break;
        case 1:       
               tlc_addFade(0, 4000, 0, millis(), millis() + 1000);
               break;
        case 2:
              tlc_addFade(0, 0, 4000, millis(), millis() + 3000);
              break;
        case 3:
              tlc_addFade(0, 4000, 0, millis(), millis() + 3000);
              break;      
        }
}

Grumpy_Mike: Try this:-

#include "Tlc5940.h"
#include "tlc_fades.h"

int stage = 0; int maxstage = 3;

void setup(){ Tlc.init(); }

void loop(){ if (!tlc_isFading(0)) functionGo(); tlc_updateFades(); Tlc.update();

} void functionGo(){     stage += 1;     if(stage > maxstage) stage = 0;       switch(stage) {         case 0:               tlc_addFade(0, 0, 4000, millis(), millis() + 1000);               break;         case 1:     
              tlc_addFade(0, 4000, 0, millis(), millis() + 1000);               break;         case 2:               tlc_addFade(0, 0, 4000, millis(), millis() + 3000);               break;         case 3:               tlc_addFade(0, 4000, 0, millis(), millis() + 3000);               break;              } }

I feel really bad I never checked back here; I hope you accept my apology Grumpy_Mike. I got it working, started messing with it, then my college work started to overwhelm me a bit, and I quickly forgot about my project.

I uploaded your code and from what I see it works the way as designed with a small flaw--when I uploaded it, the LED starts at an immediate fade down command, but other than that it seems fine. If I get more than one color to work I will post the code. Also under case 2 and 3, if I change the channel number and the time to 1000ms, what is happening is that both channels will turn on full brightness, green will fade down, then the blue follows.

With my code, I can copy and paste the if else statements only if I want 2 colors that fade at the same time for each channel. If I wanted to add a third color, would your code be more efficient at it?

Not sure where you want three colours. If it is in the fade then just replace the component that is currently zero with a colour value.