Simple code for fading leds… but not quite right

hey, i have some simple code that should be working, but i can’t quite see what is wrong with it.

i have an attiny85 working with a tlc5940, and testing is fine, i can choose led 1-16, and pick a brightness for the led.

the library uses the format tlc5940.set (led, brightness) to set the specific led.

in the code i use 2 arrays for storing the current brightness, and choosing the specific led.

void positioni is a routine to light the leds from 0 to currentLed (for ease of this 4), so leds 0,1,2,3 and 4 are fully lit (4095)

void fadeTop reduces the brightness of all the leds from currentLed (4) to the end of the leds (7), by a factor of fade (100). the pwm relies on NOT using any delays, so this fade factor is what i am using to change the speed of the fade.

if i change the currentLed from (4) to (2), then i would expect fadeTop to change its base number and then start the fade out, but this doesn’t happen.

presently in this code, i have disabled the micInput, and is running directly from the sensitivity potentiometer, this is purely for sanity’s sake. the micinput is working correctly when not disabled.

the bulk of the code is structured inside a timed loop, so that the mic input is controllable

i seem to think that the error occurs in this line;
tlc5940.set(ledArrayA_, ledBrightA*-=fade);_
but no changes seem to affect.
thanks!
_
```_
#include “Tiny85_TLC5940.h”

// version 3.4
// adding tlc5940
// fades not working

int  latchPin = PB1;
int  clockPin = PB3;
int  dataPin  = PB0;
int  micPin  = A2;
int  sensPin  = A3;

int  micInput = 0;

byte  ledArrayA[8] = {0, 1, 2, 3, 4, 5, 6, 8};
unsigned int  ledBrightA[8] = {0, 0, 0, 0, 0, 0, 0, 0};

byte  ledArrayB[8] = {9, 10, 11, 12, 13, 14, 15, 16};
unsigned int  ledBrightB[8] = {0, 0, 0, 0, 0, 0, 0, 0};

int  bright=4095;
int  currentLed = 0;
int  fade = 100;

unsigned long previousTime = 0;
const long    interval = 25;

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT); 
  pinMode(clockPin, OUTPUT);
  pinMode(micPin, INPUT);
  pinMode(sensPin, INPUT);
 
  tlc5940.init();
  tlc5940.clear();
} // end of setup

void loop() {
  unsigned long currentTime = millis();

if (currentTime - previousTime >= interval) {

//  micInput = map(analogRead(micPin), map(analogRead(sensPin), 0, 1023, 0, 1023), 1023, -1, 7);
  micInput = map(analogRead(sensPin), 0, 1023, -1, 7);

currentLed = micInput;

positioni();
  fadeTop();
//  trailFade();

previousTime = currentTime;
  }

tlc5940.update();

} // end of loop

void positioni() {
    for (int i =-1; i<=currentLed; i++) {   
      tlc5940.set (ledArrayA[i], ledBrightA[i]=4095);
//      tlc5940.set (ledArrayB[i], ledBrightB[i]+=600);
      }
    }

void fadeTop(){
    for (int i=currentLed; i<=7; i++) {
      tlc5940.set(ledArrayA[i], ledBrightA[i]-=fade);
//      tlc5940.set(ledArrayB[i], ledBrightB[i]+=fade);
    }
}

void trailFade(){
    for (int i=currentLed; i<=7; i++) {
      tlc5940.set (i, bright);
      bright = bright/2;
      } 
    }
_
```*_

