[SOLVED] Arduino Uno & TLC5490

I have an arduino connected to a TLC5490 currently controlling a row of 4 rbg leds. I’m utilizing the TLC5490 library found at http://playground.arduino.cc/learning/TLC5940. I’m trying to program a single led running pattern through the 4 leds. I’m finding though that it skips the first iteration of the void loop. I left a serial output in the code to monitor the counter steps and a delay of 5000ms to give enough time to confirm with the serial and the lit led. The variable counter should initialize at 0 (the serial output confirms this) and then step up by 1 after each loop. Even though counter outputs 0 it isn’t tripping the setLedColor1 function to trigger the led on the 1st iteration but it will on the 5th when it goes back to the first led. I did try to utilize a for statement but I had the same results. I could really use some help with this one cause I’m really stumped. Thank you to all in advance.

int counter;
#include "Tlc5940.h"

void setup()
{
  Tlc.init();
  Serial.begin(9600);
}
     
void loop()
{  
  Serial.println(counter);
  
  if (counter == 0)
  {
    setLedColor1(4095, 0, 0);
  }
  else
  {
    setLedColor1(0, 0, 0);
  }
  
  if (counter == 1)
  {
    setLedColor2(4095, 0, 0);
  }
  else
  {
    setLedColor2(0, 0, 0);
  }
  
  if (counter == 2)
  {
    setLedColor3(4095, 0, 0);
  }
  else
  {
    setLedColor3(0, 0, 0);
  }
  
  if (counter == 3)
  {
    setLedColor4(4095, 0, 0);
  }
  else
  {
    setLedColor4(0, 0, 0);
  }
  
  Tlc.update();
  delay(5000);
  
  if (counter == 3)
  {
    counter = 0;
  }
  else
  {
    counter = counter + 1;
  }
}

void setLedColor1(int redPin1, int greenPin1, int bluePin1)
{
  Tlc.set(0, redPin1);
  Tlc.set(1, greenPin1);
  Tlc.set(2, bluePin1);
}

void setLedColor2(int redPin2, int greenPin2, int bluePin2)
{
  Tlc.set(3, redPin2);
  Tlc.set(4, greenPin2);
  Tlc.set(5, bluePin2);
}

void setLedColor3(int redPin3, int greenPin3, int bluePin3)
{
  Tlc.set(6, redPin3);
  Tlc.set(7, greenPin3);
  Tlc.set(8, bluePin3);
}

void setLedColor4(int redPin4, int greenPin4, int bluePin4)
{
  Tlc.set(9, redPin4);
  Tlc.set(10, greenPin4);
  Tlc.set(11, bluePin4);
}

Tlc.update(); fires the change. Call it whenever you change an LED to push the change. I see you were doing it, but Have you looked at the sample code provided with the library? Do the examples work?

Some other things:

1- Counter is used before it is initialized with a value. The compiler may assign 0 to new ints, but it is not required. I think it is assigning a value here since you weren’t getting a compile error about the null pointer.

2- #defines and #includes should be the first lines in a file, unless there is a good reason to not put them there.

3- A for loop would simplify your code a lot. (See below. The example has not been run through a compiler, and may need some tweaks for functionality.)

    #include "Tlc5940.h"

    bool turnLightsOn = true;

    void setup()
    {
      Tlc.init();
      Serial.begin(9600);
    }
         
    void loop()
    {      
     for (int led = 0; led <= 3; led++){
          Serial.println(led);
          if (turnLightsOn) {
               setLedColor(led, 4095, 0, 0);
               Tlc.update();
          } else {
               setLedColor(led, 0, 0, 0);
               Tlc.update();
          }
         delay(5000);
      }
    turnLightsOn = !turnLightsOn;
    }

    void setLedColor(int led, int redPin1, int greenPin1, int bluePin1)
    {
      int firstPin = led * 3;
      Tlc.set(firstPin, redPin1);
      Tlc.set(firstPin + 1, greenPin1);
      Tlc.set(firstPin + 2, bluePin1);
    }

