PWM + Millis + TLC5940

Hello

I am trying to manually fade the channels on a TLC5940 to avoid using the addFade function in the TLC5940 library which is giving my troubles.

I can fade the channels using a delay.

When I use the BlinkWithoutDelay example as a reference it does not work properly.

Here is as far as I have got. The channels turn on and stay on with no increment.

#include "Tlc5940.h"
            
long previousMillis = 0;        
long interval = 100;           


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

void loop()
{
  
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) { 
    
    previousMillis = currentMillis;   

for(int fadeValue = 0 ; fadeValue <= 4095; fadeValue +=5) {
    
    Tlc.set(21, fadeValue);
    Tlc.set(22, fadeValue);    
        
    Tlc.update(); 
   
  }
 }
}

TLC5940 library: https://code.google.com/p/tlc5940arduino/

Read your code.
What it is doing is waiting for a time to pass. Then it is feeding the various fade values to the TLC very rapidly and then outputting just the last one.

What your code should do is when the time interval has passed setting only the next value to the TLC and then output it.

So you do not want a for loop at all. You still need a fade variable but you only increment it once per "blink"

I appreciate it Mike.

I changed this:

for(int fadeValue = 0 ; fadeValue <= 4095; fadeValue +=5)

into this:

if(fadeValue <= 4095, fadeValue +=5)

and made

int fadeValue = 0;

a variable in the beginning.

#include "Tlc5940.h"
            
long previousMillis = 0;        
long interval = 50;           
int fadeValue = 0;

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

void loop()
{
  
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) { 
    
    previousMillis = currentMillis;   

if(fadeValue <= 4095, fadeValue +=5) {
    
    Tlc.set(21, fadeValue);
    Tlc.set(22, fadeValue);    
        
    Tlc.update(); 
   
  }
 }
}

It works, is this the best way to program it?

Well it fades up once only but if that is what you want to do then it is fine.
You might want to change it by putting an else on the if and getting it to fade down. In that case you can no longer use the fade value to decide what direction to fade it but a seprate direction variable.
Anyway have a play with it and get comfortable with the concept.
We'll done.

Thanks Mike