Fastled chasing first and third leds

so i got the other project working thanks to the help offered on here, now i have the problem of making the first (0) and third (2) leds on a led strip chase.

the code i have got is from somewhere online (i lost it and cant find it again from alot of googling around for a solution)

heres the code

#include <FastLED.h>
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB

#define NUM_LEDS 44
#define DATA_PIN 2
#define BRIGHTNESS         32

CRGB leds1[NUM_LEDS];

unsigned long delayPeriod = 50;

void setup() {
  delay(2000);
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds1, NUM_LEDS);  // GRB ordering is typical
  FastLED.setBrightness(BRIGHTNESS);
}

void loop()  {
  for (int i = 0; i < NUM_LEDS; i++) {
    leds1[i] = CRGB::Blue;
    FastLED.show();
    leds1[i] = CRGB::Black;;
    FastLED.show();
    delay(delayPeriod);}    
  
}

i tried
for (int i = 0, 2; i < NUM_LEDS; i++) {

for (int i = [0, 2]; i < NUM_LEDS; i++) {

for (int i = 0+2; i < NUM_LEDS; i++) {

none of which work.

im guessing the [0, 2] was along the right lines but i never defined an array and think thats where i went wrong?

some assistance would be great appreciated.

once i get first and third leds chasing i will probably add to it so every third led chases so it would end up as 0, 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44 leds chasing with the leds in between off. lord i hope that makes sense.

If you want to turn on 2 LEDs, if the index to the LEDs is i, turn on that LED and also turn on LED i+2

leds1[i] = CRGB::Blue;
leds1[i + 2] = CRGB::Blue;

ok so i tried what you said, it loops twice then stops and also only one led is chasing.

heres the code

#include <FastLED.h>
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB

#define NUM_LEDS 44
#define DATA_PIN 2
#define BRIGHTNESS         255

CRGB leds1[NUM_LEDS];

unsigned long delayPeriod = 50;

void setup() {
  delay(2000);
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds1, NUM_LEDS);  // GRB ordering is typical
  FastLED.setBrightness(BRIGHTNESS);
}

void loop()  {
  for (int i = 0; i < NUM_LEDS; i++) {
    leds1[i] = CRGB::Blue;
    FastLED.show();
    delay(delayPeriod);
    leds1[i] = CRGB::Black;
    leds1[i+3] = CRGB::Blue;
    FastLED.show();
    delay(delayPeriod);
    leds1[i+3] = CRGB::Black;
    leds1[i+5] = CRGB::Blue;
    FastLED.show();
    delay(delayPeriod);
    leds1[i+5] = CRGB::Black;
    leds1[i+7] = CRGB::Blue;
    FastLED.show();
    delay(delayPeriod);
    leds1[i+7] = CRGB::Black;
    leds1[i+10] = CRGB::Blue;
    FastLED.show();
    delay(delayPeriod);
    leds1[i+10] = CRGB::Black;
    leds1[i+13] = CRGB::Blue;
    FastLED.show();
    delay(delayPeriod);
    leds1[i+13] = CRGB::Black;
    leds1[i+16] = CRGB::Blue;
    FastLED.show();
    delay(delayPeriod);
    leds1[i+16] = CRGB::Black;
    leds1[i+19] = CRGB::Blue;
    FastLED.show();
    delay(delayPeriod);
    leds1[i+19] = CRGB::Black;
    leds1[i+22] = CRGB::Blue;
    FastLED.show();
    delay(delayPeriod);
    leds1[i+22] = CRGB::Black;
    leds1[i+25] = CRGB::Blue;
    FastLED.show();
    delay(delayPeriod);
    leds1[i+25] = CRGB::Black;
    leds1[i+28] = CRGB::Blue;
    FastLED.show();
    delay(delayPeriod);
    leds1[i+28] = CRGB::Black;
    leds1[i+31] = CRGB::Blue;
    FastLED.show();
    delay(delayPeriod);
    leds1[i+31] = CRGB::Black;
    leds1[i+34] = CRGB::Blue;
    FastLED.show();
    delay(delayPeriod);
    leds1[i+34] = CRGB::Black;
    leds1[i+37] = CRGB::Blue;
    FastLED.show();
    delay(delayPeriod);
    leds1[i+37] = CRGB::Black;
    leds1[i+41] = CRGB::Blue;
    FastLED.show();
    delay(delayPeriod);
    leds1[i+41] = CRGB::Black;
    delay(delayPeriod);}    
  
}

only other thing is this uses 70% available memory in an attiny85 , is there a more efficient way to handle this?

You also need to add a check to prevent the LED being addressed is actually in the array. One way would be to stop the for loop early to prevent writing outside of the array

i dont know how to add a check (never mind i re-read your post and realised what you meant, but it needs to continually loop

its wierd, if i add a second led thats on in the chase it works fine and exactly as expected but if add anymore than 2, it stops at the end of the chase instead of looping constantly and 2 on leds are next to each other and one has an off one in between second and third.

#include <FastLED.h>
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB

#define NUM_LEDS 44
#define DATA_PIN 2
#define BRIGHTNESS         255

CRGB leds1[NUM_LEDS];

unsigned long delayPeriod = 50;

void setup() {
  delay(2000);
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds1, NUM_LEDS);  // GRB ordering is typical
  FastLED.setBrightness(BRIGHTNESS);
}

void loop()  {
  for (int i = 0; i < NUM_LEDS; i++) {
    leds1[i] = CRGB::Blue;
    FastLED.show();
    delay(delayPeriod);
    leds1[i] = CRGB::Black;
    leds1[i+2] = CRGB::Blue;
    FastLED.show();
    delay(delayPeriod);
    leds1[i+2] = CRGB::Black;
    leds1[i+5] = CRGB::Blue;
    FastLED.show();
    delay(delayPeriod);
    leds1[i+5] = CRGB::Black;    
    delay(delayPeriod);
    FastLED.show();    
  }
}

Start by looking carefully at your code. You are not making proper use of the for loop. If you use the for loop vatiable properly you can do something like this

const byte maximum = 7;
const byte step = 2;

void setup()
{
  Serial.begin(115200);
  for (int i = 0; i < 8; i++)
  {
    Serial.print(i);
    Serial.print("\t");
    if (i + step < maximum)
    {
      Serial.print(i + step);
    }
    Serial.println();
    delay(500);
  }
}

void loop()
{
}

Note how the number printed never exceeds the maximum

i'll have to take a careful look at that later on, i dont really understand the format (and have to go to work in a moment) so i will have to play with it and see if i can make it understandable by seeing it work when im home.

thanks for the help helibob, it is appreciated. looking forward to trying to make sense of that serial print function and how it works

so i been playing with this and cannot understand it

If it is my example that you are referring to here it is again with comments

const byte maximum = 7; //maximum number in the array of LEDs
const byte step = 2;  //gap between the 2 LEDs that are on

void setup()
{
  Serial.begin(115200);
  for (int i = 0; i < 8; i++) //count from 0 to 7
  {
    Serial.print(i);  //print teh index to an LED (0 to 7)
    Serial.print("\t"); //print a tab to space out the printing
    if (i + step < maximum) //if the other LED to be controlled is on the strip
    {
      Serial.print(i + step); //print its index number
    }
    Serial.println(); //print a new line between pairs of numbers
    delay(500); //wait a bit
  }
}

void loop()
{
}

NOTE that there is no LED control in this sketch, it is merely an example of printing a sequence of 2 pairs of numbers with a fixed gap between the numbers. In practice you would use the numbers as the index to the LEDs being controlled. As an exercise try changing some of the values in teh sketch. See what happens if you increase step to 3, for instance

is the definitions at the start and setup of my code okay to use with the example you have given me to play around with?

None of the declarations/definitions at the start of your code have anything to do with what my example shows except for the number of LEDs

Apply the principle I showed to your sketch rather than multiple repetitions of what is basically the same code

Your code worked its way along the strip by using an ever increasing number to access the LEDs but the for loop will do that for you

Do you understand how my example works ?

The real question is

  • Do you want something "off-the-shelves" (which means DCR = {download, compile, run} ) or
  • are you interested in learning to code? :wink:

In the second case @UKHeliBob 's way is the best: It leaves to you to provide the solution , but gives you valuable hints! Good luck!

ok so im confused because theres no definitions or anything, i dont really get how i would apply this to my sketch.

can we begin from the very start, i.e start a fresh because this has all really confused the living hell out of me

honestly, im now entirely confused.

i have nothing against off the shelf because i can download it, compile it, load it to a digispark board and see it working, edit it, re-upload and see it working (or not work)

basically what im trying to get is one strip of 44 leds to chase every other led along the entire strip over and over for a warp core lamp im building

OK.
Start with my example sketch in post #8

  • the for loop in it causes the value of i to count from 0 to 7 in steps of 1
  • you know how to control an LED using a number to control which LED turns on because you do it in your sketch in post #3 ie leds1[i] = CRGB::Blue;`
  • the mistake you made was in not just using the value of i supplied by the for loop to control the series of LEDs. Instead you did it by adding increasing numbers to the value of i when all you really needed was to use the value of i inside the for loop as in my example
  • doing that would control each LED in turn from 0 to the total number of LEDs in the strip if you set the end point of the for loop appropriately
  • but you don't just want to control a single LED, you want to control the next but one LED as well, such as the first and third LEDs
  • back to my example. See how it prints the value of i and then the value of i plus the value of the step variable. With step set to 2 it first prints 0 then 0 + 2, ie 2. Next time through the for loop i becomes 1 so it prints 1 and 3 and so on
  • however, if you used the equivalent 2 numbers to control LEDs in your strip then you would end up trying to control LEDs past the end of it because of adding a value to the current value of i
  • so, before I print the second value in each pair the code checks to see if i + step will be greater than the maximum number that I set and if so it does not print it
  • the equivalent for your LED strip would be not try and control LEDs that are not on the strip

Now, with that background I suggest that you write a sketches to do the following in setup()

  1. turn on each LED in turn in the strip using a for loop with a short delay() after each one
  2. as above but turn off each LED before turning on the next one
  3. as 1. above but turn on the current LED and the current LED + 2
  4. as 3. but don't turn on any LEDs that are not in the strip

The setting up of the LED strip is all in your sketch in reply #6. Don't put anything in loop(), put the code at the end of setup() in each case and it will run once as a test. When it runs OK in setup() then move the for loop and the code that it contains to loop() and it will run repeatedly. Of course, for 1. above you will not know that it is running again because all of the LEDs will be on

If you do not want to learn how to do this yourself then don't expect to find code that does exactly what you want or rely on someone doing it for free, but you may be lucky

1 Like

It is a valid approach to take available code, study it and change it to do what you want.

However, it requires some basic knowhow in programming which - with regard to your questions - to be frank - you seem to be missing ... :wink:

There are two general possibilities

  • you get always others to do the job and stay on a low level of understanding (which is okay, but then one should not ask for explaination rather than for a complete sketch) or
  • you improve your skills and will be able to more and more solve your projects on your own (even after some time, you may help others if you like to do so).

(I think the second way although it is more exhausting, gives much more profit and satisfaction on the long term ...)

Anyway, it is up to you ... :wink:

ok so theres 2 replies to sort here,

heli bob i think i understand from your explanation, but wouldnt that be printing 2 leds along the strip at intervals?

ec, i may have to try to find someone to write this for me, and the reason why is for both of you,

im not using an arduino, im using a digispark attiy85 board wich apparently doesnt support spi fucntionality, upon some reason this requires further code to be added in order to get spi working and that is why im having difficulties getting anything that requires clockpin definitions. so im at an entire loss,

this code HAS to go onto a digispark board because of what the led strip is giong into (a model warp core)

Once you have the value of the for loop variable it is up to you what you do with it to control the LEDs.

  • turn 2 on with a space between them
  • leave them on then turn on 2 more
  • turn off the first 2 then turn on 2 more
  • turn off one of the first 2 and turn on the next one
  • something else

You choose what you want to do

i looked at your code but theres no setup to it, such as include fastled, pin declerations or anything so when i load it up, it essentially does nothing. this is what i meant by starting from the beginning.

you've presented me with a section of code that i have a limited understanding of but no idea how to make it do anything because the initial setup seems to be incomplete.

so i tried this but only a singular green led (second on along the strip) lights up

#include <FastLED.h>
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB

#define NUM_LEDS 44
#define DATA_PIN 2
#define BRIGHTNESS         32

CRGB leds1[NUM_LEDS];

const byte maximum = 7; //maximum number in the array of LEDs
const byte step = 2;  //gap between the 2 LEDs that are on

void setup()
{
  Serial.begin(115200);
  for (int i = 0; i < 8; i++) //count from 0 to 7
  {
    Serial.print(i);  //print teh index to an LED (0 to 7)
    Serial.print("\t"); //print a tab to space out the printing
    if (i + step < maximum) //if the other LED to be controlled is on the strip
    {
      Serial.print(i + step); //print its index number
    }
    Serial.println(); //print a new line between pairs of numbers
    delay(500); //wait a bit
  }
}

void loop()
{
}