TLC5940 and individual LED control w/the Uno

by jetjaguar on Tue Mar 19, 2013 8:10 pm

Hello everyone!

I'm still quite new to arduino and electronics in general, so please bear with me. I'm working on a project with over a hundred LEDs using an Arduino Uno and many TLC5940's. I know many, many novices have started posts like this with overly ambitious LED projects, but this one is actually very simple - because only a couple LEDs will ever be on at the same time. All I need to figure out at this point is how to individually control each LEDs so that they are either either on, off, or fading on/off at different times.

For the life of me I haven't been able to accomplish this. I'm using the library from Sparkfun's website. My setup is exactly like the schematic here: http://tronixstuff.wordpress.com/2010/07/19/review-%E2%80%93-texas-instruments-tlc5940-16-channel-led-driver%C2%A0ic/ .

The problem I'm running into is that the LED's do not turn off when they should, in a somewhat unpredictable manner. If I run the code below, the first two LED's WILL turn off, but the remaining four will stay dimly lit. If I play with the timings then different ones will remain dimly lit. Something basic must be very wrong or missing...

Code: Select all | TOGGLE FULL SIZE
#include "Tlc5940.h" //led driver
#include "tlc_fades.h"

void  setup ()
{

  Tlc.init ();
 
  {
    tlc_addFade (0, 0, 500, 1000, 3000); tlc_addFade (0, 500, 0, 2750, 3000);
    tlc_addFade (1, 0, 500, 1000, 3000); tlc_addFade (1, 500, 0, 2750, 3000);
    tlc_addFade (2, 0, 500, 1000, 3000); tlc_addFade (2, 500, 0, 2750, 4000);
    tlc_addFade (3, 0, 500, 1000, 3000); tlc_addFade (3, 500, 0, 2750, 4000);
    tlc_addFade (4, 0, 500, 1000, 3000); tlc_addFade (4, 500, 0, 2750, 4000);
    tlc_addFade (5, 0, 500, 1000, 3000); tlc_addFade (5, 500, 0, 2750, 4000);
  } 
 
}

void  loop ()
{
 tlc_updateFades ();
}


Thank you in advance!
jetjaguar
 
Posts: 8
Joined: Thu Jun 14, 2012 10:21 pm

Re: TLC5940 and individual LED control w/the Uno

by jetjaguar on Sat Mar 23, 2013 1:09 pm

Going off of the site site below, I'll try buying some decoupling capacitors:

http://arduino.cc/forum/index.php?PHPSESSID=2ad6483bc4f6fa78406acd64a2406621&topic=134953.0
jetjaguar
 
Posts: 8
Joined: Thu Jun 14, 2012 10:21 pm

Re: TLC5940 and individual LED control w/the Uno

by adafruit_support_mike on Sat Mar 23, 2013 2:52 pm

I'm not familiar with the library, but having read the thread listed above, your fade parameters look a bit wonky..

From what I can tell, tlc_addFade()'s paramaters run like so:

Code: Select all | TOGGLE FULL SIZE
tlc_addFade( channel, startValue, endValue, startMillis, endMillis )

//  channel     : the LED you want to fade
//  startValue  : the output value at the start of the fade
//  endValue    : the output value at the end of the fade
//  startMillis : when to start the fade (based on the Arduino's millis() value)
//  endFade     : when to end the fade


and based on that description, this line from your code:

Code: Select all | TOGGLE FULL SIZE
    tlc_addFade (0, 0, 500, 1000, 3000); tlc_addFade (0, 500, 0, 2750, 3000);


creates one fade that runs from time=1000 to time=3000, then creates another fade for the same LED that runs from time=2750 to time=3000.

Those time spans overlap. From time=2750 to time=3000, you have two different 'fades' trying to control the same LED. I can see that causing all sorts of problems.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 8715
Joined: Thu Feb 11, 2010 1:51 pm

Re: TLC5940 and individual LED control w/the Uno