for (int i =-1; i<=currentLed; i++) {     
       tlc5940.set (ledArrayA[i], ledBrightA[i]=4095);
...

What memory do you think you mess up with in LedArrayA and ledBrightA when i is -1?

Map function is might be a little unclear. See discussion http://forum.arduino.cc/index.php?topic=417690.0

  micInput = map(analogRead(sensPin), 0, 1023, -1, 7);

Better look like (also preventing faulty -1 in the array)

  micInput = map(analogRead(sensPin), 0, 1024, 0, 8);

ah, yeah… that was my off switch for a different chip, before i was using arrays, i can see how that would cause problems.

fixed, but to no real change.

Waht happens if you split your fade in 2 lines?

You are also operating on currentLed in both functions. Positioni sets it to 4095 and fadeTop fades it before it sets to 4095 again.

void positioni() {
     for (int i =-1; i<=currentLed; i++) {     
       tlc5940.set (ledArrayA[i], ledBrightA[i]=4095);
//       tlc5940.set (ledArrayB[i], ledBrightB[i]+=600);
       }

to

void positioni() {
     for (int i =0; i<=currentLed; i++) {     
       ledBrightA[i]=4095;
       tlc5940.set (ledArrayA[i], ledBrightA[i]);
//       tlc5940.set (ledArrayB[i], ledBrightB[i]+=600);
       }

and

void fadeTop(){
    for (int i=currentLed; i<=7; i++) {
      tlc5940.set(ledArrayA[i], ledBrightA[i]-=fade);
//      tlc5940.set(ledArrayB[i], ledBrightB[i]+=fade);
    }

to

void fadeTop(){
    for (int i=currentLed; i<=7; i++) {
      ledBrightA[i]-=fade;
      tlc5940.set(ledArrayA[i], ledBrightA[i]);
//      tlc5940.set(ledArrayB[i], ledBrightB[i]+=fade);
    }

no change at all

for every iteration of i, it chooses the led, then sets it to 4095

by moving it, it sets the brightness to 4095, then it chooses the led, then it sets it to 4095.

this is how i originally had the code, but then changed it, and no matter which way around you do it its the same outcome, however this way looks cleaner.

edit… maybe this was the issue… after adding the led checks it all appears to be working. thanks!

FIXED!!

im not 100% what i tweaked… i changed all the -1’s, and although this code looks a little different, i just deleted some variables that weren’t needed and removed all the redundant code.

i added an if statement to the fade in and fade out to check to see if the led was on / off to stop a roll over
and added a fadein / fadeout variable to have the speeds of the fade different to each other

#include "Tiny85_TLC5940.h"

// version 3.5
// adding tlc5940 

int   latchPin = PB1;
int   clockPin = PB3;
int   dataPin  = PB0;
int   micPin   = A2;
int   sensPin  = A3;

int   micInput = 0;

byte   ledArrayA[8] = {0, 1, 2, 3, 4, 5, 6, 7};
int   ledBrightA[8] = {0, 0, 0, 0, 0, 0, 0, 0};

byte   ledArrayB[8] = {9, 10, 11, 12, 13, 14, 15, 16};
int   ledBrightB[8] = {0, 0, 0, 0, 0, 0, 0, 0};

int   fadeOut = 500;
int   fadeIn = 1000;

unsigned long previousTime = 0;
const long    interval = 25;

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  pinMode(micPin, INPUT);
  pinMode(sensPin, INPUT);
  
  tlc5940.init();
  tlc5940.clear();
} // end of setup
 
void loop() {
  unsigned long currentTime = millis();

  if (currentTime - previousTime >= interval) {

//  micInput = map(analogRead(micPin), map(analogRead(sensPin), 0, 1023, 0, 1023), 1023, 0, 8);
  micInput = map(analogRead(sensPin), 0, 1024, 0, 8);

  positioni();
  fadeTop();

previousTime = currentTime;
  }

  tlc5940.update();

} // end of loop


void positioni() {
    for (int i =0; i<=micInput; i++) {
      if (ledBrightA[i]>=(4094-fadeIn)) { ledBrightA[i]=4095; } else { ledBrightA[i]+=fadeIn; }
      if (ledBrightB[i]>=(4094-fadeIn)) { ledBrightB[i]=4095; } else { ledBrightB[i]+=fadeIn; }
        tlc5940.set(ledArrayA[i], ledBrightA[i]);
        tlc5940.set(ledArrayB[i], ledBrightB[i]);
       }
     } 


void fadeTop(){
    for (int i=micInput; i<=8; i++) {
      if (ledBrightA[i]<=(1+fadeOut)) { ledBrightA[i]=0; } else { ledBrightA[i]-=fadeOut; }
      if (ledBrightB[i]<=(1+fadeOut)) { ledBrightB[i]=0; } else { ledBrightB[i]-=fadeOut; }
        tlc5940.set(ledArrayA[i], ledBrightA[i]);
        tlc5940.set(ledArrayB[i], ledBrightB[i]);
    }
}

When you say

if i change the currentLed from (4) to (2), then i would expect fadeTop to change its base number and then start the fade out, but this doesn't happen.

do you actually change in the code or use the potentiometer mapping? How Are you sure you are setting from 4 to 2?

I assume You understand you are not fading but just alternating/blinking super quickly between 4095 and 4095-100 for the LEDs bright value. Is 25ms a sufficient enough delay for you to see something - you probably just see an averaged value of the brightness between the two... And is 4095 and 4095-100 sufficiently different to say led 1,2,3,4 are in one way and the other different?

kelvinmead:
FIXED!!

im not 100% what i tweaked… i changed all the -1’s, and although this code looks a little different, i just deleted some variables that weren’t needed and removed all the redundant code.

i added an if statement to the fade in and fade out to check to see if the led was on / off to stop a roll over
and added a fadein / fadeout variable to have the speeds of the fade different to each other

#include "Tiny85_TLC5940.h"

// version 3.5
// adding tlc5940

int  latchPin = PB1;
int  clockPin = PB3;
int  dataPin  = PB0;
int  micPin  = A2;
int  sensPin  = A3;

int  micInput = 0;

byte  ledArrayA[8] = {0, 1, 2, 3, 4, 5, 6, 7};
int  ledBrightA[8] = {0, 0, 0, 0, 0, 0, 0, 0};

byte  ledArrayB[8] = {9, 10, 11, 12, 13, 14, 15, 16};
int  ledBrightB[8] = {0, 0, 0, 0, 0, 0, 0, 0};

int  fadeOut = 500;
int  fadeIn = 1000;

unsigned long previousTime = 0;
const long    interval = 25;

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT); 
  pinMode(clockPin, OUTPUT);
  pinMode(micPin, INPUT);
  pinMode(sensPin, INPUT);
 
  tlc5940.init();
  tlc5940.clear();
} // end of setup

void loop() {
  unsigned long currentTime = millis();

if (currentTime - previousTime >= interval) {

//  micInput = map(analogRead(micPin), map(analogRead(sensPin), 0, 1023, 0, 1023), 1023, 0, 8);
  micInput = map(analogRead(sensPin), 0, 1024, 0, 8);

positioni();
  fadeTop();

previousTime = currentTime;
  }

tlc5940.update();

} // end of loop

void positioni() {
    for (int i =0; i<=micInput; i++) {
      if (ledBrightA[i]>=(4094-fadeIn)) { ledBrightA[i]=4095; } else { ledBrightA[i]+=fadeIn; }
      if (ledBrightB[i]>=(4094-fadeIn)) { ledBrightB[i]=4095; } else { ledBrightB[i]+=fadeIn; }
        tlc5940.set(ledArrayA[i], ledBrightA[i]);
        tlc5940.set(ledArrayB[i], ledBrightB[i]);
      }
    }

void fadeTop(){
    for (int i=micInput; i<=8; i++) {
      if (ledBrightA[i]<=(1+fadeOut)) { ledBrightA[i]=0; } else { ledBrightA[i]-=fadeOut; }
      if (ledBrightB[i]<=(1+fadeOut)) { ledBrightB[i]=0; } else { ledBrightB[i]-=fadeOut; }
        tlc5940.set(ledArrayA[i], ledBrightA[i]);
        tlc5940.set(ledArrayB[i], ledBrightB[i]);
    }
}

What you fixed while I was typing the previous answer is now that the value does move and is not reset every time to 4095 and alternating.