LED Star field TLC5940 Coding Issues (newb)

Hi Guys

I am working towards making a star-field ceiling installation, using white LEDS and TLC5940 LED Drivers

I’ve breadboarded one chip with 16 leds to get an idea of how it will all go together and work out the code. I’m getting some weird results and I’m also very new to programming, so the code is confusing me a bit.

I’ve used alternating blue and white LEDs to make it easier to see what’s going on. I’m feeding it 7.5v through the arduino’s ext power. This is how it’s wired:

I have been playing around with example sketches and have hacked together the following sketch using the tlc5940 arduino library…

#include "Tlc5940.h"
void setup()
{
 /* Call Tlc.init() to setup the tlc.
 You can optionally pass an initial PWM value 
 (0 - 4095) for all channels.*/
 Tlc.init();
}

void randomled(int m)
// displays random LEDs for m times
{
 randomSeed(analogRead(0));
 for (int qq=0; qq<m; qq++)
 {
 Tlc.clear();
 Tlc.set(random(16), 1095);
 Tlc.update();
 delay(200);
 }
}

void loop()
{
 randomled(100);
}

This works to a degree, but oddly it lights up a blue & white led together at the same time. So it’s like the code is treating every 2 leds as a pair and turning them on together. This happens at random intervals, as it should, but always shows the 2 leds next to each other each time. A video might explain better:

So apart from working out why it’s lighting the LEDs in pairs, I also need to make the whole thing look like twinkling stars. I thought the ‘Breathing’ routine would work best. However, being new to programming, I just don’t know where to start. There are examples of the breathing routine out there, but I can’t work out how to get each random LED to ‘breath’ on and off. The whole installation will use around 128 LEDs and I’m thinking, to make it look more ‘natural’, it will need to light more than one LED at a time. So would I just have, say, 4 separate circuits? all running at the same time. This way there would be an overlap in LEDs that are on/off.

Hope some of this makes sense. I do want to learn this stuff for myself, so happy to get pointers in the right direction. If I need to give any more info, let me know.

According to the library code:

To ensure that a call to update() does shift in new data, use

    while(Tlc.update());

Hi

Thanks for taking the time to reply. However, I'm still not clear on how to proceed. I've changed code to include the while statement around Tlc.update(); It compiles and uploads, but does not change anything. I'm still not clear why it's lighting 2 LEDs at once and then how to make them randomly 'breath' instead of just flash.

Thanks

It looks wrong to me to seed the random number generator every time through the loop. Just do it once.

Try doing an update after the clear.

If two LEDs chase each other around, it looks like you are setting two on but I can't see how from your sketch.

Try manually doing all 16 values (0 to 15) and see if one (like zero) happens to cause that.

