Go Down

Topic: Impossible? Adding blinking to a WS2811 which is already using delays (Read 591 times) previous topic - next topic

Grumpy_Mike

This bit:-
Code: [Select]
if (start_num < 7 && millis() - startTime >= interval)
when it is reached start_num is always the same value. It is what ever it is when the :-
Code: [Select]
for (int i = start_num; i < 7; i++) {
finishes. So why do you include it in the test?

I will give you a hint. When writing code put your cursor on the right of an { and the matching } will have a line round it. This saves you counting and lets you see the extent of your {  } structure.

Try and do this after the { for the :-
Code: [Select]
for (int i = start_num; i < 7; i++) {
Loop

See if this is where you expect it to be.

Despereaux

This bit:-
Code: [Select]
if (start_num < 7 && millis() - startTime >= interval)
when it is reached start_num is always the same value. It is what ever it is when the :-
Code: [Select]
for (int i = start_num; i < 7; i++) {
finishes. So why do you include it in the test?

I will give you a hint. When writing code put your cursor on the right of an { and the matching } will have a line round it. This saves you counting and lets you see the extent of your {  } structure.

Try and do this after the { for the :-
Code: [Select]
for (int i = start_num; i < 7; i++) {
Loop

See if this is where you expect it to be.
Thanks for the advice about the start_num, I always like to learn to make things more efficient. But as for the blinking issue, you've got me stumped! I thought I knew what you were getting at. I had it wrapped around both loops. So I moved it out of the countdown loop and wrapped it around only the blinking loop, thinking that would solve the conflict. But that didn't change anything. So then I moved the end bracket } to literally every possible point in the loop, and none of them solved the issue. What have I missed?

Grumpy_Mike

I think you missed that you have one too many { } pairs so just moving one } is not going to result in the grouping you want. The code to blank lights has to be within the loop where i is changing.

Despereaux

I think you missed that you have one too many { } pairs so just moving one } is not going to result in the grouping you want. The code to blank lights has to be within the loop where i is changing.
Hmm, I hate to just ask for the solution because it's better to learn through experience, but I have to because I've got nothing. I feel like I've moved and removed every {} to every possible position five times and yet I just can't get it to work.

Grumpy_Mike

Ok this was a more interesting fault that I could initially tell from the code. The problem was not only with the sequence in which you did things but also with the toggle variable. The times when you thought the LEDs were not blinking they actually were. I could tell that because I put a print statement in the bit that flickered the LEDs and it printed that they were in that toggling routine. The problem was that although that routine was running, because of the state of the toggle variable when the start variable was changed the LEDs were being toggled to the same value as they already were on. This happened when the start variable was odd not even. When it was even it was working fine.

So the solution was to give each LED its own toggle value, that is put it in an array. The lastMillis was not needed so it was removed. However I thought a much better effect was generated when the LEDs toggled at a random rate so I added the
Code: [Select]
if (random(100)>50)
instead of the fixed toggle, this removed the need for the toggle variable all together.
Note if you are controlling three LEDs at a time, like most WS2811 strips the number of LEDs needs to be reduced to one third of the number. In this case that variable should be the number of controllers, not LEDs.

Code: [Select]

#include <FastLED.h>
#define NUM_LEDS 250
#define DATA_PIN 6
#define BRIGHTNESS 10
#define COLOR_ORDER RGB
CRGB leds[NUM_LEDS];

int color;

int flicker1[] = {2, 5, 8, 11, 14, 17, 20};

unsigned long startTime = millis();
unsigned long interval = 6000;
unsigned long int lastMillis;
uint8_t start_num = 0;
bool toggle[7];

void setup() {
 // delay(2000); // crap not needed
  //FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
}

void loop() {
  if (millis() - lastMillis > 50) { // filcker remaining LEDs
    for (int i = start_num; i < 7; i++) {
      if (random(100)>50) // a better effect?
      //if(toggle[i])
      {
        //show colorA
        leds[flicker1[i]] = CRGB(255, 50, 0);   
      }
      else
      {
        leds[flicker1[i]] = CRGB(90, 23, 0);
      }
      toggle[i] = !toggle[i]; // not needed if keeping the random flicker   
    }
      lastMillis = millis(); // for next flicker     
      FastLED.show();
  } // end of flicker all remaining LEDs

  if (start_num < 7 && (millis() - startTime >= interval)) { // turn one LED off
    leds[flicker1[start_num]] = CRGB(0, 0, 0);
    startTime = millis();
    FastLED.show();
    start_num++;
    if(start_num >= 7) start_num = 0; // start sequence over
  }
}

Despereaux

Ok this was a more interesting fault that I could initially tell from the code. The problem was not only with the sequence in which you did things but also with the toggle variable. The times when you thought the LEDs were not blinking they actually were. I could tell that because I put a print statement in the bit that flickered the LEDs and it printed that they were in that toggling routine. The problem was that although that routine was running, because of the state of the toggle variable when the start variable was changed the LEDs were being toggled to the same value as they already were on. This happened when the start variable was odd not even. When it was even it was working fine.

So the solution was to give each LED its own toggle value, that is put it in an array. The lastMillis was not needed so it was removed. However I thought a much better effect was generated when the LEDs toggled at a random rate so I added the
Code: [Select]
if (random(100)>50)
instead of the fixed toggle, this removed the need for the toggle variable all together.
Note if you are controlling three LEDs at a time, like most WS2811 strips the number of LEDs needs to be reduced to one third of the number. In this case that variable should be the number of controllers, not LEDs.

Code: [Select]

#include <FastLED.h>
#define NUM_LEDS 250
#define DATA_PIN 6
#define BRIGHTNESS 10
#define COLOR_ORDER RGB
CRGB leds[NUM_LEDS];

int color;

int flicker1[] = {2, 5, 8, 11, 14, 17, 20};

unsigned long startTime = millis();
unsigned long interval = 6000;
unsigned long int lastMillis;
uint8_t start_num = 0;
bool toggle[7];

void setup() {
 // delay(2000); // crap not needed
  //FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
}

void loop() {
  if (millis() - lastMillis > 50) { // filcker remaining LEDs
    for (int i = start_num; i < 7; i++) {
      if (random(100)>50) // a better effect?
      //if(toggle[i])
      {
        //show colorA
        leds[flicker1[i]] = CRGB(255, 50, 0);   
      }
      else
      {
        leds[flicker1[i]] = CRGB(90, 23, 0);
      }
      toggle[i] = !toggle[i]; // not needed if keeping the random flicker   
    }
      lastMillis = millis(); // for next flicker     
      FastLED.show();
  } // end of flicker all remaining LEDs

  if (start_num < 7 && (millis() - startTime >= interval)) { // turn one LED off
    leds[flicker1[start_num]] = CRGB(0, 0, 0);
    startTime = millis();
    FastLED.show();
    start_num++;
    if(start_num >= 7) start_num = 0; // start sequence over
  }
}

You're an absolute genius! I thought it was something with the toggle because of the way they would blink and freeze at the same timing of the toggle. But I don't think I ever would have come up with the solution you did on my own. And I love the random effect, it feels more real. I had planned on something like that but was so stuck on the other thing I couldn't even imagine taking that next step. Thank you so much for all your help and patience with a nooby, I really do appreciate it!

Grumpy_Mike

Beginner or not this is always how it feels when something works:-
http://www.youtube.com/watch?v=9saYHg_JmWE

Quote
Thank you so much
You are welcome, and thank you for not giving, that makes my effort worth it.

Go Up