Go Down

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

PaulS

Quote
Like this?

If the arrays are to be pins to turn on, then the first thing that you need to do is turn all pins off. Then, turn the pins in the array on.

You also need to make sure that your loop index does not run off the end of the array (as yours do).
The art of getting good answers lies in asking good questions.

nyg78


If the arrays are to be pins to turn on, then the first thing that you need to do is turn all pins off. Then, turn the pins in the array on.

By adding a "Tlc.clear();"  before each "for" in the "void loop" section?

Quote

You also need to make sure that your loop index does not run off the end of the array (as yours do).

Not sure what you mean by this.

PaulS

Quote
By adding a "Tlc.clear();"  before each "for" in the "void loop" section?

If that actually turns all pins off, yes.

Quote
Not sure what you mean by this.

Code: [Select]
int pattern2 [24] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23};
But, you only provide 12 initial values.

Code: [Select]
for(int i=0; i<24; i++)
  Tlc.set(pattern1[i], 2047); //even channels

Then, you access all 24 of them.
The art of getting good answers lies in asking good questions.

nyg78


Quote
Not sure what you mean by this.

Code: [Select]
int pattern2 [24] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23};
But, you only provide 12 initial values.

Code: [Select]
for(int i=0; i<24; i++)
  Tlc.set(pattern1[i], 2047); //even channels

Then, you access all 24 of them.

Changed the "i<24;" to "i<12;" and now it works.  So this number will be how many LEDs I have in the array I want to use?

PaulS

Quote
So this number will be how many LEDs I have in the array I want to use?

It will be the number of pin numbers in the array. It's really hard to put an LED in an array.
The art of getting good answers lies in asking good questions.

nyg78

Starting to understand it now.  I ran into a problem that I can't figure out.  I'm trying out different patterns and came up with the following, which works, but for some reason instead of starting on pattern1 and going up,it starts on pattern2 and goes up.
Code: [Select]

#include "Tlc5940.h"

int pattern1 [24]= {0, 4, 8, 12, 16, 20};
int pattern2 [24]= {1, 5, 9, 13, 17, 21};
int pattern3 [24]= {2, 6, 10, 14, 18, 22};
int pattern4 [24]= {3, 7, 11, 15, 19, 23};

void setup()
{
  Tlc.init();
}

void loop() {
  Tlc.clear();
  for(int i=0; i<6; i++)
  Tlc.set(pattern1[i], 2047);
  Tlc.update();
  delay(500);
 
  Tlc.clear();
  for(int i=0; i<6; i++)
  Tlc.set(pattern2[i], 2047);
  Tlc.update();
  delay(500);
 
  Tlc.clear();
  for(int i=0; i<6; i++)
  Tlc.set(pattern3[i], 2047);
  Tlc.update();
  delay(500);
 
  Tlc.clear();
  for(int i=0; i<6; i++)
  Tlc.set(pattern4[i], 2047);
  Tlc.update();
  delay(500);
}

PaulS

Code: [Select]
int pattern1 [24]= {0, 4, 8, 12, 16, 20};
Why are the arrays so large?

Quote
instead of starting on pattern1 and going up,it starts on pattern2 and goes up.

Does it ever show pattern1?

I'd remove the last three blocks of code, and see what happens. Perhaps the issue is simply that pattern1 does not look like you think it does.
The art of getting good answers lies in asking good questions.

nyg78


Code: [Select]
int pattern1 [24]= {0, 4, 8, 12, 16, 20};
Why are the arrays so large?

Oops, fixed that.

Quote
instead of starting on pattern1 and going up,it starts on pattern2 and goes up.

Does it ever show pattern1?
[/quote]
Yes.  I want it to go pattern 1, 2, 3, 4, but it goes 2, 3, 4, 1 in that order.  I've uploaded it a few times to see it there was an issue uploading but everytime it does the same thing.

PaulS

Quote
Yes.  I want it to go pattern 1, 2, 3, 4, but it goes 2, 3, 4, 1 in that order.  I've uploaded it a few times to see it there was an issue uploading but everytime it does the same thing.

That's why I suggested not showing 4 patterns. Just show pattern1 over and over. Is what is displayed what you expect to see for pattern1?

The art of getting good answers lies in asking good questions.

nyg78

