Fastled travelling repeated pattern

I was advised on reddit forum which I link below, the following code to have a pattern travel across a led strip. On linked reddit tProcessing: tqrr4cccm6g71.mp4...
heres a video of the effect o I want to achive, and im attaching it to this post also.

As in the video i want this pattern to travel across in a repeating manner, so there would be the same patterns multiple instances simultaneously travelling across the strip. Can anyone help with this code?

It seems to be pos and sweeppos multiplying, depending on the number of leds on the strip, however i it to be a scalable solution since some strips have 60 and some have 300 leds.


#include <FastLED.h>
#define LED_PIN     10
#define NUM_LEDS    300

#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];

#define UPDATES_PER_SECOND 100


void setup() {


  delay( 3000 ); // power-up safety delay
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, 120);
  FastLED.setBrightness(  BRIGHTNESS );
}

CRGBPalette16 sweep = CRGBPalette16(
                        CRGB::Blue,
                        CRGB::Green,
                        CRGB::Green,
                        CRGB::Green,
                        CRGB::Green,
                        CRGB::Green,
                        CRGB::Green,
                        CHSV( 90, 255, 255),
                        CHSV( 90, 255, 255),
                        CHSV( 90, 255, 255),
                        CRGB::Yellow,
                        CRGB::Yellow,
                        CRGB::Yellow,
                        CRGB::Yellow,
                        CRGB::Yellow,
                        CRGB::Yellow
                 );

const uint8_t SweepSize = 15; 

const CRGB BackgroundColor = CRGB::Red;

