Go Down

Topic: TLC5940 help, adding LEDs causes weird flashing (Read 11892 times) previous topic - next topic

nyg78

I started playing around with the TLC5940 with my Duemilanove.  I set it up exactly as indicated in the playground and used the decoupling capacitors.  I have two 5940s hooked up but I'm only using 24 LEDs total.  I tried the sample code and it works.  I'm working on my own patterns.  Here's where I ran into trouble.  I tried the following which works fine ("odd" numbers come on and "even" numbers are out, then "even" numbers come on and "odd" numbers go out, then repeat):
Code: [Select]


#include "Tlc5940.h"

void setup()
{
  /* Call Tlc.init() to setup the tlc.
     You can optionally pass an initial PWM value (0 - 4095) for all channels.*/
  Tlc.init();
}

void loop()
{
  Tlc.clear();
    Tlc.set(1, 4095);
    Tlc.set(3, 4095);
    Tlc.set(5, 4095);
    Tlc.set(7, 4095);
    Tlc.set(9, 4095);
    Tlc.set(11, 4095);
    delay(500);
    Tlc.update();
    Tlc.set(0, 4095);
    Tlc.set(1, 0);
    Tlc.set(2, 4095);
    Tlc.set(3, 0);
    Tlc.set(4, 4095);
    Tlc.set(5, 0);
    Tlc.set(6, 4095);
    Tlc.set(7, 0);
    Tlc.set(8, 4095);
    Tlc.set(9, 0);
    Tlc.set(10, 4095);
    Tlc.set(11, 0);
    Tlc.set(12, 4095);
    delay(500);
    Tlc.update();
}


When I add so much as one more LED, they all just blink very quickly at the same time, then go out, then blink quickly on again, etc.  Even adding an LED from the same TLC does this.  The chips seem to work fine because like I said, I've tried the sample code like the Knight Rider (basic animation) and all LEDs light up on both chips.  How can I fix this?

Also, I want to add a pot to change the flash speed.  I've used one before on another project but had one lug going to 5V on the Arduino board.  With the TLC hookup, that slot is taken.  Can I use a pot, and if I can, where would I hook up that lug that would normally go to 5V?

Thank you.

nyg78

#1
Jan 15, 2012, 09:04 pm Last Edit: Jan 15, 2012, 09:06 pm by nyg78 Reason: 1
I fixed the LED problem, now I just need help on adding the pot.  Since the pot and TLC setup both use the 5V input on the Arduino board, can I move one of those wires to another port?  If so, which port would I use?  Thank you.

PaulS

Quote
I fixed the LED problem, now I just need help on adding the pot.  Since the pot and TLC setup both use the 5V input on the Arduino board, can I move one of those wires to another port?

Multiple things can be connected to the 5V pin (not port - a port is a collection of pins).

It is up to you to figure out how to connect multiple things together to connect to the 5V pin. Are you using a breadboard? If so this should be trivial.
The art of getting good answers lies in asking good questions.

nyg78

So would I run the wire from the pot that would normally go to the 5V pin to the hot rail on the breadboard?  I'm a bit weary about damaging parts.  I'm using this exact setup by the way, with the addition of the decoupling capacitors:

http://tlc5940arduino.googlecode.com/svn/wiki/images/breadboard-arduino-tlc5940.png

Grumpy_Mike

Quote
So would I run the wire from the pot that would normally go to the 5V pin to the hot rail on the breadboard?

Yes

nyg78

Now I need help combining two sketches.  These are not mine, I borrowed them and modified them. 

sketch #1 twinkling leds:
Code: [Select]

/*
* random twinkling leds with speed control pot
*/

int potPin = 0;                 // input for the pot
int timer = 100;                // you can adjust the numbers below to set te max and min for the speed adjustment
int x = 10;                      // sets the max speed (0 = fast) the lower the number the faster it can go
int y = 300;                    // sets the min speed (100 = slow) the higher the number the slower it can go

#include "Tlc5940.h"

#define UP 1
#define DOWN 0
#define NUM_LEDS 23
#define TWINKLE_SPACING 6
#define MAX_BRIGHTNESS 4095

int led_brightness[NUM_LEDS];
int led_direction[NUM_LEDS];
int led_speed[NUM_LEDS];
int twinkle_spacing = TWINKLE_SPACING;
int min_speed;
int max_speed;

