I have a code that checks an IR sensor for detection ( such as a hand over the sensor). When a hand is sensed the leds turn on. After the hand is moved , the code waits .5 seconds and then the leds turn off. This works but it only works smoothly for 1 or 2 leds attached to the TLC5940 at most. Once I tell it to do the exact same for more leds, the stop turning off after .5 seconds and just stay on forever. Why would this be?
Here is the code with just 1 led....
#include "Tlc5940.h"
int Threshold;
unsigned long DetectTime;
void setup()
{
Tlc.init();
Threshold = analogRead(A0);
}
void loop()
{
int NewThreshold = (Threshold - 7);
int ReceiverVal = analogRead(A0);
int Value;
if(ReceiverVal < NewThreshold)
{
DetectTime = millis();
Tlc.set(0,500);
Tlc.update();
}
if((millis() - DetectTime) > 500)
{
Tlc.set(0,0);
Tlc.update();
}
}
But if I add more Tlc.set() to this code, the leds stay on forever. Why would that matter?
Try adding a Serial.println() to monitor ReceiverVal. You want to determine if the problem is with the input from the trigger or with the output to the Tlc.
Quick question about decoupling though.... Since from a TLC5940 there are 2 connections to each the ground and + 5v, which do I add the de-coupling capacitors to? Or does it not matter? Also, in the link for de-coupling, it gives suggestions for the sizes of capacitors.... 47 microF and .1 microF. Are these going to be fine or are there other values that will work better? Thanks.
p.s. A simple diagram to show the caps + TLC would help please
OK so the de-coupling does not appear to be working, unless I have the 2 caps set up incorrectly. I just have a 47 micro and a .1 micro each connected from ground to + 5V. What is weird about this is the delay does work perfectly but only if I set a value to each led up to about 20. Which means they are very dim. Any value greater than that and the leds just stay on forever after I wave my hand over the sensor.
Again, here is the code...
#include "Tlc5940.h"
int Threshold;
unsigned long DetectTime;
void setup()
{
Tlc.init();
Threshold = analogRead(A0);
}
void loop()
{
int NewThreshold = (Threshold - 7);
int ReceiverVal = analogRead(A0);
int Value;
if(ReceiverVal < NewThreshold)
{
DetectTime = millis();
Value = 20;
for(int i =0; i<16; i++)
Tlc.set(i,Value);
Tlc.update();
}
if((millis() - DetectTime) > 500)
{
Value = 0;
for(int i =0; i<16; i++)
Tlc.set(i,Value);
Tlc.update();
}
}
Have you tried monitoring the RecieverVal yet? My bet is that your trigger, which is an optical device, is getting interference from the lights being on. I'm not sure how you have things set up, but you might try completely shielding your trigger from your lights with a piece of cardboard or something.
Since from a TLC5940 there are 2 connections to each the ground and + 5v
No there is only one connection on a TLC5940. Just put a 0.1uF capacitor between pins 21 and 22 as close to the chip as you can manage. It should be a ceramic capacitor with the leads as short as possible.
Just try controlling the LEDs without any IR sensor input. Does it work as expected, if not then try and fix that first. If it does then it is your sensors that are at fault.
The trick of getting a project to work is just to take one step at a time NOT two.
I've done a little investigating with Serial.println()... Here is an example of what I got...
The threshold value of the light in the room held at a VERY constant value between 980 and 981 which is good because there was no change in light. When I put my hand over the sensor the reading dropped dramatically. But I have folded about 10 pieces of computer paper 6 times and put that in between the sensor and the leds and also put a bunch of paper of the leds, so I dont believe that the actual light from the leds is what is causing the disruption. I think because I am outputting too much power that it is somehow affecting the value that the sensor takes in. Does this sound possible or am I losing it? I hope there is some sort of solution to this problem...
Maybe you could give us a sketch of your entire circuit (including all wires, resistors, de-coupling capacitors,etc. and where they are) and someone might be able to locate the issue. I think we've ruled out your code as the source of the problem.
I have a high powered IR led simply attached to a resistor, and to + 5V and ground
I also have an IR photodiode attached close to the IR led. it is negative to A0, and also negative to +5V, and positive to ground
I have 9 leds attached to the TLC, negative to a pin, positive to + 5V. Then I have 1 TLC attached exactly how it is in the example on TLC in arduino playgorund WITH a .1 micro F cap between 21 and 22.
Thats about it
If I take the delay out of the code completely it works perfectly for any number of leds, but when the delay sections are introduced it all goes to S#it after more than 2 or 3 leds.