Yes.  All of these patterns are the same, just start and end at different pins/LEDs.  I could rearrange the code so that I get the results I want like this:

Code: [Select]

void loop() {
  Tlc.clear();
  for(int i=0; i<6; i++)
  Tlc.set(pattern4[i], 2047);
  Tlc.update();
  delay(500);
 
  Tlc.clear();
  for(int i=0; i<6; i++)
  Tlc.set(pattern1[i], 2047);
  Tlc.update();
  delay(500);
 
  Tlc.clear();
  for(int i=0; i<6; i++)
  Tlc.set(pattern2[i], 2047);
  Tlc.update();
  delay(500);
 
  Tlc.clear();
  for(int i=0; i<6; i++)
  Tlc.set(pattern3[i], 2047);
  Tlc.update();
  delay(500);
}

When I use this, it actually runs pattern 1, 2, 3, then 4 in that order.  I could do it this way, but I was just trying to see why it won't work the way one would think it would work.

nyg78

Been working on this again lately.  Fixed some things, but turning the pot in twinkling mode still isn't right.  If I have the pot on max speed in twinkling mode, it still twinkles too slowly.  And if I push the button to switch patterns (leaving the pot all the way to max), the next pattern moves very fast (like it should).  So how can I fix the twinkling mode so it will move at the same speed as the other patterns?  Also, in the void insideOut() section at the bottom of the sketch, I'm sure that can be shortened, but how?
Code: [Select]


#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;


int potPin = 0;                 // input pin for the potentiometer
int val = 0;                    // variable to store the value coming from the sensor
long previousMillis = 0;        // will store last time LED was updated
long interval = (10);           // interval at which to blink (milliseconds)
long x = 20;                    // sets the max speed (0 = fast) the lower the number the faster it can go
long y = 300;

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

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

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

int pattern1 [12]= {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22}; // evens on
int pattern2 [12] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23}; // odds on

int pattern3 [2]= {11, 12}; // inside two LEDs on
int pattern4 [2]= {10, 13};
int pattern5 [2]= {9, 14};
int pattern6 [2]= {8, 15};
int pattern7 [2]= {7, 16};
int pattern8 [2]= {6, 17};
int pattern9 [2]= {5, 18};
int pattern10 [2]= {4, 19};
int pattern11 [2]= {3, 20};
int pattern12 [2]= {2, 21};
int pattern13 [2]= {1, 22};
int pattern14 [2]= {0, 23};  // outside two LEDs on

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(){
  val2 = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         
    interval = 0;
  for ( int i = 0; i < 10; i++ ) {
    interval += analogRead(0);        // reads the pot and sets the interval to the value
}
    interval = interval / 10;
    interval = map(interval, 0, 1023, x, y);   // maps the interval value to the max and min values set above with x and y 
{
  val3 = digitalRead(switchPin);     // read the input again to check for bounces
  delay(10);                         
  if (val2 == val3) {                 // make sure we got 2 consistant readings!
    if (val2 != buttonState) {          // the button state has changed!
      if (val3 == LOW) {                // check if the button is pressed
        if (lightMode == 0) {          // if its random twinkling leds
          lightMode = 1;               // change to evens on/odds off, then odds on/evens off, etc.
        } else {
          if (lightMode == 1) {        // if its evens on/odds off, then odds on/evens off, etc.
            lightMode = 2;             // change to two on at a time starting inside going out
          } else {
            if (lightMode == 2) {      // if its two on at a time starting inside going out
              lightMode = 3;           // turn off
            } else {
      if (lightMode == 3) {    //  if its off
                lightMode = 0;         // change to random twinkling leds
              }
    }
          }
        }
      }
    }
    buttonState = val2;                 // save the new state in our variable
  }

  // Now do whatever the lightMode indicates
if (lightMode == 0) { // random twinkling leds
    allTwinkle();
}
if (lightMode == 1) { // evens on/odds off, then odds on/evens off, etc.
    evensOdds();
}
if (lightMode == 2) { // two on at a time starting inside going out
    insideOut();
  }
}
}
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);
  }
}

void allTwinkle(){ // random twinkling leds
  Tlc.clear();{
      if (twinkle_spacing == 0) {
       random_led();
       twinkle_spacing = TWINKLE_SPACING;
     } else {
       twinkle_spacing--;
     }
       twinkle();
       Tlc.update();
       delay(interval);   
}
}

