Go Down

Topic: WS2812B Fastled Theater chase (Read 926 times) previous topic - next topic

Bbkhawk

May 25, 2018, 07:06 pm Last Edit: May 25, 2018, 07:08 pm by Bbkhawk
Hi new member here with a slight problem.I have a small program that uses 4 leds at a time moving along a strip of 150 WS2812B leds.It is working fine but I do not know how to add more leds to the sequence (i.e) 4 more led's begin 1 second later and chase the first 4.Here is my code so far.

#include <FastLED.h>



// Define the array of leds

#define LED_DT 6
#define COLOR_ORDER GRB
#define LED_TYPE WS2812
#define NUM_LEDS 150
uint8_t max_bright = 255;
struct CRGB leds[NUM_LEDS];



void setup() {
  delay (1000);
  LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setBrightness(max_bright);
   
   
}

 

void loop() {
 
 for(int i=0;i<NUM_LEDS;i++) {
 leds[i-4].setRGB (0, 0, 0);
 leds.setRGB ( 255, 255, 0);
 FastLED.show();
 delay (50);

Thank you in advance for any help you can offer.I tried to upload a GIF with the effect I want but it would not upload.


lesept

#1
May 25, 2018, 10:57 pm Last Edit: May 26, 2018, 10:18 am by lesept
This shouldn't be working as the index i-4 is negative if i is less than 4.
To do what you want to do, I would create a function that lits 4 consecutive leds:
Code: [Select]
void set4leds(int pos) {
 leds[pos].setRGB (0, 0, 0);
 leds[pos+1].setRGB (0, 0, 0);
 leds[pos+2].setRGB (0, 0, 0);
 leds[pos+3].setRGB (0, 0, 0);
 }
and call it with needed arguments. Before the first second, only one call with increasing position and after the first second, 2 calls to have the 2 bands chasing.

declare :
Code: [Select]
int index1;
int index2;
unsigned long initialtime;


in the setup :
Code: [Select]
index1 = 0;
index2 = 0;
initialtime = millis();


in the loop :
Code: [Select]
  FastLED.clear();
  set4leds(index1);
  index1 = (index1+1)%(NUM_LEDS-4);
  if (millis()-initialtime>1000) {
    set4leds(index2);
    index2 = (index2+1)%(NUM_LEDS-4);
}
 FastLED.show();
 delay (50);
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

Bbkhawk

Hi lesept.Thank you for your reply.The code I have I changed very slightly from a program from the internet.I will try to find the original.In the original I was = to -1 which made one led move so I changed it to -4 and it worked.No idea how as I have had my Arduino for around 2 weeks and am just beginning to learn from scratch.I am not sure how I would implement your code.Would I just copy and paste this as is ? I don't think I would as I think I have to define parameters first.Please forgive the (noob) questions.I am 59 years old but very willing to learn.

Thank you again.

Bbkhawk

This is the effect I am trying to get number 16 in the list but with more space between them i.e 6 dark leds between the lit ones.
https://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/#LEDStripEffectRunningLights

ieee488

This is the effect I am trying to get number 16 in the list but with more space between them i.e 6 dark leds between the lit ones.
https://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/#LEDStripEffectRunningLights

The person who wrote that tutorial is very responsive to questions.

You should have asked him.


.

lesept

#5
May 26, 2018, 10:23 am Last Edit: May 26, 2018, 10:26 am by lesept
Hi BbkHawk

Here is the code I suggest : it compiles OK but I didn't try it.

Code: [Select]
#include <FastLED.h>

// Define the array of leds
#define LED_DT 6
#define COLOR_ORDER GRB
#define LED_TYPE WS2812
#define NUM_LEDS 150
uint8_t max_bright = 255;
struct CRGB leds[NUM_LEDS];

int index1;
int index2;
unsigned long initialtime;

void set4leds(int pos) {
  leds[pos].setRGB (0, 0, 0);
  leds[pos + 1].setRGB (0, 0, 0);
  leds[pos + 2].setRGB (0, 0, 0);
  leds[pos + 3].setRGB (0, 0, 0);
}

void setup() {
  delay (1000);
  LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setBrightness(max_bright);
  index1 = 0;
  index2 = 0;
  initialtime = millis();
}

void loop() {
  FastLED.clear();
  set4leds(index1);
  index1 = (index1 + 1) % (NUM_LEDS - 4);
  if (millis() - initialtime > 1000) {
    set4leds(index2);
    index2 = (index2 + 1) % (NUM_LEDS - 4);
  }
  FastLED.show();
  delay (50);
}

However, it's definitely not the same as the example from the website you cited. This should send 4 lit LEDs in the strip, followed by another group of 4 one second later.
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

lesept

#6
May 26, 2018, 10:31 am Last Edit: May 26, 2018, 10:42 am by lesept
The theater chase from tweaking4all seems to be like this :
  • One LED of each N LEDs (meaning the other N-1 are off)
  • Shift every LEDs by one to the right: do the shift N-1 times (due to periodicity)
(I hope I explain clearly, I'm not native english speaker)

This can be done by playing with modulo (the % operator)

Code: [Select]
void setLEDs (int K,int N) {
  for (int i=0;i<NUM_LEDS;i++) {
    if (i%N == K)   leds[i].setRGB (127, 0, 0);  // set red LED
  }
}
 


This function should be called in the loop :

Code: [Select]
void loop() {
  FastLED.clear();
  for  (int i=0;i<N;i++) {
    setLEDs (i,N); }
  FastLED.show();
  delay (50);
}


The setup changes to
Code: [Select]
void setup() {
  delay (1000);
  LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setBrightness(max_bright);
}


and you need to declare the period before the setup:
N = 5;
for example.

Try it ! :)
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

Bbkhawk

#7
May 26, 2018, 12:00 pm Last Edit: May 26, 2018, 12:30 pm by Bbkhawk
Hi lesept,

Thank you very much for your time and reply.I will try this and let you know what happens.I tried the first sketch you gave to me which works but it only sends 4 leds then another 4 I second later.Once the first 8 reach the end of the strip it repeats.I tried to add more using index3 and so on but it only shows two sets of four.By the way your English is very good :)

#include <FastLED.h>

// Define the array of leds
#define LED_DT 6
#define COLOR_ORDER GRB
#define LED_TYPE WS2812
#define NUM_LEDS 150
uint8_t max_bright = 255;
struct CRGB leds[NUM_LEDS];

int index1;
int index2;
int index3;
unsigned long initialtime;

void set4leds(int pos) {
  leds[pos].setRGB (0, 0, 0);
  leds[pos + 1].setRGB (255,255, 0);
  leds[pos + 2].setRGB (255,255, 0);
  leds[pos + 3].setRGB (255,255, 0);
}

void setup() {
  delay (1000);
  LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setBrightness(max_bright);
  index1 = 0;
  index2 = 0;
  index3 = 0;
  initialtime = millis();
}

void loop() {
  FastLED.clear();
  set4leds(index1);
  index1 = (index1 + 1) % (NUM_LEDS - 4);
  if (millis() - initialtime > 1000) {
    set4leds(index2);
    index2 = (index2 + 1) % (NUM_LEDS - 4);
    set4leds(index3);
    index3 = (index3 + 1) % (NUM_LEDS - 4);
  }
  FastLED.show();
  delay (50);
}

lesept

Yes, it's not that easy. The test "if 'millis()..." acts as a chronometer. If you want more than one, you should add more chronos as well. S maybe copy all the if block (the lines between the {} including the if) and change the 1000 to 2000, 3000 and so on. And of course as you did use index2, index3...
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

Bbkhawk

#9
May 26, 2018, 01:19 pm Last Edit: May 26, 2018, 01:48 pm by Bbkhawk
Hi lesept,
Thank you again.It compiled ok but led's are still the same.I will try your second sketch and see if that works.
Once again thank you for your time and patience.

Hi again.I tried your second sketch but I was getting multiple errors.I will try to work on the original sketch you posted as that is practically there.
Thank you.

Bbkhawk

Hi lesept,

Just an update.I messed around with the timings in your first sketch and now have a very satisfactory result.
Thank you once again for all of your help.

lesept

#11
May 26, 2018, 08:33 pm Last Edit: May 27, 2018, 09:41 pm by lesept
Here is the second one : it compiles ok, please try it and tell me.
Code: [Select]
#include <FastLED.h>

// Define the array of leds
#define LED_DT 6
#define COLOR_ORDER GRB
#define LED_TYPE WS2812
#define NUM_LEDS 150
uint8_t max_bright = 255;
struct CRGB leds[NUM_LEDS];

int N = 5;

void setLEDs (int K, int N) {
  for (int i = 0; i < NUM_LEDS; i++) {
    if (i % N == K) leds[i].setRGB (127, 0, 0); // set red LED
  }
}

void setup() {
  delay (1000);
  LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setBrightness(max_bright);
}

void loop() {
  FastLED.clear();
  for  (int i = 0; i < N; i++) {
    setLEDs (i, N);
  }
  FastLED.show();
  delay (50);
}

If it works, you'll see red leds moving along your strip. Then we can play with it changing colors.
N = 5 is the interval between lit leds. Anyways, that's what I'm trying to get...
delay(50) sets the speed : increase the number to decrease the speed.

I'm in France, so there's almost no time difference with you. Also not much years... ;)
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

Bbkhawk

Hi lesept,

I tried your new sketch.It compiled fine but all the leds on the strip are static and lit red i.e all the leds in the strip are on.
Ah good to know we are of similar age :)

lesept

#13
May 27, 2018, 09:33 pm Last Edit: May 27, 2018, 09:42 pm by lesept
Hi, sorry for the delay, I've been busy working outside all day...

Move the line "int N = 5;"  just before the line "FastLED.clear();" in the loop
And move the line "FastLED.clear();" in the setLEDs function, just before the line "for (int i=0 ..." <-- this was the mistake :)
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

Bbkhawk

Hi lesept,

That just lights up every 5th led in the strand all static too.Thank you for your help though.I do not want you to waste too much time on this.Though your code is helping me understand so much.

Go Up