For loop trigger for tlc5940

Hi everyone,
I’ve been playing with tlc5940 and trying to understand how to write software for it.
I’m trying to use for loop to fade leds.
For testing I did the following loop

#include "Tlc5940.h"
#include "tlc_fades.h"

void setup()
{

  Tlc.init(4095);
  Serial.begin(9600);

}

void loop()
{
  int start = 0;
  int finish = 1000;
  for (int led=0; led < 7; led++)
  {
    tlc_addFade(led ,0, 4095, start, finish);
    start = start + 200;
    finish = finish + start;
  }

   tlc_updateFades();
}

This works as expected and fades leds

No I want o start fading leds on trigger so I put the same loop into if condition

#include "Tlc5940.h"
#include "tlc_fades.h"

  int val = 1000;
  int go = 0;

void setup()
{
  Tlc.init(4095);
  Serial.begin(9600);
}
void loop()
{

  val=analogRead(0);
  Serial.println(val,DEC);
  delay(500);

  if (val <= 600 && go == 0 )
  {
    go = 1;
    int start = 0;
    int finish = 1000;
    for (int led=0; led < 7; led++)
    {
      tlc_addFade(led ,4095, 0, start, finish);
      start = start + 200;
      finish = finish + start;
    }
    tlc_updateFades();
  }
}

Now instead fading the leds just light up.
What am I doing wrong?

Thank you

What am I doing wrong?

Probably this:

if (val <= 600 && go == 0 )
  {
    go = 1;

Just a guess, but try moving tlc_updateFades() out of the if statement so it runs every time

    start = start + 200;
    finish = finish + start;

You might want to look at the compound operators, so you can at least look like a smart C coder:

    start += 200;
    finish += start;

AWOL:

What am I doing wrong?

Probably this:

if (val <= 600 && go == 0 )

{
    go = 1;

Not sure what is wrong here I change go to 1 because I want it to run once.

PaulS:

    start = start + 200;

finish = finish + start;



You might want to look at the compound operators, so you can at least look like a smart C coder:


start += 200;
    finish += start;

Thanks for the tip I’m not a coder so still learning :slight_smile:

wildbill:
Just a guess, but try moving tlc_updateFades() out of the if statement so it runs every time

I don’t need it to run every time.