const int BLINK_MODE_TWINKLE = 1;


void setup()
{
min_speed = MAX_BRIGHTNESS / 20;
max_speed = MAX_BRIGHTNESS / 30;
if (min_speed < 1) {
   min_speed = 1;
}
if (max_speed < 3) {
   max_speed = 10;
}

int i = 0;
for (i = 0 ; i < 3 ; i++) {
   led_brightness[i] = 0;
   led_direction[i] = DOWN;
   led_speed[i] = min_speed;
}

Tlc.init();

randomSeed(analogRead(0));
}


void loop()
{
  for ( int i = 0; i < 10; i++ ) {
    timer += analogRead(0);               // reads the pot and sets the timer to the value
}
    timer = timer / 10;
    timer = map(timer, 0, 1023, x, y);   // maps the timer value to the max and min vaues set above with x and y 
{
  Tlc.clear();
    BLINK_MODE_TWINKLE:
      if (twinkle_spacing == 0) {
        random_led();
        twinkle_spacing = TWINKLE_SPACING;
      } else {
        twinkle_spacing--;
      }

      twinkle();

      delay(timer);   
  }
}
   
void twinkle() {
  int brightness;
  int dir;
  int rate;
 
  int i = 0;
  for (i = 0 ; i < NUM_LEDS ; i++) {
   
    brightness = led_brightness[i];
    dir = led_direction[i];
    rate = led_speed[i];
   
    Tlc.set(i, brightness);
   
    if (dir == DOWN) {
      brightness -= rate;
      if (brightness <= 0) {
        brightness = 0;
      }
    }

    if (dir == UP) {
      brightness += rate;
      if (brightness >= MAX_BRIGHTNESS) {
        dir = DOWN;
        brightness = MAX_BRIGHTNESS;
      }
    }
   
    led_brightness[i] = brightness;
    led_direction[i] = dir;
  }
  Tlc.update();
}

void random_led() {
  int led = random(NUM_LEDS);
 
  if (led_direction[led] == 0 && led_brightness[led] == 0) {
    led_direction[led] = UP;
    led_speed[led] = random(min_speed, max_speed);
  }
}



Sketch #2, multiple patterns with button to cycle through
Code: [Select]

/*
* multiple patterns for leds
*/

#include "Tlc5940.h"

int potPin = 0;                 // input for the pot
int timer = 100;
int x = 10;                    // sets the max speed (0 = fast) the lower the number the faster it can go
int y = 300;                   // sets the min speed (100 = slow) the higher the number the slower it can go

int switchPin = 2;              // switch is connected to pin 2
int val;                        // variable for reading the pin status
int val2;                       // variable for reading the delayed status
int buttonState;                // variable to hold the button state

int lightMode = 0;              // What mode is the light in?


void setup()
{
  pinMode(switchPin, INPUT);    // Set the switch pin as input

  Serial.begin(9600);           // Set up serial communication at 9600bps
  buttonState = digitalRead(switchPin);   // read the initial state
 
   Tlc.init();

}