void evensOdds(){ // evens on/odds off, then odds on/evens off, etc.
  Tlc.clear();{
    for(int i=0; i<12; i++)
    Tlc.set(pattern1[i], 2047); //even channels
    Tlc.update();
    delay(interval);
    Tlc.clear();
    for(int i=0; i<12; i++)
    Tlc.set(pattern2[i], 2047); //odd channels
    Tlc.update();
    delay(interval);
    Tlc.clear();
}
}

void insideOut(){ // two on at a time starting inside going out
    Tlc.clear();{
    for(int i=0; i<2; i++)
    Tlc.set(pattern3[i], 4095);
    Tlc.update();
    delay(interval);
    Tlc.clear();
    for(int i=0; i<2; i++)
    Tlc.set(pattern4[i], 4095);
    Tlc.update();
    delay(interval);
    Tlc.clear();
    for(int i=0; i<2; i++)
    Tlc.set(pattern5[i], 4095);
    Tlc.update();
    delay(interval);
    Tlc.clear();
    for(int i=0; i<2; i++)
    Tlc.set(pattern6[i], 4095);
    Tlc.update();
    delay(interval);
    Tlc.clear();
    for(int i=0; i<2; i++)
    Tlc.set(pattern7[i], 4095);
    Tlc.update();
    delay(interval);
    Tlc.clear();
    for(int i=0; i<2; i++)
    Tlc.set(pattern8[i], 4095);
    Tlc.update();
    delay(interval);
    Tlc.clear();
    for(int i=0; i<2; i++)
    Tlc.set(pattern9[i], 4095);
    Tlc.update();
    delay(interval);
    Tlc.clear();
    for(int i=0; i<2; i++)
    Tlc.set(pattern10[i], 4095);
    Tlc.update();
    delay(interval);
    Tlc.clear();
    for(int i=0; i<2; i++)
    Tlc.set(pattern11[i], 4095);
    Tlc.update();
    delay(interval);
    Tlc.clear();
    for(int i=0; i<2; i++)
    Tlc.set(pattern12[i], 4095);
    Tlc.update();
    delay(interval);
    Tlc.clear();
    for(int i=0; i<2; i++)
    Tlc.set(pattern13[i], 4095);
    Tlc.update();
    delay(interval);
    Tlc.clear();
    for(int i=0; i<2; i++)
    Tlc.set(pattern14[i], 4095);
    Tlc.update();
    delay(interval);
    Tlc.clear();
    for(int i=0; i<2; i++)
    Tlc.set(pattern14[i], 0);
    Tlc.update();
    delay(interval);
    Tlc.clear();
}
}

nyg78

If I use the following code (just the twinkling part, removing the button and multiple patterns), the "twinkling" goes much faster when I turn the pot.  I want it to go at this speed when I use the button to switch to other patterns.  I'm not using delay, so that doesn't seem to be the problem.  Any ideas?  Thank you.
Code: [Select]

// twinkling LEDs

#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;


int potPin = 0;    // select the input pin for the potentiometer
int val = 0;       // variable to store the value coming from the sensor
long previousMillis = 0;        // will store last time LED was updated
long interval = (10);           // interval at which to blink (milliseconds)
long x = 20;                    // sets the max speed (0 = fast) the lower the number the faster it can go
long y = 300;

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++ ) {
    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 
{
  Tlc.clear();
    if (twinkle_spacing == 0) {
      random_led();
      twinkle_spacing = TWINKLE_SPACING;
    } else {
      twinkle_spacing--;
    }
      twinkle();
      delay(interval);   
  }
}
   
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);
  }
}


PaulS

Quote
I'm not using delay, so that doesn't seem to be the problem.

You're not?
Code: [Select]
      delay(interval);   
The art of getting good answers lies in asking good questions.

nyg78

Changed that to this
Code: [Select]


       interval(val);   


and an error mesage says " 'interval' cannot be used as a function".

Grumpy_Mike

Quote
and an error mesage says " 'interval' cannot be used as a function".

True it can't.

Getting rid of delays does not mean replacing the delay command with another command, it means restructuring the way your code is written, see the blink without delay example.

Go Up