TLC 5940 + Arduino mini pro fading issue

As this is my first post around here: Hello everybody :slight_smile:

I started a project using an Arduino mini pro, a TLC5940, 5 RGB LED’s and an accelerometer (ADXL3xx) + tilt-switch as input:

I want to display different patterns with the LED’s depending on the orientation of the sensors (up, left, right, down). I use the X-axis of the accelerometer to get the orientation and the tilt-switch to distinguish between +90° and -90°rotation (right / left).

So far, it works fine, but I tried to get a fade-in effect for every time the orientation changes. This works only the FIRST time, e.g. when i power the Arduino. After that, the LED’s do not fade anymore. Here is my code:

//import TLC libraries
#include "Tlc5940.h"
#include "tlc_fades.h" 



//RGB LED channels
int channelR1 = 2;
int channelG1 = 0;
int channelB1 = 1;

int channelR2 = 5;
int channelG2 = 3;
int channelB2 = 4;

int channelR3 = 8;
int channelG3 = 6;
int channelB3 = 7;

int channelR4 = 11;
int channelG4 = 9;
int channelB4 = 10;

int channelR5 = 14;
int channelG5 = 12;
int channelB5 = 13;


//Accelerometer (measures rotation in Y-axis)
int sensorPin = A0;
int sensorValue = 0;
int outputValue = 0;

//Tilt-sensor (measures left vs. right oriantation)
const int tiltPin = 2;

//integers for smoothing the tilt-switch
int tiltState;
int lastTiltState;
long lastDebounceTime = 0;
long debounceDelay = 20;

void setup()
{
  Serial.begin(9600);
  //define tiltPin (pin2) as INPUT_PULLUP (only ground needed)
  pinMode(tiltPin, INPUT_PULLUP);
  // start communication with TLC chip
  Tlc.init();
}

void loop()
{
  // read and map accelerometer input to 180° rotation
  sensorValue = analogRead(sensorPin);
  outputValue = map(sensorValue, 396, 624, 0, 180);
  // some code to filter noise from the tilt-switch
  int reading = digitalRead(tiltPin);
  if (reading != lastTiltState){
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    tiltState = reading;
  }

  // code to display the different patterns depending on the orientation

  // UP
  if (outputValue > -10 && outputValue < 45) {
    Tlc.clear();
    tlc_removeFades(channelG1);
    tlc_addFade (channelG1,0,500,0,3000);
    tlc_updateFades();
    Tlc.update();
    Serial.println ("UP");
  } 


  // LEFT
  else if (outputValue > 45 && outputValue < 135 && tiltState == HIGH) {
    Tlc.clear();
    tlc_removeFades(channelG2);
    tlc_addFade (channelG2,0,500,0,3000);
    tlc_updateFades();
    Tlc.update();
    Serial.println ("LEFT");
  } 


  // RIGHT
  else if (outputValue > 45 && outputValue < 135 && tiltState == LOW) {
    Tlc.clear();
    tlc_removeFades(channelG3);
    tlc_addFade (channelG3,0,500,0,3000);
    tlc_updateFades();
    Tlc.update();
    Serial.println ("RIGHT"); 
  } 


  // DOWN
  else if (outputValue > 135) {
    Tlc.clear();
    tlc_removeFades(channelG4);
    tlc_addFade (channelG4,0,500,0,3000);
    tlc_updateFades();
    Tlc.update();
    Serial.println ("DOWN");
  } 

  lastTiltState = reading;

}

My Arduino skills are not very advanced, thus I appreciate any help!!

Does it still send the serial stuff after the first time?

Yes, it runs through all the if-loops and sends the right serial.println…The only thing that stops working is the fade-in effect.

I tried another version of the code, without the addFade command. Now the fade-in effect does not stop working, but it fades to 500 then goes back to 0 and starts fading to 500 again (almost like a blinking effect) How do I get it doing this only fading in once and then staying at 500 until the outputValue changes?

  // UP
  if (outputValue > -10 && outputValue < 45) {
    Tlc.clear();
    orientation = 1;
    for (int i=0; i<500; i=i+10){
      Tlc.set(channelG1, i);
      Tlc.update();
      delay(50);
    }
  }

I'll dig around and find my tlc5940s and experiment with all of that fading stuff. And what exactly do you want to happen with your LEDs in each position?

Ok I found out whats wrong with the original code.

Heres Your code:

// UP
  if (outputValue > -10 && outputValue < 45) {
    Tlc.clear();
    tlc_removeFades(channelG1);
    tlc_addFade (channelG1,0,500,0,3000);
    tlc_updateFades();
    Tlc.update();
    Serial.println ("UP");
  }

Heres the syntax for the addFade command:

tlc_addFade(channel, start value, end value , startMillis, endMillis);

You are misunderstanding the meaning of start millis and end millis. These are milliseconds since the program began, not since you executed the command. And also the tlc.update line is unnecessary and might cause issues.

Here is something that should work:

// UP
if (outputValue > -10 && outputValue < 45) {
Tlc.clear();
tlc_removeFades(channelG1);
uint32_t startMillis = millis() + 10; // The 10 is added so the processer has time to do calculations and stuff before executing the add fade command
uint32_t endMillis = startMillis + 3000; // the 3000 is your fade duration in milliseconds, so 3 seconds
tlc_addFade (channelG1,0,500,startMillis, endMillis);
tlc_updateFades();
Serial.println (“UP”);
}

thanks for your help!! i tried your code, but it unfortunately does not work like i hoped for..(It does fade the LED in, but only after I rotate the accelerometer to another position. After it faded to 500 it turns off)

what i try to do is the following:

i have 5 RGB LED's that show different 'patterns' (for example 3 LED show a certain color and 2 show another one) depending on the orientation of the accelerometer input.

If I rotate the accelerometer so that "UP" is true (a value between -10 and 45) I want the respective pattern to fade in and stay until the accelerometer is rotated again to another position.

Well it turns off because at every condition you call Tlc.clear();