void loop(){
  val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         // 10 milliseconds is a good amount of time
    timer = 0;
  for ( int i = 0; i < 10; i++ ) {
    timer += analogRead(0);               // reads the pot and sets the timer to the value
}
    timer = timer / 10;
    timer = map(timer, 0, 1023, x, y);   // maps the timer value to the max and min vaues set above with x and y 
{
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  delay(10);                         // 10 milliseconds is a good amount of time
  if (val == val2) {                 // make sure we got 2 consistant readings!
    if (val != buttonState) {          // the button state has changed!
      if (val == LOW) {                // check if the button is pressed
        if (lightMode == 0) {          // if its off
          lightMode = 1;               // odds on evens off, then odds off evens on
        } else {
          if (lightMode == 1) {        // if its all-on
                lightMode = 0;         //two on at a time starting in middle going out
              }
          }
        }
      }
    }
    buttonState = val;                 // save the new state in our variable
  }

  // Now do whatever the lightMode indicates

if (lightMode == 0) { // odds on evens off, then odds off evens on
    Tlc.clear();
    Tlc.set(1, 2047);
    Tlc.set(3, 2047);
    Tlc.set(5, 2047);
    Tlc.set(7, 2047);
    Tlc.set(9, 2047);
    Tlc.set(11, 2047);
    Tlc.set(13, 2047);
    Tlc.set(15, 2047);
    Tlc.set(17, 2047);
    Tlc.set(19, 2047);
    Tlc.set(21, 2047);
    Tlc.set(23, 2047);
    delay(timer);
    Tlc.update();
    Tlc.set(0, 2047);
    Tlc.set(1, 0);
    Tlc.set(2, 2047);
    Tlc.set(3, 0);
    Tlc.set(4, 2047);
    Tlc.set(5, 0);
    Tlc.set(6, 2047);
    Tlc.set(7, 0);
    Tlc.set(8, 2047);
    Tlc.set(9, 0);
    Tlc.set(10, 2047);
    Tlc.set(11, 0);
    Tlc.set(12, 2047);
    Tlc.set(13, 0);
    Tlc.set(14, 2047);
    Tlc.set(15, 0);
    Tlc.set(16, 2047);
    Tlc.set(17, 0);
    Tlc.set(18, 2047);
    Tlc.set(19, 0);
    Tlc.set(20, 2047);
    Tlc.set(21, 0);
    Tlc.set(22, 2047);
    Tlc.set(23, 0);
    delay(timer);
    Tlc.update();
}
if (lightMode == 1) { //two on at a time starting in middle going out
    Tlc.clear();
    Tlc.set(11, 4095);
    Tlc.set(12, 4095);
    delay(timer);
    Tlc.update();
    Tlc.set(10, 4095);
    Tlc.set(11, 0);
    Tlc.set(12, 0);
    Tlc.set(13, 4095);
    delay(timer);
    Tlc.update();
    Tlc.set(9, 4095);
    Tlc.set(10, 0);
    Tlc.set(13, 0);
    Tlc.set(14, 4095);
    delay(timer);
    Tlc.update();
    Tlc.set(8, 4095);
    Tlc.set(9, 0);
    Tlc.set(14, 0);
    Tlc.set(15, 4095);
    delay(timer);
    Tlc.update();
    Tlc.set(7, 4095);
    Tlc.set(8, 0);
    Tlc.set(15, 0);
    Tlc.set(16, 4095);
    delay(timer);
    Tlc.update();
    Tlc.set(6, 4095);
    Tlc.set(7, 0);
    Tlc.set(16, 0);
    Tlc.set(17, 4095);
    delay(timer);
    Tlc.update();
    Tlc.set(5, 4095);
    Tlc.set(6, 0);
    Tlc.set(17, 0);
    Tlc.set(18, 4095);
    delay(timer);
    Tlc.update();
    Tlc.set(4, 4095);
    Tlc.set(5, 0);
    Tlc.set(18, 0);
    Tlc.set(19, 4095);
    delay(timer);
    Tlc.update();
    Tlc.set(3, 4095);
    Tlc.set(4, 0);
    Tlc.set(19, 0);
    Tlc.set(20, 4095);
    delay(timer);
    Tlc.update();
    Tlc.set(2, 4095);
    Tlc.set(3, 0);
    Tlc.set(20, 0);
    Tlc.set(21, 4095);
    delay(timer);
    Tlc.update();
    Tlc.set(1, 4095);
    Tlc.set(2, 0);
    Tlc.set(21, 0);
    Tlc.set(22, 4095);
    delay(timer);
    Tlc.update();
    Tlc.set(0, 4095);
    Tlc.set(1, 0);
    Tlc.set(22, 0);
    delay(timer);
    Tlc.update();
    Tlc.set(0, 0);
    delay(timer);
    Tlc.update();
}
}

nyg78

Here's my attempt at combining:

sketch #3
Code: [Select]

/*
* multiple patterns including twinkling
*/

#include "Tlc5940.h"

#define UP 1
#define DOWN 0
#define NUM_LEDS 23
#define TWINKLE_SPACING 7
#define MAX_BRIGHTNESS 4095

int led_brightness[NUM_LEDS];
int led_direction[NUM_LEDS];
int led_speed[NUM_LEDS];
int twinkle_spacing = TWINKLE_SPACING;
int min_speed;
int max_speed;

const int BLINK_MODE_TWINKLE = 1;
const int BLINK_MODE_ALL_ON = 2;
int blink_mode = BLINK_MODE_ALL_ON;


