Led Driver problems with ALL leds

Hey folks. I have a code here that i’d like someone to help me with. First, here is a picture of what I’m working with. It is a custom pcb that I designed … see attachments
What this does is the black colored photodiode in the center receives infrared light from the 4 clear leds off of an object( hand, notebook, whatever you want, etc.) and when your hand is over it the 16 blue leds turn on. This works no problem. The thing I have a problem with is that I do NOT want the lights to turn off as soon as your hand is no longer of the photodiode. I want there to be a delay for a much better effect. Here is the code that works for 1 LED ONLY! If I try to do the same thing with more than this one led, ( or any 1 led at a time) then the lights just stay on forever as soon as your hand is over it.

#include "Tlc5940.h"

int Threshold;
long DetectTime;

  void setup()
    {
      Tlc.init();
      Threshold = analogRead(A5);
    }

  void loop()
    {
      int NewThreshold = (Threshold - 7);
      int ReceiverVal = analogRead(A5);
      int Value;
      
      if(ReceiverVal < NewThreshold)
           {
             DetectTime = millis();
             {
                    Tlc.set(0,1000);
                    Tlc.update();
                 }
             }
     
      if((millis() - DetectTime) > 500)
           {
                 
              {
                Tlc.set(0,0);
                Tlc.update();
               }   
            }
  }

For instance, if I try…

Tlc.set(0,1000);
Tlc.set(1,1000);
Tlc.update();

That does not work any more. The 2 lights remain on.

Any suggestions would be great!! Thanks

Can you post the code that doesn't work?

This might be relevant, from the library write-up:

If data has already been shifted in this grayscale cycle, another call to update() will immediately return 1 without shifting in the new data. To ensure that a call to update() does shift in new data, use

 while(Tlc.update());

here is a code that does NOT work

#include "Tlc5940.h"

int Threshold;
long DetectTime;

  void setup()
    {
      Tlc.init();
      Threshold = analogRead(A5);
    }

  void loop()
    {
      int NewThreshold = (Threshold - 7);
      int ReceiverVal = analogRead(A5);
      int Value;
      
      if(ReceiverVal < NewThreshold)
           {
             DetectTime = millis();
             {
               
               for(int i=0; i<16; i++)
               {
                    Tlc.set(i,1000);
                    Tlc.update();
               }
                 }
             }
     
      if((millis() - DetectTime) > 500)
           {
                 
              {
                for(int j=0; j<16; j++)
                {
                Tlc.set(j,0);
                Tlc.update();
                }
               }   
            }
  }

Where would I insert while(Tlc.update()); in my code? Thanks

It seems to be saying keep calling Tlc.update until it returns zero, so something like this:

#include "Tlc5940.h"

int Threshold;
long DetectTime;

void setup()
{
  Tlc.init();
  Threshold = analogRead(A5);
}

void loop()
{
  int NewThreshold = (Threshold - 7);
  int ReceiverVal = analogRead(A5);
  int Value;

  if(ReceiverVal < NewThreshold)
  {
    DetectTime = millis();

    for(int i=0; i<16; i++)
    {
      Tlc.set(i,1000);
      while(Tlc.update()); 
    }
  }

  if((millis() - DetectTime) > 500)
  {

    for(int j=0; j<16; j++)
    {
      Tlc.set(j,0);
      while(Tlc.update()); 
    }
  }
}

That works once and a while but is very spotty. It will use the delay once or twice, but after a few times the lights will stay on forever again. So hopefully this could be fixed with code, otherwise I will just have to put up with the lights turning off as soon as my hand is removed. ( Not bad I just want everything ). Any idea on how to make it work better/every single time?

DetectTime should really be unsigned long, not long.

Apart from that, hard to say without seeing the hardware. Maybe the code has bugs, maybe the library. Hard to tell.

Shouldn’t there be an “else” there?

 if(ReceiverVal < NewThreshold)
  {
    DetectTime = millis();

    for(int i=0; i<16; i++)
    {
      Tlc.set(i,1000);
      while(Tlc.update()); 
    }
  }
  else
    if((millis() - DetectTime) > 500)
    {

      for(int j=0; j<16; j++)
      {
        Tlc.set(j,0);
        while(Tlc.update()); 
      }
    }

Try some debugging prints to the serial port. That might clarify what is happening.