for(int sweepPos = -SweepSize; sweepPos < NUM_LEDS; sweepPos++) {

fill_solid(leds, NUM_LEDS, BackgroundColor);  // wasteful but easy 

for(int i = 0; i < SweepSize; i++) { 
int pos = i + sweepPos; if(pos > NUM_LEDS){ sweepPos = 0;} 
if(pos < 0) continue;  // not on the strip yet

leds[pos] = ColorFromPalette(sweep, (255 / SweepSize) * i);  
// set color// past the end of the strip } 
FastLED.show(); 
FastLED.delay(20);

Do you have the above animation running? Can you list the entire functioning program, that will make working with it possible easier.

What, at least, is the number of LEDs in you strip?

Essential it looks like the for loop sets a string of consecutive LEDs, the starting index of which is moved along.

To do multiple snakes running down the path, you can just (just!) loop over however many snakes you want, and set their starting points to be already along the entire chain.

a7

Thank you, i edited the entire file above. The strip has 300 leds, how ever it can be different number and this makes dynamically setting starting point difficult for me.

Ordinarily I'd let you battle it out, or take code ppl will provide here if you wait nice enough.

But by the time I figured out how the thing works, well, see:

Don't say I never did anything for you. :expressionless:

It's crude, but should give you a clue. I also fixed changed it so it works. Better.

EDIT: I have messed up my own demo, I think there's a glitch in wokwi OR a user error. So it still kinda works, but is now damaged, sry. I was trying to make an improvement, thought I had a named separate play pen, but no, oops.

Sigh:

a7

Thank you so much this seems to be working however I was hoping there was a way I could get this done by only changing the defined number of leds in the system CRGB leds[NUM_LEDS];.

With your current change for example , if I have 300 leds I would add about 5 or 6 for() loops, and then when I scale my hardware back to 100 leds, I would need to remove 2-3 of those for loops and somehow I feel there must be another way to do this.

I will try to edit
int pos = i + sweepPos + 30;
and change it to
int pos = i + sweepPos + (NUM_LEDS/3);

and for the next for() loop do
int pos = i + sweepPos + (NUM_LEDS/3*2);

and see if I can control these with an if() so that the 3rd or 4th declared pos does not exceed the number of leds in the system.

Is as such so far.


#include <FastLED.h>
#include <pixeltypes.h>

#define LED_PIN     10
#define NUM_LEDS    300
#define BRIGHTNESS  96 //who do pieces turn Red when I turn this to 255?
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];

CRGBPalette16 sweep = CRGBPalette16(
                        CRGB::Cyan,
                        CRGB::Blue,
                        CRGB::Green,
                        CRGB::Green,
                        CRGB::Green,
                        CRGB::Green,
                        CRGB::Green,
                        CHSV( 90, 255, 255),
                        CHSV( 90, 255, 255),
                        CHSV( 90, 255, 255),
                        CRGB::Yellow,
                        CRGB::Yellow,
                        CRGB::Yellow,
                        CRGB::Yellow,
                        CRGB::Yellow,
                        CRGB::Yellow
                      );

const uint8_t SweepSize = 15;


 void setup() {

  delay( 3000 ); // power-up safety delay
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(  BRIGHTNESS );
}


const CRGB BackgroundColor = CRGB::Red;

void loop() {
  for (int sweepPos = 0; sweepPos < NUM_LEDS; sweepPos++) {

    fill_solid(leds, NUM_LEDS, BackgroundColor);  // wasteful but easy



    for (int i = 0; i < SweepSize; i++) {
      int pos = i + sweepPos; // if(pos > NUM_LEDS){ sweepPos = 0;}
      // if(pos < 0) continue;  // not on the strip yet

      // Serial.println(pos);

      pos += NUM_LEDS;
      leds[pos % NUM_LEDS] = ColorFromPalette(sweep, (255 / SweepSize) * i);
      // set color// past the end of the strip
    }


    for (int i = 0; i < SweepSize; i++) {
      int pos = i + sweepPos + (NUM_LEDS/4); // if(pos > NUM_LEDS){ sweepPos = 0;}    // if(pos < 0) continue;  // not on the strip yet    // Serial.println(pos);
      pos += NUM_LEDS;
      leds[pos % NUM_LEDS] = ColorFromPalette(sweep, (255 / SweepSize) * i);  // set color// past the end of the strip    
    }

  for (int i = 0; i < SweepSize; i++) {
      int pos = i + sweepPos + (NUM_LEDS/4*2); // if(pos > NUM_LEDS){ sweepPos = 0;}
      // if(pos < 0) continue;  // not on the strip yet

      // Serial.println(pos);

      pos += NUM_LEDS;
      leds[pos % NUM_LEDS] = ColorFromPalette(sweep, (255 / SweepSize) * i);
      // set color// past the end of the strip
    }

  for (int i = 0; i < SweepSize; i++) {
      int pos = i + sweepPos + (NUM_LEDS/4*3); // if(pos > NUM_LEDS){ sweepPos = 0;}
      // if(pos < 0) continue;  // not on the strip yet

      // Serial.println(pos);

      pos += NUM_LEDS;
      leds[pos % NUM_LEDS] = ColorFromPalette(sweep, (255 / SweepSize) * i);
      // set color// past the end of the strip
    }
  
    FastLED.show();
    FastLED.delay(50);


  }
}

The use of modular arithmetic means you don’t need to worry about writing before or past the end of reality.

And yes, there are certainly better ways of coding multiple snakes. The single snake loop can be improved to handle NSAKES number of snakes. Rheir respective offsets, like the hard coded 30 in my crude duplicate loop, cou,d be held in an array of offsets.

Adjusting to new physical circumstances would be changing

NUM_LEDS

SWEEPSIZE

NSNAKES

and the array of snake offsets, unless that is algorithmic determined, like snake N is just 30*N offset.

Then you might have

SNAKS_SPACING

as another adjustable constant.

So learn up on arrays, look at the loop that has the offset in it no give a shot at generalizing it to handle writing all the snake pixels at pos and the offsets.

Edit: haven’t look at your new code. Will, L8R.

a7

OK, your shot is good.

Now you see the repeated pattern. Arrays will help you write one loop that sets a pixel for all N number of snakes.

Or that for loop could be a function that takes the offset of a snake as a parameter and writes an entire snake, just call it for all N number of snakes.

There are many ways to go, but for now it works good yay! And you can tinker with colors and sizes and things.

BTW many of the (repeated) comments can be removed, as they now no longer make sense since the introduction of the modualr arithmetic.

Watch out for fun if you code numbers that mean snakes overlap. If you ever write something that makes the snakes go at different speeds this will come up.

So arrays and functions if you are going to get more serious. :expressionless:

a7