by jetjaguar on Sat Mar 23, 2013 5:01 pm

Thank you for the reply mystone.

I changed the code so that each led ended with ...3000,4000); and that worked just fine, they all turned completely off.

However, when I changed the code to this:

Code: Select all | TOGGLE FULL SIZE
    tlc_addFade (0, 0, 500, 1000, 3000); tlc_addFade (0, 500, 0, 3000, 3000);
    tlc_addFade (1, 0, 500, 1000, 3000); tlc_addFade (1, 500, 0, 3000, 3200);
    tlc_addFade (2, 0, 500, 1000, 3000); tlc_addFade (2, 500, 0, 3000, 3400);
    tlc_addFade (3, 0, 500, 1000, 3000); tlc_addFade (3, 500, 0, 3000, 3600);
    tlc_addFade (4, 0, 500, 1000, 3000); tlc_addFade (4, 500, 0, 3000, 3800);
    tlc_addFade (5, 0, 500, 1000, 3000); tlc_addFade (5, 500, 0, 3000, 4000);


The last one, LED 5, would stay dimly lit. When I added another line: "tlc_addFade (6, 0, 500, 1000, 3000); tlc_addFade (6, 500, 0, 3000, 4000); ", then LED 5 went off, but the new LED, number 6, stayed dimly lit.

I have also added a 0.1uF cap between the power and gnd pins on the driver.
jetjaguar
 
Posts: 8
Joined: Thu Jun 14, 2012 10:21 pm

Re: TLC5940 and individual LED control w/the Uno

by jetjaguar on Sat Mar 23, 2013 8:15 pm

Everything seems to work if the last tlc_fade line is something like this:

Code: Select all | TOGGLE FULL SIZE
...
tlc_addFade (5, 0, 500, 1000, 3000); tlc_addFade (5, 500, 0, 3000, 4000);
tlc_addFade (5, 0, 0, 4001, 4001);


Does that make sense to anyone?

I also had to increase the buffer length in the tlc_fades.h file from 24 to 35 in order to get all 16 LEDs to light up.
jetjaguar
 
Posts: 8
Joined: Thu Jun 14, 2012 10:21 pm

Re: TLC5940 and individual LED control w/the Uno

by adafruit_support_mike on Sun Mar 24, 2013 9:35 am

I just looked at the library.. can't say it left a good impression. The author does some things that work but are stylistically improper, like putting all the code in the header files.

The part of the library that handles fades uses what's called a 'naive' algorithm: it performs a simple set of operations blindly, and leaves it to the user to make sure nothing will go wrong. There's nothing wrong with that, but it can trip someone who's still getting used to the code.

I think your best option is to avoid any time overlaps at all.. instead of

Code: Select all | TOGGLE FULL SIZE
tlc_addFade (5, 0, 500, 1000, 3000); tlc_addFade (5, 500, 0, 3000, 4000);


try keeping the intervals completely separate:

Code: Select all | TOGGLE FULL SIZE
tlc_addFade (5, 0, 500, 1000, 3000); tlc_addFade (5, 500, 0, 3001, 4000);


Having two fades for the same channel that start and end during the same pass through the library's "what should I do now?" loop probably creates situations with wonky side effects.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 8715
Joined: Thu Feb 11, 2010 1:51 pm

Re: TLC5940 and individual LED control w/the Uno

by jetjaguar on Sun Mar 24, 2013 2:03 pm

Thank you again mstone, but doing as you suggest does not help...

I'm going to try to find another library. As cool and popular as these chips seem to be, I'm surprised that I'm having so much trouble finding good examples of their use.
jetjaguar
 
Posts: 8
Joined: Thu Jun 14, 2012 10:21 pm

Re: TLC5940 and individual LED control w/the Uno

by jetjaguar on Sun Mar 24, 2013 4:05 pm

Adding "Tlc.update();" after "tlc_updateFades ();" in the loop looks like it may have taken care of my problems.
jetjaguar
 