int potPin = 0;                 // input for the pot
int timer = 100;
int x = 10;                    // sets the max speed (0 = fast) the lower the number the faster it can go
int y = 300;                   // sets the min speed (100 = slow) the higher the number the slower it can go


int switchPin = 2;              // switch is connected to pin 2

int val;                        // variable for reading the pin status
int val2;                       // variable for reading the delayed status
int buttonState;                // variable to hold the button state

int lightMode = 0;              // What mode is the light in?


void setup()
{
  pinMode(switchPin, INPUT);    // Set the switch pin as input

  Serial.begin(9600);           // Set up serial communication at 9600bps
  buttonState = digitalRead(switchPin);   // read the initial state
 
  {min_speed = MAX_BRIGHTNESS / 20;
   max_speed = MAX_BRIGHTNESS / 30;
if (min_speed < 1) {
   min_speed = 1;
}
if (max_speed < 3) {
   max_speed = 10;}
}
int i = 0;
for (i = 0 ; i < 3 ; i++) {
   led_brightness[i] = 0;
   led_direction[i] = DOWN;
   led_speed[i] = min_speed;
}

   Tlc.init();
   
   randomSeed(analogRead(0));

}

void loop(){
  val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         // 10 milliseconds is a good amount of time
    timer = 0;
  for ( int i = 0; i < 10; i++ ) {
    timer += analogRead(0);               // reads the pot and sets the timer to the value
}
    timer = timer / 10;
    timer = map(timer, 0, 1023, x, y);   // maps the timer value to the max and min vaues set above with x and y 
{
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  delay(10);                         // 10 milliseconds is a good amount of time
  if (val == val2) {                 // make sure we got 2 consistant readings!
    if (val != buttonState) {          // the button state has changed!
      if (val == LOW) {                // check if the button is pressed
        if (lightMode == 0) {          // if its off
          lightMode = 1;               // twinkling
        } else {
          if (lightMode == 1) {        // if its twinkling
            lightMode = 2;             // odds on evens off, then odds off evens on
          } else {
            if (lightMode == 2) {      // if its odds on evens off, then odds off evens on
              lightMode = 3;           //two on at a time starting in middle going out
            } else {
  if (lightMode == 3) { //  if two on at a time starting in middle going out
                lightMode = 0;           // twinkling
              }
}
          }
        }
      }
    }
    buttonState = val;                 // save the new state in our variable
  }

  // Now do whatever the lightMode indicates
if (lightMode == 0) { // twinkling
    Tlc.clear();{
      BLINK_MODE_TWINKLE:
       if (twinkle_spacing == 0) {
        random_led();
        twinkle_spacing = TWINKLE_SPACING;
      } else {
        twinkle_spacing--;
      }
      twinkle();
      delay(timer);   
  }
    Tlc.update();

}
if (lightMode == 1) { // odds on evens off, then odds off evens on
Tlc.clear();
    Tlc.set(1, 2047);
    Tlc.set(3, 2047);
    Tlc.set(5, 2047);
    Tlc.set(7, 2047);
    Tlc.set(9, 2047);
    Tlc.set(11, 2047);
    Tlc.set(13, 2047);
    Tlc.set(15, 2047);
    Tlc.set(17, 2047);
    Tlc.set(19, 2047);
    Tlc.set(21, 2047);
    Tlc.set(23, 2047);
    delay(timer);
    Tlc.update();
    Tlc.set(0, 2047);
    Tlc.set(1, 0);
    Tlc.set(2, 2047);
    Tlc.set(3, 0);
    Tlc.set(4, 2047);
    Tlc.set(5, 0);
    Tlc.set(6, 2047);
    Tlc.set(7, 0);
    Tlc.set(8, 2047);
    Tlc.set(9, 0);
    Tlc.set(10, 2047);
    Tlc.set(11, 0);
    Tlc.set(12, 2047);
    Tlc.set(13, 0);
    Tlc.set(14, 2047);
    Tlc.set(15, 0);
    Tlc.set(16, 2047);
    Tlc.set(17, 0);
    Tlc.set(18, 2047);
    Tlc.set(19, 0);
    Tlc.set(20, 2047);
    Tlc.set(21, 0);
    Tlc.set(22, 2047);
    Tlc.set(23, 0);
    delay(timer);
    Tlc.update();
}
if (lightMode == 2) { //two on at a time starting in middle going out
  Tlc.clear();
    Tlc.set(11, 4095);
    Tlc.set(12, 4095);
    delay(timer);
    Tlc.update();
    Tlc.set(10, 4095);
    Tlc.set(11, 0);
    Tlc.set(12, 0);
    Tlc.set(13, 4095);
    delay(timer);
    Tlc.update();
    Tlc.set(9, 4095);
    Tlc.set(10, 0);
    Tlc.set(13, 0);
    Tlc.set(14, 4095);
    delay(timer);
    Tlc.update();
    Tlc.set(8, 4095);
    Tlc.set(9, 0);
    Tlc.set(14, 0);
    Tlc.set(15, 4095);
    delay(timer);
    Tlc.update();
    Tlc.set(7, 4095);
    Tlc.set(8, 0);
    Tlc.set(15, 0);
    Tlc.set(16, 4095);
    delay(timer);
    Tlc.update();
    Tlc.set(6, 4095);
    Tlc.set(7, 0);
    Tlc.set(16, 0);
    Tlc.set(17, 4095);
    delay(timer);
    Tlc.update();
    Tlc.set(5, 4095);
    Tlc.set(6, 0);
    Tlc.set(17, 0);
    Tlc.set(18, 4095);
    delay(timer);
    Tlc.update();
    Tlc.set(4, 4095);
    Tlc.set(5, 0);
    Tlc.set(18, 0);
    Tlc.set(19, 4095);
    delay(timer);
    Tlc.update();
    Tlc.set(3, 4095);
    Tlc.set(4, 0);
    Tlc.set(19, 0);
    Tlc.set(20, 4095);
    delay(timer);
    Tlc.update();
    Tlc.set(2, 4095);
    Tlc.set(3, 0);
    Tlc.set(20, 0);
    Tlc.set(21, 4095);
    delay(timer);
    Tlc.update();
    Tlc.set(1, 4095);
    Tlc.set(2, 0);
    Tlc.set(21, 0);
    Tlc.set(22, 4095);
    delay(timer);
    Tlc.update();
    Tlc.set(0, 4095);
    Tlc.set(1, 0);
    Tlc.set(22, 0);
    delay(timer);
    Tlc.update();
    Tlc.set(0, 0);
    delay(timer);
    Tlc.update();
}
}
}
void twinkle() {
  int brightness;
  int dir;
  int rate;
  int i = 0;
  for (i = 0 ; i < NUM_LEDS ; i++) {
   
    brightness = led_brightness[i];
    dir = led_direction[i];
    rate = led_speed[i];
   
    Tlc.set(i, brightness);
   
    if (dir == DOWN) {
      brightness -= rate;
      if (brightness <= 0) {
        brightness = 0;
      }
    }
    if (dir == UP) {
      brightness += rate;
      if (brightness >= MAX_BRIGHTNESS) {
        dir = DOWN;
        brightness = MAX_BRIGHTNESS;
      }
    }
    led_brightness[i] = brightness;
    led_direction[i] = dir;
  }
  Tlc.update();
}


