I am new to arduinos and any type of programming but I've been working on a project requiring me to fade 4x16 LEDs off of one arduino (atmega 328). I've played around with the tlc fade code and everything works well unless multiple sets of LEDs are triggered simultaneously. Each "pod" of LEDs goes through two waves of fade up and a fade down after the sensor has been triggered with an exception to stop an existing wave if the sensor is triggered during a previous pulse. I have included my code which I've tried to comment to the best of my abilities. Any advice on coding or my specific problem would be much appreciated. Thanks.
PS. This code shows only two tlcs but i will ultimately be using four.
Code:
#include "Tlc5940.h" //TLC library general
#include "tlc_fades.h" //TLC library fades
int calibrationTime = 10; //time in seconds for PIR to calibrate
int counter1 = 0; //counter is the variable for the specific pin on the TLC5940
int counter2 = 0; //counter is the variable for the specific pin on the TLC5940
uint16_t LEDSpeed1 = 200; //time between each LED
uint16_t LEDSpeed2 = 1000; //time between each LED
uint16_t duration1 = 1600; //duration
uint16_t duration2 = 30000; //duration
uint16_t maxValue = 1000;
uint16_t midValue = 10; //as counter gets higher max brightness decreases
boolean isFading1 = LOW;
boolean isFading2 = LOW;
const int irInput1 = 6; //pin for PIR 1
int irState1 = 0;
const int irInput2 = 4; //pin for PIR 2
int irState2 = 0;
const int sensorDelay = 8000; //time in milliseconds after sensor is toggled high before it will take another reading
uint32_t currentTime1 = 0; //time of the last PIR toggled HIGH
uint32_t currentTime2 = 0; //time of the last PIR toggled HIGH
TLC_CHANNEL_TYPE channel; //TLC code for defining channel
void setup()
{
Serial.begin(9600);
Tlc.clear();
Tlc.init();
pinMode(irInput1, INPUT);
pinMode(irInput2, INPUT);
for(int i = 0; i < calibrationTime; i++){
delay(1000);
}
}
void loop()
{
Tlc.clear(); //clear pins
isFading1 = LOW; //set channel to zero
isFading2 = LOW;
//sample sensor
if (millis()>(currentTime1+sensorDelay)){ //confirm it has been a long enough wait for PIR to take another reading
irState1 = digitalRead(irInput1); //read PIR
}
if (millis()>(currentTime2+sensorDelay)){ //confirm it has been a long enough wait for PIR to take another reading
irState2 = digitalRead(irInput2); //read PIR
}
// fadeout
if (tlc_fadeBufferSize < TLC_FADE_BUFFER_LENGTH - 2) {
if (irState1 == HIGH){ //if pin is high stop the fades and fade all LEDs out before starting waves
counter1 = 0;
currentTime1 = millis();
Tlc.clear();
tlc_updateFades();
for (int i = 0; i < 16; i++){ //cycles through channels 0 - 15
uint16_t stopValue = Tlc.get(i); //find the current channel value
tlc_removeFades(i); //take fade off of that channel
tlc_updateFades(); //update
Tlc.set(i, stopValue); //set the channel to previous stop value
uint16_t duration = (stopValue + 1)*2; //set fade duration to stop value plus one so there are no zero duration fades
uint32_t startMillis = millis(); //start fade at current time
uint32_t endMillis = startMillis + (duration); //end fade at current time plus duration
tlc_addFade(i, stopValue, 0, startMillis, endMillis); //take fade from previous stop value to zero for the specified duration
tlc_updateFades(); //update
irState1 = LOW; //reset irState1 LOW
}
counter1++;
}
}
if (tlc_fadeBufferSize < TLC_FADE_BUFFER_LENGTH - 2) {
if (irState2 == HIGH){ //if pin is high stop the fades and fade all LEDs out before starting waves
counter2 = 0;
currentTime2 = millis();
Tlc.clear();
tlc_updateFades();
for (int i = 16; i < 32; i++){
uint16_t stopValue = Tlc.get(i);
tlc_removeFades(i);
tlc_updateFades();
Tlc.set(i, stopValue);
uint16_t duration = (stopValue + 1)*2;
uint32_t startMillis = millis();
uint32_t endMillis = startMillis + (duration);
tlc_addFade(i, stopValue, 0, startMillis, endMillis);
tlc_updateFades();
irState2 = LOW;
}
counter2++;
}
}
// i s F a d i n g
for (int i = 0; i < 16; i++){
if (tlc_isFading(i) == 1){
isFading1 = HIGH;
break;
}
}
for (int i = 16; i < 32; i++){
if (tlc_isFading(i) == 1){
isFading2 = HIGH;
break;
}
}
// w a v e 1
if (tlc_fadeBufferSize < TLC_FADE_BUFFER_LENGTH - 2) {
if ((counter1 == 1)&&(irState1 == LOW)&&(isFading1 == LOW)){
Tlc.clear();
for (int i = 0; i < 16; i++){ //from channel 0 - 15
uint32_t startMillis1 = millis() + ((LEDSpeed1*(i+1))); //start time is a function of LEDspeed constant, channel and how many waves have occured
uint32_t endMillis1 = startMillis1 + (duration1); //end time is start time plus duration
tlc_addFade(i, 0, maxValue, startMillis1, endMillis1); //add Fade up
tlc_addFade(i, maxValue, midValue, endMillis1, endMillis1 + (duration1*4)); //add fade down
tlc_addFade(i, midValue, (maxValue*.5), endMillis1 + (duration1*4), (endMillis1 + (duration1*4))+duration1); //add Fade up
tlc_addFade(i, (maxValue*.5), 0, (endMillis1 + (duration1*4))+duration1, (endMillis1 + (duration1*4))+duration1 + (duration2)); //add fade down
tlc_updateFades(); //update
irState1 = LOW; //set IRstate low
}
Tlc.clear();
tlc_updateFades();
counter1++;
}
}
if (tlc_fadeBufferSize < TLC_FADE_BUFFER_LENGTH - 2) {
if ((counter2 == 1)&&(irState2 == LOW)&&(isFading2 == LOW)){
// Tlc.clear();
for (int i = 16; i < 32; i++){
uint32_t startMillis1 = millis() + ((LEDSpeed1*(i-15)));
uint32_t endMillis1 = startMillis1 + (duration1);
tlc_addFade(i, 0, maxValue, startMillis1, endMillis1);
tlc_addFade(i, maxValue, midValue, endMillis1, endMillis1 + (duration1*4));
tlc_addFade(i, midValue, (maxValue*.5), endMillis1 + (duration1*4), (endMillis1 + (duration1*4))+duration1);
tlc_addFade(i, (maxValue*.5), 0, (endMillis1 + (duration1*4))+duration1, (endMillis1 + (duration1*4))+duration1 + (duration2));
tlc_updateFades();
irState2 = LOW;
}
Tlc.clear();
tlc_updateFades();
counter2++;
}
}
Tlc.clear();
tlc_updateFades();
}
#include "tlc_fades.h" //TLC library fades
int calibrationTime = 10; //time in seconds for PIR to calibrate
int counter1 = 0; //counter is the variable for the specific pin on the TLC5940
int counter2 = 0; //counter is the variable for the specific pin on the TLC5940
uint16_t LEDSpeed1 = 200; //time between each LED
uint16_t LEDSpeed2 = 1000; //time between each LED
uint16_t duration1 = 1600; //duration
uint16_t duration2 = 30000; //duration
uint16_t maxValue = 1000;
uint16_t midValue = 10; //as counter gets higher max brightness decreases
boolean isFading1 = LOW;
boolean isFading2 = LOW;
const int irInput1 = 6; //pin for PIR 1
int irState1 = 0;
const int irInput2 = 4; //pin for PIR 2
int irState2 = 0;
const int sensorDelay = 8000; //time in milliseconds after sensor is toggled high before it will take another reading
uint32_t currentTime1 = 0; //time of the last PIR toggled HIGH
uint32_t currentTime2 = 0; //time of the last PIR toggled HIGH
TLC_CHANNEL_TYPE channel; //TLC code for defining channel
void setup()
{
Serial.begin(9600);
Tlc.clear();
Tlc.init();
pinMode(irInput1, INPUT);
pinMode(irInput2, INPUT);
for(int i = 0; i < calibrationTime; i++){
delay(1000);
}
}
void loop()
{
Tlc.clear(); //clear pins
isFading1 = LOW; //set channel to zero
isFading2 = LOW;
//sample sensor
if (millis()>(currentTime1+sensorDelay)){ //confirm it has been a long enough wait for PIR to take another reading
irState1 = digitalRead(irInput1); //read PIR
}
if (millis()>(currentTime2+sensorDelay)){ //confirm it has been a long enough wait for PIR to take another reading
irState2 = digitalRead(irInput2); //read PIR
}
// fadeout
if (tlc_fadeBufferSize < TLC_FADE_BUFFER_LENGTH - 2) {
if (irState1 == HIGH){ //if pin is high stop the fades and fade all LEDs out before starting waves
counter1 = 0;
currentTime1 = millis();
Tlc.clear();
tlc_updateFades();
for (int i = 0; i < 16; i++){ //cycles through channels 0 - 15
uint16_t stopValue = Tlc.get(i); //find the current channel value
tlc_removeFades(i); //take fade off of that channel
tlc_updateFades(); //update
Tlc.set(i, stopValue); //set the channel to previous stop value
uint16_t duration = (stopValue + 1)*2; //set fade duration to stop value plus one so there are no zero duration fades
uint32_t startMillis = millis(); //start fade at current time
uint32_t endMillis = startMillis + (duration); //end fade at current time plus duration
tlc_addFade(i, stopValue, 0, startMillis, endMillis); //take fade from previous stop value to zero for the specified duration
tlc_updateFades(); //update
irState1 = LOW; //reset irState1 LOW
}
counter1++;
}
}
if (tlc_fadeBufferSize < TLC_FADE_BUFFER_LENGTH - 2) {
if (irState2 == HIGH){ //if pin is high stop the fades and fade all LEDs out before starting waves
counter2 = 0;
currentTime2 = millis();
Tlc.clear();
tlc_updateFades();
for (int i = 16; i < 32; i++){
uint16_t stopValue = Tlc.get(i);
tlc_removeFades(i);
tlc_updateFades();
Tlc.set(i, stopValue);
uint16_t duration = (stopValue + 1)*2;
uint32_t startMillis = millis();
uint32_t endMillis = startMillis + (duration);
tlc_addFade(i, stopValue, 0, startMillis, endMillis);
tlc_updateFades();
irState2 = LOW;
}
counter2++;
}
}
// i s F a d i n g
for (int i = 0; i < 16; i++){
if (tlc_isFading(i) == 1){
isFading1 = HIGH;
break;
}
}
for (int i = 16; i < 32; i++){
if (tlc_isFading(i) == 1){
isFading2 = HIGH;
break;
}
}
// w a v e 1
if (tlc_fadeBufferSize < TLC_FADE_BUFFER_LENGTH - 2) {
if ((counter1 == 1)&&(irState1 == LOW)&&(isFading1 == LOW)){
Tlc.clear();
for (int i = 0; i < 16; i++){ //from channel 0 - 15
uint32_t startMillis1 = millis() + ((LEDSpeed1*(i+1))); //start time is a function of LEDspeed constant, channel and how many waves have occured
uint32_t endMillis1 = startMillis1 + (duration1); //end time is start time plus duration
tlc_addFade(i, 0, maxValue, startMillis1, endMillis1); //add Fade up
tlc_addFade(i, maxValue, midValue, endMillis1, endMillis1 + (duration1*4)); //add fade down
tlc_addFade(i, midValue, (maxValue*.5), endMillis1 + (duration1*4), (endMillis1 + (duration1*4))+duration1); //add Fade up
tlc_addFade(i, (maxValue*.5), 0, (endMillis1 + (duration1*4))+duration1, (endMillis1 + (duration1*4))+duration1 + (duration2)); //add fade down
tlc_updateFades(); //update
irState1 = LOW; //set IRstate low
}
Tlc.clear();
tlc_updateFades();
counter1++;
}
}
if (tlc_fadeBufferSize < TLC_FADE_BUFFER_LENGTH - 2) {
if ((counter2 == 1)&&(irState2 == LOW)&&(isFading2 == LOW)){
// Tlc.clear();
for (int i = 16; i < 32; i++){
uint32_t startMillis1 = millis() + ((LEDSpeed1*(i-15)));
uint32_t endMillis1 = startMillis1 + (duration1);
tlc_addFade(i, 0, maxValue, startMillis1, endMillis1);
tlc_addFade(i, maxValue, midValue, endMillis1, endMillis1 + (duration1*4));
tlc_addFade(i, midValue, (maxValue*.5), endMillis1 + (duration1*4), (endMillis1 + (duration1*4))+duration1);
tlc_addFade(i, (maxValue*.5), 0, (endMillis1 + (duration1*4))+duration1, (endMillis1 + (duration1*4))+duration1 + (duration2));
tlc_updateFades();
irState2 = LOW;
}
Tlc.clear();
tlc_updateFades();
counter2++;
}
}
Tlc.clear();
tlc_updateFades();
}