[quote author=Nick Gammon link=topic=176171.msg1307332#msg1307332 date=1373236085] According to the library code:

To ensure that a call to update() does shift in new data, use

    while(Tlc.update());

[/quote]

Yes, but the "delay(200)" after the update makes that moot.

He only does it once for every 100 LEDs;

Yep. I don’t see any way you can get that video from that sketch. Something is missing.

For starters, no LED should stay lit for more than one 1/5th of a second…

Hey

So based on your replies, I decided to look at the wiring. Instead of troubleshooting each connection, I stripped it all off the breadboard and rewired from scratch. I can now confirm that it is working correctly and each individual LED is lighting instead of in pairs.

Thanks for your help…

So now on to the 2nd part of my question. How do I make the LEDs ‘breath’ when they are lit, instead of just turn on and off?

The ultimate goal here is to make a very large star field installation on a ceiling, using over 100 LEDs

IntelliTom:
Hey

So based on your replies, I decided to look at the wiring. Instead of troubleshooting each connection, I stripped it all off the breadboard and rewired from scratch. I can now confirm that it is working correctly and each individual LED is lighting instead of in pairs.

Thanks for your help…

So now on to the 2nd part of my question. How do I make the LEDs ‘breath’ when they are lit, instead of just turn on and off?

The ultimate goal here is to make a very large star field installation on a ceiling, using over 100 LEDs

There’s an example called “fades” in the TLC library.

Hi

I appreciate your help on this. However, I can’t seem to get what I need. The fades demo that comes with the TLC5940 library works, but does not give me the kind of ‘randomness’ that I need. It appears it is designed to ‘pulse’ in a certain direction. I could achieve what I wanted by physically wiring the leds in a random fashion. But I feel like I should be working out exactly how to get each LED to fade on and off at random intervals. Below is the code I am working on. ‘randomled’ is the correct random display of LEDs and ‘fadeleds’ gives me the right ‘breath’ action. But I can’t seem to work out how to combine the 2 actions.

#include "Tlc5940.h"
#include "tlc_fades.h"
TLC_CHANNEL_TYPE channel;
void setup()
{
 /* Call Tlc.init() to setup the tlc.
 You can optionally pass an initial PWM value 
 (0 - 4095) for all channels.*/
 Tlc.init();
}

void fadeleds()
{
  if (tlc_fadeBufferSize < TLC_FADE_BUFFER_LENGTH - 4) {
    if (!tlc_isFading(channel)) {
      uint16_t duration = analogRead(0) * 4;
      int maxValue = analogRead(0) * 4;
      uint32_t startMillis = millis() + 50;
      uint32_t endMillis = startMillis + duration;
      tlc_addFade(channel, 0, maxValue, startMillis, endMillis);
      tlc_addFade(channel, maxValue, 0, endMillis, endMillis + duration);
    }
    if (channel++ == NUM_TLCS * 16) {
      channel = 0;
    }
  }
  tlc_updateFades();
}

void randomled(int m)
// displays random LEDs for m times
{
 randomSeed(analogRead(0));
 for (int qq=0; qq<m; qq++)
 {
 Tlc.clear();
 Tlc.set(random(16), 3895);
 while(Tlc.update());
 delay(500);
 }
}

void loop()
{

  
  
  
}

I’ve been staring at this for hours. There must be a simple way to get the random command to trigger the fade action. I think it is because I do not fully understand how the fade code works.

Stop trying to be clever with "randomSeed(analogRead(0));" .

It probably doesn't do what you think, take it out.

Hi, thanks. But I think you over estimate my abilities. My knowledge of code is lacking. My code examples are hacked together from examples in the TLC5940 examples.

At any rate, the randomseed function is not the problem. I can't work out how to combine the two functions to achieve my goal.

IntelliTom: Hi, thanks. But I think you over estimate my abilities. My knowledge of code is lacking. My code examples are hacked together from examples in the TLC5940 examples.

At any rate, the randomseed function is not the problem. I can't work out how to combine the two functions to achieve my goal.

The bit you want is:

      uint32_t startMillis = millis() + 50;
      uint32_t endMillis = startMillis + duration;
      tlc_addFade(channel, 0, maxValue, startMillis, endMillis);
      tlc_addFade(channel, maxValue, 0, endMillis, endMillis + duration);

But you need random channel and duration.

I have an LED strip from Adafruit that does some fancy things with addressable LEDs. Judging by the code and its comments they set up the field initially in memory (RAM) and then shift the lot out in bulk.

So your problem really is to set up an array (eg. 128 bytes) for your 128 LEDs, and then "populate" it with the random pattern you desire (or any pattern). For example, you might start at the start, skip a random number of bytes (say, 1 to 4) and set that one to "on", skip some more bytes, etc.

Then after that just call a function to turn on all of the LEDs that correspond to the "on" entries in the array.

Thanks Guys. I have managed to get it working using this code:

#include "Tlc5940.h"
#include "tlc_fades.h"
TLC_CHANNEL_TYPE channel;

int l;

void setup()
{
  Tlc.init();
  randomSeed(analogRead(0));
}

void randomled()
{
  if (tlc_fadeBufferSize < TLC_FADE_BUFFER_LENGTH - 14) {
    if (!tlc_isFading(channel)) {
      uint16_t duration = analogRead(0) * 8;

      uint32_t startMillis = millis() + 70;
      uint32_t endMillis = startMillis + duration;
      tlc_addFade(l, 33, 4096, startMillis, endMillis);          // fade in
      tlc_addFade(l, 4096, 33, endMillis, endMillis + duration); // fade out
    }
    if (l++ == NUM_TLCS * 16) {
      l = 0;
    }
  }
  tlc_updateFades();
}

void loop()
{
l = random(0,16);
randomled();
}

I’m still not 100% sure on exactly how it works, but it seems to replicate ‘Twinkling Stars’. The next step is to add another TLC5940 and another 16 LEDs and see how it works.