void random_led() {
  int led = random(NUM_LEDS);
 
  if (led_direction[led] == 0 && led_brightness[led] == 0) {
    led_direction[led] = UP;
    led_speed[led] = random(min_speed, max_speed);
  }
}


Both sketch #1 and #2 work individually, but when I combine them into sketch #3 and turn the pot to adjust the speed, that's where the problem starts.   When it's in the random pattern, it doesn't go as fast as it did when I was just using sketch #1.  The speed for the other patterns is the same.  So how can I fix this?  Thank you.

Grumpy_Mike

Quote
These are not mine, I borrowed them

So I hope you are going to give them back.

Quote
So how can I fix this?

The two sketches use the delay function to control the timing. This means you just do one sketch and then the other. You are concatenating them you are not combining them.
You need to combine them by eliminating the calls to the delay function. This involves understanding what these two sketches do and deciding exactly what you want your new sketch to do and then write it, without using delay to control the timing.
There is an example in your arduino IDE called blink without delay, this shows you the basic thing you need to do with the timer millis() to make sure you only do things when you want to do them.

nyg78


Quote
These are not mine, I borrowed them

So I hope you are going to give them back.

Maybe

Quote
So how can I fix this?

The two sketches use the delay function to control the timing. This means you just do one sketch and then the other. You are concatenating them you are not combining them.
You need to combine them by eliminating the calls to the delay function. This involves understanding what these two sketches do and deciding exactly what you want your new sketch to do and then write it, without using delay to control the timing.
There is an example in your arduino IDE called blink without delay, this shows you the basic thing you need to do with the timer millis() to make sure you only do things when you want to do them.