Posts: 8
Joined: Thu Jun 14, 2012 10:21 pm

Re: TLC5940 and individual LED control w/the Uno

by adafruit_support_mike on Sun Mar 24, 2013 5:26 pm

Good to hear you found something that worked.

The library uses a combination of interrupts and callback functions to make the LEDs blink, and it's hard to trace the sequence of operations just from reading the code.

jetjaguar wrote:I'm going to try to find another library. As cool and popular as these chips seem to be, I'm surprised that I'm having so much trouble finding good examples of their use.

That's one of the risks with Open Source software.. the quality of a piece of code depends on the number of people who've read and understood it, not on the number of people who use it.

I remember Back In The Old Days when perl coders were adamant that anyone writing code for a webserver should use a package called 'CGI.pm'.. 278k of the most appalling spaghetti code you can imagine, maintained by a single guy who was working desperately to keep it running for every oddball server configuration in the world. The size and complexity of the mess was a testament to his brilliance, but it wasn't until some relatively big names started saying, "don't say one more word about that package until you've actually opened the file and read the code" that people backed off enough to let a good library develop.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 8715
Joined: Thu Feb 11, 2010 1:51 pm

Re: TLC5940 and individual LED control w/the Uno

by solaron99 on Fri May 10, 2013 10:36 am

Hi everyone,

very informative post regarding fades.h. I would like to loop a fading LED.

The sketch below fades in successfully but does not fade out.

I added Tlc.update(); after tlc_updateFades(); as per your suggestion but does not fade in and fade out in a loop.

Any ideas?
Thank you.


#include "Tlc5940.h" //led driver
#include "tlc_fades.h"

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

void loop ()
{

tlc_addFade (12, 0, 400, 0000, 2000); tlc_addFade (12, 400, 0, 3000, 5000);

tlc_updateFades();

// Tlc.update();
}
solaron99
 
Posts: 4
Joined: Wed Jan 09, 2013 8:33 pm

Re: TLC5940 and individual LED control w/the Uno

by adafruit_support_mike on Fri May 10, 2013 4:50 pm

You need to move the addFade() calls out of the loop() function and into the setup() function.

The loop() function executes over and over again. As soon as control leaves the bottom of the function, it comes right back in at the top. The result is that you're calling addFade() over and over with the same values, probably pushing the oldest ones out of memory before you can see any results from them.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 8715
Joined: Thu Feb 11, 2010 1:51 pm

Re: TLC5940 and individual LED control w/the Uno

by solaron99 on Mon May 13, 2013 10:28 am

Hi Mike,

perhaps I should've been more clear.

Yes, indeed the Led fades in and fades out one cycle only when inside the
setup function. However, the led turns of and remains off.

I simply want it to loop continuously...breathe.

I added Tlc.update(); after tlc_updateFades(); as per suggestion but still does not
fade in and fade out in a loop.

As simple as this sounds, how do I get this simple sketch to continuously fade in and fade out in a loop using the fades.h library?

Thank you.

#include "Tlc5940.h" //led driver
#include "tlc_fades.h"

void setup ()
{
Tlc.init ();

tlc_addFade(12, 0, 400, 0000, 1000); tlc_addFade(12, 400, 0, 2000, 4000);
}

void loop ()
{

tlc_updateFades();
}
solaron99
 
Posts: 4
Joined: Wed Jan 09, 2013 8:33 pm

Re: TLC5940 and individual LED control w/the Uno

by adafruit_support_mike on Mon May 13, 2013 11:29 am

The TLC library isn't our code so I'm really not familiar with it, but IIRC, it registers the fades with start and end times. You'll need to write your main loop so it keeps track of the time and registers a new fade periodically. The first rising fade will run from 0-2000ms, the first falling fade will run from 2001-4000, the second rising fade will run from 4001-6000, the second falling fade will run from 6001-8000, etc.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 8715
Joined: Thu Feb 11, 2010 1:51 pm