I’ve gone through all of the examples and they work fine. I used the examples to base my code on. I was unaware of having #define and #include as the first lines in the file. I changed that with no new results. I should have mentioned that I did try a for statement but had the same results. I went back to the if statements just to have it more expanded and so I could maybe determine the problem. I did run your code and the problem persists. The first led will not light until the second one is lit. I have programmed some successful programs and not been met with this problem. Here is a copy of a program that randomly selects a color out of 7 possibilities for the 4 leds.

int randomLedColor1;
int randomLedColor2;
int randomLedColor3;
int randomLedColor4;
int lastRandomLedColor1;
int lastRandomLedColor2;
int lastRandomLedColor3;
int lastRandomLedColor4;
#include "Tlc5940.h"
#include "tlc_config.h"

void setup()
{
  Tlc.init();
}
     
void loop()
{
  do
  {
    randomLedColor1 = random(7);
    randomLedColor2 = random(7);
    randomLedColor3 = random(7);
    randomLedColor4 = random(7);
  } while(randomLedColor1 == randomLedColor2 || randomLedColor1 == randomLedColor3 || randomLedColor1 == randomLedColor4 || randomLedColor2 == randomLedColor3 || randomLedColor2 == randomLedColor4 || randomLedColor3 == randomLedColor4 || randomLedColor1 == lastRandomLedColor1 || randomLedColor2 == lastRandomLedColor2 || randomLedColor3 == lastRandomLedColor3 || randomLedColor4 == lastRandomLedColor4);
    
  lastRandomLedColor1 = randomLedColor1;
  lastRandomLedColor2 = randomLedColor2;
  lastRandomLedColor3 = randomLedColor3;
  lastRandomLedColor4 = randomLedColor4;
  
  switch (randomLedColor1)
  {
    case 0: //red
      setLedColor1(4095, 0, 0);
      break;
    case 1: //green
      setLedColor1(0, 4095, 0);
      break;
    case 2: //blue
      setLedColor1(0, 0, 4095);
      break;
    case 3: //yellow
      setLedColor1(4095, 4095, 0);
      break;
    case 4: //cyan
      setLedColor1(0, 4095, 4095);
      break;
    case 5: //magenta
      setLedColor1(4095, 0, 4095);
      break;
    case 6: //orange
      setLedColor1(4095, 1023, 0);
  }
  
  switch (randomLedColor2)
  {
    case 0: //red
      setLedColor2(4095, 0, 0);
      break;
    case 1: //green
      setLedColor2(0, 4095, 0);
      break;
    case 2: //blue
      setLedColor2(0, 0, 4095);
      break;
    case 3: //yellow
      setLedColor2(4095, 4095, 0);
      break;
    case 4: //cyan
      setLedColor2(0, 4095, 4095);
      break;
    case 5: //magenta
      setLedColor2(4095, 0, 4095);
      break;
    case 6: //orange
      setLedColor2(4095, 1023, 0);
  }
  
  switch (randomLedColor3)
  {
    case 0: //red
      setLedColor3(4095, 0, 0);
      break;
    case 1: //green
      setLedColor3(0, 4095, 0);
      break;
    case 2: //blue
      setLedColor3(0, 0, 4095);
      break;
    case 3: //yellow
      setLedColor3(4095, 4095, 0);
      break;
    case 4: //cyan
      setLedColor3(0, 4095, 4095);
      break;
    case 5: //magenta
      setLedColor3(4095, 0, 4095);
      break;
    case 6: //orange
      setLedColor3(4095, 1023, 0);
  }
  
  switch (randomLedColor4)
  {
    case 0: //red
      setLedColor4(4095, 0, 0);
      break;
    case 1: //green
      setLedColor4(0, 4095, 0);
      break;
    case 2: //blue
      setLedColor4(0, 0, 4095);
      break;
    case 3: //yellow
      setLedColor4(4095, 4095, 0);
      break;
    case 4: //cyan
      setLedColor4(0, 4095, 4095);
      break;
    case 5: //magenta
      setLedColor4(4095, 0, 4095);
      break;
    case 6: //orange
      setLedColor4(4095, 1023, 0);
  }
  
  Tlc.update();
  delay(5000);
}