I looked at the example and here's what I came up with.  It compiles with no errors, but when I upload it to test it out, turning the pot does nothing.  My goal is to vary the speed evenly on all patterns, but with sketch #3 in my previous post, the twinkling pattern changes speed too slowly when I turn the pot compared to the other patterns.

Code: [Select]


/*
* LEDs with multiple patterns including twinkling
*/

#include "Tlc5940.h"

#define UP 1
#define DOWN 0
#define NUM_LEDS 23
#define TWINKLE_SPACING 7
#define MAX_BRIGHTNESS 4095

int led_brightness[NUM_LEDS];
int led_direction[NUM_LEDS];
int led_speed[NUM_LEDS];
int twinkle_spacing = TWINKLE_SPACING;
int min_speed;
int max_speed;

const int BLINK_MODE_TWINKLE = 1;
const int BLINK_MODE_ALL_ON = 2;
int blink_mode = BLINK_MODE_ALL_ON;

int potPin = 0;                 // input for the pot
long previousMillis = 0;        // will store last time LED was updated
long interval = 100;
long x = 10;                    // sets the max speed (0 = fast) the lower the number the faster it can go
long y = 300;                   // sets the min speed (100 = slow) the higher the number the slower it can go

int switchPin = 2;              // switch is connected to pin 2

int val;                        // variable for reading the pin status
int val2;                       // variable for reading the delayed status
int buttonState;                // variable to hold the button state

int lightMode = 0;              // What mode is the light in?


void setup()
{
  pinMode(switchPin, INPUT);    // Set the switch pin as input

  Serial.begin(9600);           // Set up serial communication at 9600bps
  buttonState = digitalRead(switchPin);   // read the initial state
 
  {min_speed = MAX_BRIGHTNESS / 20;
   max_speed = MAX_BRIGHTNESS / 30;
if (min_speed < 1) {
   min_speed = 1;
}
if (max_speed < 3) {
   max_speed = 10;}
}
int i = 0;
for (i = 0 ; i < 3 ; i++) {
   led_brightness[i] = 0;
   led_direction[i] = DOWN;
   led_speed[i] = min_speed;
}

   Tlc.init();
   
   randomSeed(analogRead(0));

}

void loop(){
  unsigned long currentMillis = millis();
  val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         // 10 milliseconds is a good amount of time
    interval = 0;
  for ( int i = 0; i < 10; i++ ) {
    interval += analogRead(0);               // reads the pot and sets the timer to the value
}
    interval = interval / 10;
    interval = map(interval, 0, 1023, x, y);   // maps the timer value to the max and min vaues set above with x and y 
{
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  delay(10);                         // 10 milliseconds is a good amount of time
  if(currentMillis - previousMillis > interval) { // save the last time you blinked the LED
    previousMillis = currentMillis; 
  if (val == val2) {                 // make sure we got 2 consistant readings!
    if (val != buttonState) {          // the button state has changed!
      if (val == LOW) {                // check if the button is pressed
        if (lightMode == 0) {          // if its off
          lightMode = 1;               // twinkling
        } else {
          if (lightMode == 1) {        // if its twinkling
            lightMode = 2;             // odds on evens off, then odds off evens on
          } else {
            if (lightMode == 2) {      // if its odds on evens off, then odds off evens on
              lightMode = 3;           //two on at a time starting in middle going out
            } else {
  if (lightMode == 3) { //  if two on at a time starting in middle going out
                lightMode = 0;           // twinkling
              }
}
          }
        }
      }
    }
    buttonState = val;                 // save the new state in our variable
  }
  }
  // Now do whatever the lightMode indicates
if (lightMode == 0) { // twinkling
    Tlc.clear();{
      BLINK_MODE_TWINKLE:
       if (twinkle_spacing == 0) {
        random_led();
        twinkle_spacing = TWINKLE_SPACING;
      } else {
        twinkle_spacing--;
      }
      twinkle();
  }
    Tlc.update();

}
if (lightMode == 1) { // odds on evens off, then odds off evens on
Tlc.clear();
    Tlc.set(1, 2047);
    Tlc.set(3, 2047);
    Tlc.set(5, 2047);
    Tlc.set(7, 2047);
    Tlc.set(9, 2047);
    Tlc.set(11, 2047);
    Tlc.set(13, 2047);
    Tlc.set(15, 2047);
    Tlc.set(17, 2047);
    Tlc.set(19, 2047);
    Tlc.set(21, 2047);
    Tlc.set(23, 2047);
    Tlc.update();
    Tlc.set(0, 2047);
    Tlc.set(1, 0);
    Tlc.set(2, 2047);
    Tlc.set(3, 0);
    Tlc.set(4, 2047);
    Tlc.set(5, 0);
    Tlc.set(6, 2047);
    Tlc.set(7, 0);
    Tlc.set(8, 2047);
    Tlc.set(9, 0);
    Tlc.set(10, 2047);
    Tlc.set(11, 0);
    Tlc.set(12, 2047);
    Tlc.set(13, 0);
    Tlc.set(14, 2047);
    Tlc.set(15, 0);
    Tlc.set(16, 2047);
    Tlc.set(17, 0);
    Tlc.set(18, 2047);
    Tlc.set(19, 0);
    Tlc.set(20, 2047);
    Tlc.set(21, 0);
    Tlc.set(22, 2047);
    Tlc.set(23, 0);
    Tlc.update();
}
if (lightMode == 2) { //two on at a time starting in middle going out
  Tlc.clear();
    Tlc.set(11, 4095);
    Tlc.set(12, 4095);
    Tlc.update();
    Tlc.set(10, 4095);
    Tlc.set(11, 0);
    Tlc.set(12, 0);
    Tlc.set(13, 4095);
    Tlc.update();
    Tlc.set(9, 4095);
    Tlc.set(10, 0);
    Tlc.set(13, 0);
    Tlc.set(14, 4095);
    Tlc.update();
    Tlc.set(8, 4095);
    Tlc.set(9, 0);
    Tlc.set(14, 0);
    Tlc.set(15, 4095);
    Tlc.update();
    Tlc.set(7, 4095);
    Tlc.set(8, 0);
    Tlc.set(15, 0);
    Tlc.set(16, 4095);
    Tlc.update();
    Tlc.set(6, 4095);
    Tlc.set(7, 0);
    Tlc.set(16, 0);
    Tlc.set(17, 4095);
    Tlc.update();
    Tlc.set(5, 4095);
    Tlc.set(6, 0);
    Tlc.set(17, 0);
    Tlc.set(18, 4095);
    Tlc.update();
    Tlc.set(4, 4095);
    Tlc.set(5, 0);
    Tlc.set(18, 0);
    Tlc.set(19, 4095);
    Tlc.update();
    Tlc.set(3, 4095);
    Tlc.set(4, 0);
    Tlc.set(19, 0);
    Tlc.set(20, 4095);
    Tlc.update();
    Tlc.set(2, 4095);
    Tlc.set(3, 0);
    Tlc.set(20, 0);
    Tlc.set(21, 4095);
    Tlc.update();
    Tlc.set(1, 4095);
    Tlc.set(2, 0);
    Tlc.set(21, 0);
    Tlc.set(22, 4095);
    Tlc.update();
    Tlc.set(0, 4095);
    Tlc.set(1, 0);
    Tlc.set(22, 0);
    Tlc.update();
    Tlc.set(0, 0);
    Tlc.update();
}
}
}
void twinkle() {
  int brightness;
  int dir;
  int rate;
  int i = 0;
  for (i = 0 ; i < NUM_LEDS ; i++) {
   
    brightness = led_brightness[i];
    dir = led_direction[i];
    rate = led_speed[i];
   
    Tlc.set(i, brightness);
   
    if (dir == DOWN) {
      brightness -= rate;
      if (brightness <= 0) {
        brightness = 0;
      }
    }
    if (dir == UP) {
      brightness += rate;
      if (brightness >= MAX_BRIGHTNESS) {
        dir = DOWN;
        brightness = MAX_BRIGHTNESS;
      }
    }
    led_brightness[i] = brightness;
    led_direction[i] = dir;
  }
  Tlc.update();
}

void random_led() {
  int led = random(NUM_LEDS);
 
  if (led_direction[led] == 0 && led_brightness[led] == 0) {
    led_direction[led] = UP;
    led_speed[led] = random(min_speed, max_speed);
  }
}