void setLedColor1(int redPin1, int greenPin1, int bluePin1)
{
  Tlc.set(0, redPin1);
  Tlc.set(1, greenPin1);
  Tlc.set(2, bluePin1);
}

void setLedColor2(int redPin2, int greenPin2, int bluePin2)
{
  Tlc.set(3, redPin2);
  Tlc.set(4, greenPin2);
  Tlc.set(5, bluePin2);
}

void setLedColor3(int redPin3, int greenPin3, int bluePin3)
{
  Tlc.set(6, redPin3);
  Tlc.set(7, greenPin3);
  Tlc.set(8, bluePin3);
}

void setLedColor4(int redPin4, int greenPin4, int bluePin4)
{
  Tlc.set(9, redPin4);
  Tlc.set(10, greenPin4);
  Tlc.set(11, bluePin4);
}

The only thing I can think of that is different is that I’m assigning an initial value with the random function before the the leds are set. So I’ve tried assigning 0 as a value for the counter in the begin of the program and then setting it to 1 at the begin of the void loop. Still nothing. Here is a copy of that.

int counter = 0;
#include "Tlc5940.h"

void setup()
{
  Tlc.init();
  Serial.begin(9600);
}
     
void loop()
{  
  
  if (counter == 4)
  {
    counter = 1;
  }
  else
  {
    counter = counter + 1;
  }
  
  Serial.println(counter);
  
  if (counter == 1)
  {
    setLedColor1(4095, 0, 0);
  }
  else
  {
    setLedColor1(0, 0, 0);
  }
  
  if (counter == 2)
  {
    setLedColor2(4095, 0, 0);
  }
  else
  {
    setLedColor2(0, 0, 0);
  }
  
  if (counter == 3)
  {
    setLedColor3(4095, 0, 0);
  }
  else
  {
    setLedColor3(0, 0, 0);
  }
  
  if (counter == 4)
  {
    setLedColor4(4095, 0, 0);
  }
  else
  {
    setLedColor4(0, 0, 0);
  }
  
  Tlc.update();
  delay(5000);
}

void setLedColor1(int redPin1, int greenPin1, int bluePin1)
{
  Tlc.set(0, redPin1);
  Tlc.set(1, greenPin1);
  Tlc.set(2, bluePin1);
}

void setLedColor2(int redPin2, int greenPin2, int bluePin2)
{
  Tlc.set(3, redPin2);
  Tlc.set(4, greenPin2);
  Tlc.set(5, bluePin2);
}

void setLedColor3(int redPin3, int greenPin3, int bluePin3)
{
  Tlc.set(6, redPin3);
  Tlc.set(7, greenPin3);
  Tlc.set(8, bluePin3);
}

void setLedColor4(int redPin4, int greenPin4, int bluePin4)
{
  Tlc.set(9, redPin4);
  Tlc.set(10, greenPin4);
  Tlc.set(11, bluePin4);
}

Try setting the colours, to 0, and call Tlc.update() in the setup() before entering the color loop.

It might be an initialization problem.

I wasn’t really sure on how to go about setting the initials colors to 0 in the setup(). I read the documentation of the library several times found here http://alex.kathack.com/codes/tlc5940arduino/html_r014/. They are rather confusing so what I’ve done could be complete wrong. Here is a copy of the code I tried where the initial values are set to 0.

int counter;
#include "Tlc5940.h"

void setup()
{
  Tlc.init();
  Tlc.setAll(0);
  Tlc.update();
  Serial.begin(9600);
}
     