Grumpy_Mike

There is lots wrong with that sketch and like a compiler I stop at the first error:-
Code: [Select]
Tlc.clear();{
      BLINK_MODE_TWINKLE:
       if (twinkle_spacing == 0) {

What is that meant to say? BLINK_MODE_TWINKLE evaluates to 1 and then there is a colon what do you think it does?

All those lines that go:-
Code: [Select]
Tlc.set(1, 2047);
    Tlc.set(3, 2047);
    Tlc.set(5, 2047);
    Tlc.set(7, 2047);
    Tlc.set(9, 2047);

You need to learn how to use loops. Very verbose code is difficult to follow. As is the fact that you have one huge big loop() function it is hard to see what is happening. This is what I call write only code.

So break it down into a number of functions and call those functions from loop()

Quote
It compiles with no errors,

That means you have not confused the compiler.

Quote
but when I upload it to test it out, turning the pot does nothing.

That is because you have not told it the right things to do.

You calculate 'interval' so do some Serial.prints to see what value you are getting here and see if it is the range you expect. However all you seem to be doing with this value is altering the time you look at buttons and set the lightMode variable. When this is 1 or 2 then you have a static display.


nyg78


All those lines that go:-
Code: [Select]
Tlc.set(1, 2047);
    Tlc.set(3, 2047);
    Tlc.set(5, 2047);
    Tlc.set(7, 2047);
    Tlc.set(9, 2047);

You need to learn how to use loops. Very verbose code is difficult to follow. As is the fact that you have one huge big loop() function it is hard to see what is happening. This is what I call write only code.

So break it down into a number of functions and call those functions from loop()


So if I use a loop, how do I turn on only certain LEDs?  The loops that I've seen go in order from one direction to the other using all LEDs, they don't skip any.  I have different patterns that I want to use.  Also you'll notice I don't have the LEDs on for full brightness because they wouldn't work if they were (probably drawing too much power).  How do I adjust brightness in a loop?  I've been working on other patterns, and depending on how many LEDs I have on, I have to adjust the brightness level.  So, in some patterns I have 4095, some I have 2047, and some even lower.

PaulS

Quote
So if I use a loop, how do I turn on only certain LEDs?

This code will repeat for i=1, 3, 5, 7, and 9:
Code: [Select]
for(int i=1; i<10; i+=2)
{
   Tlc.set(i, 2047);
}


Look at what you are doing repetitively. Find the pattern. Make the loop iterate the same way.

If the action in the repetitive section is non-linear (maybe you want 1, 2, 3, 7, 8, 9), use an array to hold the pattern values, and increment by 1, accessing the array values.

But, as Grumpy_Mike points out, you need to address the issues in the order that he listed them, rather than picking the easiest to implement to start with.
The art of getting good answers lies in asking good questions.

Grumpy_Mike

Quote
So if I use a loop, how do I turn on only certain LEDs?

One way is to use an array holding the pattern you want to have.
This page tells you about using an array to store values:-
http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

But as PaulS points out, this is tidying up, you need to fix the code first.

nyg78

Removed BLINK-MODE-TWINKLE:  Missed that when trying to combine.

If I want the odd channels on together (ex. 1, 3, 5, etc.), then turn them off and turn on the even channels (ex. 0, 2, 4, etc.)  then repeat, do I need to use an array?  I tried what PaulS wrote in reply #11 but it only lights one LED at at time.

I looked at the array tutorial, but I'm confused on how to do this using the TLC5940 since the tutorial writes to the pins.


Grumpy_Mike

Quote
If I want the odd channels on together (ex. 1, 3, 5, etc.), then turn them off and turn on the even channels (ex. 0, 2, 4, etc.)  then repeat, do I need to use an array?

No you can do that algorithmically.
Code: [Select]
for(int i=0; i<24; i++)
{
  if(i & 1) Tlc.set(i, 2047); else Tlc.set(i, 0);
}

Would set odd numbers on and even numbers off so how do you think you can do the opposite?

Quote
but I'm confused on how to do this using the TLC5940 since the tutorial writes to the pins.

The point is it shows you how to use an array to store values accessed by an index number. So these could be to write to pins like the tutorial but equally could be to write to the Tlc.set() function.

Go Up