void loop()
{  
  Serial.println(counter);
  
  if (counter == 0)
  {
    setLedColor1(4095, 0, 0);
  }
  else
  {
    setLedColor1(0, 0, 0);
  }
  
  if (counter == 1)
  {
    setLedColor2(4095, 0, 0);
  }
  else
  {
    setLedColor2(0, 0, 0);
  }
  
  if (counter == 2)
  {
    setLedColor3(4095, 0, 0);
  }
  else
  {
    setLedColor3(0, 0, 0);
  }
  
  if (counter == 3)
  {
    setLedColor4(4095, 0, 0);
  }
  else
  {
    setLedColor4(0, 0, 0);
  }
  
  Tlc.update();
  delay(5000);
  
  if (counter == 3)
  {
    counter = 0;
  }
  else
  {
    counter = counter + 1;
  }
}

void setLedColor1(int redPin1, int greenPin1, int bluePin1)
{
  Tlc.set(0, redPin1);
  Tlc.set(1, greenPin1);
  Tlc.set(2, bluePin1);
}

void setLedColor2(int redPin2, int greenPin2, int bluePin2)
{
  Tlc.set(3, redPin2);
  Tlc.set(4, greenPin2);
  Tlc.set(5, bluePin2);
}

void setLedColor3(int redPin3, int greenPin3, int bluePin3)
{
  Tlc.set(6, redPin3);
  Tlc.set(7, greenPin3);
  Tlc.set(8, bluePin3);
}

void setLedColor4(int redPin4, int greenPin4, int bluePin4)
{
  Tlc.set(9, redPin4);
  Tlc.set(10, greenPin4);
  Tlc.set(11, bluePin4);
}

This still did not resolve the problem.

I opened a new thread on Arduino’s forum to gather some more input on this problem. I figured I’d post a link to this discussion for anyone else who is encountering this problem. Hopefully between these two great websites there will be plenty of information available to generate an answer.

http://forum.arduino.cc/index.php?topic=171008.0

Well it took awhile but I have found a solution to the problem. I can’t believe how simple the answer was this whole time. It would seem that the TLC5940 library needs a second to initialize. By simply adding a delay of 1ms to the beginning of the loop() solves everything. Here is a copy of the corrected code for those who need it.

#include "Tlc5940.h"
#include "tlc_config.h"
int counter;

void setup()
{
  Tlc.init();
}
     
void loop()
{  
  delay(1);
  
  if (counter == 0)
  {
    setLedColor1(4095, 0, 0);
  }
  else
  {
    setLedColor1(0, 0, 0);
  }
  
  if (counter == 1)
  {
    setLedColor2(4095, 0, 0);
  }
  else
  {
    setLedColor2(0, 0, 0);
  }
  
  if (counter == 2)
  {
    setLedColor3(4095, 0, 0);
  }
  else
  {
    setLedColor3(0, 0, 0);
  }
  
  if (counter == 3)
  {
    setLedColor4(4095, 0, 0);
  }
  else
  {
    setLedColor4(0, 0, 0);
  }
  
  Tlc.update();
  delay(100);
  
  if (counter == 3)
  {
    counter = 0;
  }
  else
  {
    counter = counter + 1;
  }
}

void setLedColor1(int redPin1, int greenPin1, int bluePin1)
{
  Tlc.set(0, redPin1);
  Tlc.set(1, greenPin1);
  Tlc.set(2, bluePin1);
}

void setLedColor2(int redPin2, int greenPin2, int bluePin2)
{
  Tlc.set(3, redPin2);
  Tlc.set(4, greenPin2);
  Tlc.set(5, bluePin2);
}

void setLedColor3(int redPin3, int greenPin3, int bluePin3)
{
  Tlc.set(6, redPin3);
  Tlc.set(7, greenPin3);
  Tlc.set(8, bluePin3);
}

void setLedColor4(int redPin4, int greenPin4, int bluePin4)
{
  Tlc.set(9, redPin4);
  Tlc.set(10, greenPin4);
  Tlc.set(11, bluePin4);
}

I hope this will help any others who are currently fighting this problem now or in the future. Thank you again to everyone who took the time to review this problem and suggest solutions.