How to add more meteors that overlaps and overtakes this first meteor at a faster speed

#include "FastLED.h"
#include <EEPROM.h>
#define NUM_LEDS 50 
CRGB leds[NUM_LEDS];
#define PIN 5 




void setup()
{
  FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
 
}

void loop() { 

  meteorRain(0xff,0x00,0x00,3, 164, true, 30);
 
}


void meteorRain(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {  
  setAll(0,0,0);
  
  for(int i = 0; i < NUM_LEDS+NUM_LEDS; i++) {
    
    
    // fade brightness all LEDs one step
    for(int j=0; j<NUM_LEDS; j++) {
      if( (!meteorRandomDecay) || (random(10)>5) ) {
        fadeToBlack(j, meteorTrailDecay );        
      }
    }
    
    // draw meteor
    for(int j = 0; j < meteorSize; j++) {
      if( ( i-j <NUM_LEDS) && (i-j>=0) ) {
        setPixel(i-j, red, green, blue);
      } 
    }
   
    showStrip();
    delay(SpeedDelay);
  }
}

// used by meteorrain
void fadeToBlack(int ledNo, byte fadeValue) {
 #ifdef ADAFRUIT_NEOPIXEL_H 
    // NeoPixel
    uint32_t oldColor;
    uint8_t r, g, b;
    int value;
    
    oldColor = strip.getPixelColor(ledNo);
    r = (oldColor & 0x00ff0000UL) >> 16;
    g = (oldColor & 0x0000ff00UL) >> 8;
    b = (oldColor & 0x000000ffUL);

    r=(r<=10)? 0 : (int) r-(r*fadeValue/256);
    g=(g<=10)? 0 : (int) g-(g*fadeValue/256);
    b=(b<=10)? 0 : (int) b-(b*fadeValue/256);
    
    strip.setPixelColor(ledNo, r,g,b);
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   leds[ledNo].fadeToBlackBy( fadeValue );
 #endif  
}

// *** REPLACE TO HERE ***



// ***************************************
// ** FastLed/NeoPixel Common Functions **
// ***************************************

// Apply LED color changes
void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

// Set a LED color (not yet visible)
void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H 
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

// Set all LEDs to a given color and apply it (visible)
void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue); 
  }
  showStrip();
}

similar to this

I moved your topic to an appropriate forum category @ahsan12345.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Why don't you just use the code from the video link you provided? The video is mine and I wrote the animation program a few years ago. It has some drawbacks but at least you can use it to start with. You just need to change the number of LEDs you use len = 150 and change the Line rope [] array by editing/removing/adding its elements. I had tried to make it easy to change the effects.

// Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠ° Π·Π° WS2812B https://github.com/cpldcpu/light_ws2812
 
/*
WS2812 datasheet
LED characteristic parameter
Emitting color  Wavelength(nm) Luminous intensity(mcd)  Current(mA) Voltage(V)
           Red         620-630                 550-700           20    1.8-2.2
         Green         515-530               1100-1400           20    3.0-3.2
          Blue         465-475                 200-400           20    3.2-3.4
*/
 
#include <WS2812.h>
 
// const boolean debug = true;
 
byte
c,
colorMix = 0,
a,
b
;
 
const byte
// colorMix = 0,
len = 150, // Π±Ρ€ΠΎΠΉ свСтодиоди
speedPot = A1,
mixPot = A4,
brightness = 255
;
 
// const word len = 8; // Π±Ρ€ΠΎΠΉ свСтодиоди
// word len = 8; // Π±Ρ€ΠΎΠΉ свСтодиоди
// #define len 8
 
WS2812 LED ( len ); // number of LED
 
cRGB color, clr;
 
const cRGB // GRB
black = { 0, 0, 0 },
white = { brightness, brightness, brightness },
gray = { brightness / 4, brightness / 4, brightness / 4 },
red = { 0, brightness, 0 },
green = { brightness, 0, 0 },
blue = { 0, 0, brightness },
yellow = { brightness, brightness, 0 },
cyan = { brightness, 0, brightness },
magenta = { 0, brightness, brightness }
;
 
cRGB pattern = black;
 
struct Line // Ρ†Π²ΡŠΡ‡ΠΊΠ°
{
  const cRGB Color; // цвят
  int Position; // Ρ‚Π΅ΠΊΡƒΡ‰Π° Π½Π°Ρ‡Π°Π»Π½Π° позиция
  const byte Linear; // дълТина
  const word Time; // Π²Ρ€Π΅ΠΌΠ΅Ρ‚Ρ€Π°Π΅Π½Π΅ ΠΌ/Ρƒ 2 ΡΡ‚ΡŠΠΏΠΊΠΈ
  unsigned long Next; // ΠΊΠΎΠ³Π° Π΅ Π²Ρ€Π΅ΠΌΠ΅ Π·Π° слСдваща ΡΡ‚ΡŠΠΏΠΊΠ°
  const word Pause; // Π²Ρ€Π΅ΠΌΠ΅ Π·Π° ΠΏΠ°ΡƒΠ·Π° ΠΏΡ€ΠΈ ΠΈΠ·Π»ΠΈΠ·Π°Π½Π΅ извън Π»Π΅Π½Ρ‚Π°Ρ‚Π°
  const boolean Cycle; // Π΄Π°Π»ΠΈ Π΄Π²ΠΈΠΆΠ΅Π½ΠΈΠ΅Ρ‚ΠΎ Π΅ Ρ†ΠΈΠΊΠ»ΠΈΡ‡Π½ΠΎ
  boolean Path; // ΠΏΡ€Π°Π²Π° ΠΈΠ»ΠΈ ΠΎΠ±Ρ€Π°Ρ‚Π½Π° посока Π½Π° Π΄Π²ΠΈΠΆΠ΅Π½ΠΈΠ΅
  boolean Change; // Π΄Π°Π»ΠΈ Π΅ Π½Π°ΡΡ‚ΡŠΠΏΠΈΠ»ΠΎ Π²Ρ€Π΅ΠΌΠ΅Ρ‚ΠΎ Π΄Π° сС ΠΏΡ€ΠΎΠΌΠ΅Π½ΠΈ позицията
  const boolean Direction; // ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ Π½Π° Π³Π»Π°Π²Π° ΠΈ опашка спрямо посоката Π½Π° Π΄Π²ΠΈΠΆΠ΅Π½ΠΈΠ΅
  boolean Active; // Π΄Π°Π»ΠΈ Π΅ Π°ΠΊΡ‚ΠΈΠ²Π½Π° Π·Π° Π²ΠΈΠ·ΡƒΠ°Π»ΠΈΠ·ΠΈΡ€Π°Π½Π΅
};
 
Line rope [] =
{
  //  clr,       pos,  n,   dt, t,    p, cycle,  path,  chng,   dir,   act
  { white,         0,  2,   20, 0, 4000,  true,  true, false,  true,  true },
  {   red, len/2 - 1, 16,  200, 0,    0, false,  true, false,  true, false },
  {  blue,     len/2, 16,  200, 0,    0, false, false, false,  true, false },
  { green,   len - 1,  4,   80, 0, 2000,  true, false, false,  true,  true }
     // { white,         4,  2, 1000, 0, 4000,  true,  true, false,  true }
};
 
const byte N = sizeof ( rope ) / sizeof ( Line ); // Π±Ρ€ΠΎΠΉ Ρ†Π²ΡŠΡ‡ΠΊΠΈ 2 34 17 ΠΊΠ°Ρ‚ΠΎ сС изчисляват Π½Π° Π±Π°Π·Π° дСфинирания масив
 
void clearLEDall ( cRGB blank )
{
  for ( byte n = 0; n < len; n ++ )
  {
    LED.set_crgb_at ( n, blank );
  }
}
 
byte scale ( byte scl, byte lngth, byte lnr, boolean drctn ) // цвят, сСгмСнт, дълТина, посока
{
  if ( !scl ) return 0;
  unsigned long S = lnr;
  if ( drctn ) S -= lngth; else S = ++ lngth;
  S *= S * scl;
  S /= lnr;
  S /= lnr;
  byte result = S;
  if ( !result ) result = 1;
  // if ( debug ) Serial.println ( result );
  return result;
}
 
void setup ()
{
  pinMode ( A0, OUTPUT );
  pinMode ( A2, OUTPUT );
  pinMode ( A3, OUTPUT );
  pinMode ( A5, OUTPUT );
  digitalWrite ( A0, LOW );
  digitalWrite ( A2, HIGH );
  digitalWrite ( A3, LOW );
  digitalWrite ( A5, HIGH );
  
  // if ( debug ) Serial.begin ( 115200 );
 
  LED.setOutput ( D5 );
}
 
void loop ()
{
  // Pot=analogRead(pinPot);
  // base=Pot/512;
  // pattern=black;
  // if (base) pattern=white;
  // colorMix=(Pot-base*512)/128;
 
  boolean change = false;
  
  a = b = 1; // скорост ΠΈ миксиранС
  word s = analogRead ( speedPot ), m = analogRead ( mixPot );
  if ( s > 480 && s < 544 ) digitalWrite ( 13, HIGH ); else digitalWrite ( 13, LOW ); // само Π·Π° тСст
  if ( s < 480 ) a = map ( s, 0, 480, 8, 2 );
  if ( s > 544 ) b = map ( s, 544, 1023, 2, 64 );
  colorMix = map ( m, 0, 1023, 0, 4 );
  
  unsigned long T = millis ();
  
  // if ( T > 16384 ) rope[0].Active = false;
  if ( T > 16384 ) rope[1].Active = true;
  if ( T > 32768 ) rope[2].Active = true;
  // if ( T > 65536 ) { rope[1].Active = false; rope[2].Active = false; }
  
  for ( byte n = 0; n < N; n ++ ) // int byte // ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° Π·Π° Π½Π°ΡΡ‚ΡŠΠΏΠΈΠ»ΠΎ Π²Ρ€Π΅ΠΌΠ΅
  {
    // if ( rope[n].Next <= millis () )
    // if ( millis () >= rope[n].Next ) // ΠΏΠΎ-ΠΌΠ°Π»ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠ°
    // if ( millis () >= rope[n].Next && rope[n].Active )
    if ( T >= rope[n].Next && rope[n].Active )
    {
      rope[n].Change = change = true;
      if ( !rope[n].Pause )
      // if ( rope[n].Pause == 0 )
        // rope[n].Next += rope[n].Time * a / b; // Π²Ρ€Π΅ΠΌΠ΅ Π·Π° слСдваща ΡΡ‚ΡŠΠΏΠΊΠ°
        rope[n].Next = T + rope[n].Time * a / b; // Π²Ρ€Π΅ΠΌΠ΅ Π·Π° слСдваща ΡΡ‚ΡŠΠΏΠΊΠ°
      else
      {
        // if ( ( rope[n].Position == ( len + rope[n].Linear - 1 ) ) || ( rope[n].Position == -( rope[n].Linear - 1 ) ) )
        if ( rope[n].Position == ( len + rope[n].Linear - 1 ) && rope[n].Path || rope[n].Position == -rope[n].Linear && !rope[n].Path )
          // rope[n].Next += rope[n].Pause * a / b;
          rope[n].Next = T + rope[n].Pause * a / b;
        else
          // rope[n].Next += rope[n].Time * a / b;
          rope[n].Next = T + rope[n].Time * a / b;
      }
      // if ( debug ) Serial.println ( rope[n].Next );
    }
  }
 
  if ( change )
  {
    // if ( debug ) Serial.println ( "Change" );
    // clearLEDall ( pattern ); // Π·Π°Π΄Π°Π²Π° Ρ†Π²Π΅Ρ‚ΠΎΠ²Π° Π±Π°Π·Π°
    // /*
    switch ( colorMix ) // Π·Π°Π΄Π°Π²Π° Ρ€Π°Π·Π»ΠΈΡ‡Π½Π° основа Π² зависимост ΠΎΡ‚ ΠΈΠ·Π±Ρ€Π°Π½ΠΎΡ‚ΠΎ миксиранС Π½Π° Ρ†Π²Π΅Ρ‚ΠΎΠ²Π΅Ρ‚Π΅
    {
      case 0: clearLEDall ( black ); break;
      case 1: clearLEDall ( white ); break;
      case 2: clearLEDall ( gray ); break;
      case 3: clearLEDall ( black ); break;
    }
    // */
 
    for ( byte n = 0; n < N; n ++ )
    {
      if ( rope[n].Active )
      {
      if ( !rope[n].Pause )
      {
        if ( rope[n].Cycle )
        {
          if ( rope[n].Position == len + rope[n].Linear && rope[n].Path ) rope[n].Position -= len;
          if ( rope[n].Position == -rope[n].Linear && !rope[n].Path ) rope[n].Position = len;
        }
        else
        {
          // if ( rope[n].Path && ( rope[n].Position == ( len + rope[n].Linear ) ) )
          if ( rope[n].Position == len + rope[n].Linear && rope[n].Path )
          {
            rope[n].Path = !rope[n].Path;
            rope[n].Position = len - 1;
          }
          // if ( !rope[n].Path && rope[n].Position == -rope[n].Linear )
          if ( rope[n].Position == -rope[n].Linear && !rope[n].Path )
          {
            rope[n].Path = !rope[n].Path;
            rope[n].Position = -1;
          }
        }
      }
      else
      {
        if ( rope[n].Cycle )
        {
          // if ( rope[n].Position == len + rope[n].Linear - 1 ) rope[n].Position = -1;
          if ( rope[n].Position == len + rope[n].Linear && rope[n].Path ) rope[n].Position = -1;
          if ( rope[n].Position == -rope[n].Linear && !rope[n].Path ) rope[n].Position = len;
        }
        else
        {
          if ( rope[n].Position == len + rope[n].Linear - 1 )
          {
            rope[n].Path = !rope[n].Path;
            rope[n].Position = len;
          }
          if ( rope[n].Position == -rope[n].Linear )
          {
            rope[n].Path = !rope[n].Path;
            rope[n].Position = -1;
          }
        }
      }
 
      for ( int length = 0; length < rope[n].Linear; length ++ )
      {
        int point = rope[n].Position;
        // if ( debug ) { Serial.print ( "Point and Position :" ); Serial.println ( point ); }
        // if ( rope[n].Path ) point -= length; else point += length;
        // if ( debug ) { Serial.print ( "Point and Length :" ); Serial.println ( point ); }
        point = point - length * ( rope[n].Path - !rope[n].Path ); // Π±ΠΈ трябвало Π΄Π° Π΅ ΠΊΠ°Ρ‚ΠΎ Π³ΠΎΡ€Π½ΠΎΡ‚ΠΎ само ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠ°Ρ‚Π° Π΅ ΠΏΠΎ-голяма
        if ( rope[n].Cycle && !rope[n].Pause )
        {
          if ( point >= len ) point -= len;
          if ( point < 0 ) point += len;
          // if ( debug ) { Serial.print ( "Position :" ); Serial.println ( rope[n].Position ); Serial.print ( "Point :" ); Serial.println ( point ); Serial.println (); }
        }
 
        if ( point < len && point >= 0 )
        {
          color = LED.get_crgb_at ( point );
          clr = rope[n].Color;
          clr.r = scale ( clr.r, length, rope[n].Linear, rope[n].Direction );
          clr.g = scale ( clr.g, length, rope[n].Linear, rope[n].Direction );
          clr.b = scale ( clr.b, length, rope[n].Linear, rope[n].Direction );
          switch ( colorMix )
          {
            case 0: // OR
            color.r |= clr.r;
            color.g |= clr.g;
            color.b |= clr.b;
            break;
            case 1: // AND
            color.r &= clr.r;
            color.g &= clr.g;
            color.b &= clr.b;
            break;
            case 2: // XOR
            color.r ^= clr.r;
            color.g ^= clr.g;
            color.b ^= clr.b;
            break;
            case 3: // sum
            color.r += clr.r;
            color.g += clr.g;
            color.b += clr.b;
            break;
          }
          LED.set_crgb_at ( point, color );
        }
      }
      if ( rope[n].Change )
      {
        rope[n].Change = false;
        // if ( rope[n].Path ) rope[n].Position ++; else rope[n].Position --;
        rope[n].Position = rope[n].Position + rope[n].Path - !rope[n].Path; // Π±ΠΈ трябвало Π΄Π° Π΅ ΠΊΠ°Ρ‚ΠΎ Π³ΠΎΡ€Π½ΠΎΡ‚ΠΎ ΠΈ Π΄Π°ΠΆΠ΅ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠ°Ρ‚Π° Π΅ ΠΌΠ°Π»ΠΊΠΎ ΠΏΠΎ-ΠΌΠ°Π»ΠΊΠ°
      }
      // if ( debug ) Serial.println ( rope[n].Position );
    }
    }
    LED.sync ();
  }
}
1 Like

@ahsan12345 -

This:

and if the answer is you want ever to make something like this your own, then the way to get there is to look at @flashko's sketch

  • function by function

and

  • line by line

until you figure out how it works. I know I did, some time back. I won't tell you how much I learned, nor how many things I had to google or otherwise ask and learn about nor how long it took.

What I will say is that the time (which I won't say precisely how much of that) it took was worth it.

We all start knowing nothing 'bout nothing. A sketch that does something cool that is anything like what you want to accomplish, and is well written and demonstrated, is like gold.

You just gotta mine it.

a7

1 Like

I am a complete beginner. For some reason, I am unable to run your code. I don't know if my LED strip supports those kinds of effects, or if I am calling the wrong library. However, if you can modify your code to run according to the FastLED library as shown below, it will clear all doubts, and I will be able to play around and modify it.

That doesn't tell us anything. You could add the message so we can try to navigate the problem. If it's because of the library, at the beginning of my program there is even a link to the library used to control the LEDs.

Nothing prevents you from using it. Then you can set your effects and forget or as @alto777 said you can learn the program what and how it does it but that's only if you have an interest in programming.

By the way @alto777 thanks for the kind words. I'm glad you had fun with my program.

here is the wokwi simulator video showing what i have finally achieved:

but my effect is not working smoothly as shown in your video, what will be the possible reasons?

here is the code that i have put into wokwi simulator

#include <FastLED.h>

#define LED_PIN     5
#define NUM_LEDS    50
#define BRIGHTNESS  255
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB

CRGB leds[NUM_LEDS];

byte
c,
colorMix = 0,
a,
b
;

const byte
len = 50,
speedPot = A1,
mixPot = A4;

const CRGB // GRB
black = CRGB(0, 0, 0),
white = CRGB(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS),
gray = CRGB(BRIGHTNESS / 4, BRIGHTNESS / 4, BRIGHTNESS / 4),
red = CRGB(0, BRIGHTNESS, 0),
green = CRGB(BRIGHTNESS, 0, 0),
blue = CRGB(0, 0, BRIGHTNESS),
yellow = CRGB(BRIGHTNESS, BRIGHTNESS, 0),
cyan = CRGB(BRIGHTNESS, 0, BRIGHTNESS),
magenta = CRGB(0, BRIGHTNESS, BRIGHTNESS);

struct Line {
  const CRGB Color;
  int Position;
  const byte Linear;
  const word Time;
  unsigned long Next;
  const word Pause;
  const boolean Cycle;
  boolean Path;
  boolean Change;
  const boolean Direction;
  boolean Active;
};

Line rope [] = {
  { white,         0,  2,   20, 0, 4000,  true,  true, false,  true,  true },
  {   red, NUM_LEDS/2 - 1, 16,  200, 0,    0, false,  true, false,  true, false },
  {  blue,     NUM_LEDS/2, 16,  200, 0,    0, false, false, false,  true, false },
  { green,   NUM_LEDS - 1,  4,   80, 0, 2000,  true, false, false,  true,  true }
};

const byte N = sizeof(rope) / sizeof(Line);

void clearLEDall(CRGB blank) {
  fill_solid(leds, NUM_LEDS, blank);
  FastLED.show();
}

byte scale(byte scl, byte lngth, byte lnr, boolean drctn) {
  if (!scl) return 0;
  unsigned long S = lnr;
  if (drctn) S -= lngth; else S = ++ lngth;
  S *= S * scl;
  S /= lnr;
  S /= lnr;
  byte result = S;
  if (!result) result = 1;
  return result;
}

void setup() {
  pinMode(A0, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  pinMode(A5, OUTPUT);
  digitalWrite(A0, LOW);
  digitalWrite(A2, HIGH);
  digitalWrite(A3, LOW);
  digitalWrite(A5, HIGH);

  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
}

void loop() {
  boolean change = false;

  a = b = 1;
  word s = analogRead(speedPot), m = analogRead(mixPot);
  if (s > 480 && s < 544) digitalWrite(13, HIGH);
  else digitalWrite(13, LOW);
  if (s < 480) a = map(s, 0, 480, 8, 2);
  if (s > 544) b = map(s, 544, 1023, 2, 64);
  colorMix = map(m, 0, 1023, 0, 4);

  unsigned long T = millis();

  if (T > 16384) rope[1].Active = true;
  if (T > 32768) rope[2].Active = true;

  for (byte n = 0; n < N; n++) {
    if (T >= rope[n].Next && rope[n].Active) {
      rope[n].Change = change = true;
      if (!rope[n].Pause)
        rope[n].Next = T + rope[n].Time * a / b;
      else {
        if (rope[n].Position == (NUM_LEDS + rope[n].Linear - 1) && rope[n].Path || rope[n].Position == -rope[n].Linear && !rope[n].Path)
          rope[n].Next = T + rope[n].Pause * a / b;
        else
          rope[n].Next = T + rope[n].Time * a / b;
      }
    }
  }

  if (change) {
    switch (colorMix) {
      case 0: clearLEDall(black); break;
      case 1: clearLEDall(white); break;
      case 2: clearLEDall(gray); break;
      case 3: clearLEDall(black); break;
    }

    for (byte n = 0; n < N; n++) {
      if (rope[n].Active) {
        if (!rope[n].Pause) {
          if (rope[n].Cycle) {
            if (rope[n].Position == NUM_LEDS + rope[n].Linear && rope[n].Path) rope[n].Position -= NUM_LEDS;
            if (rope[n].Position == -rope[n].Linear && !rope[n].Path) rope[n].Position = NUM_LEDS;
          } else {
            if (rope[n].Position == NUM_LEDS + rope[n].Linear && rope[n].Path) {
              rope[n].Path = !rope[n].Path;
              rope[n].Position = NUM_LEDS - 1;
            }
            if (rope[n].Position == -rope[n].Linear && !rope[n].Path) {
              rope[n].Path = !rope[n].Path;
              rope[n].Position = -1;
            }
          }
        } else {
          if (rope[n].Cycle) {
            if (rope[n].Position == NUM_LEDS + rope[n].Linear && rope[n].Path) rope[n].Position = -1;
            if (rope[n].Position == -rope[n].Linear && !rope[n].Path) rope[n].Position = NUM_LEDS;
          } else {
            if (rope[n].Position == NUM_LEDS + rope[n].Linear - 1) {
              rope[n].Path = !rope[n].Path;
              rope[n].Position = NUM_LEDS;
            }
            if (rope[n].Position == -rope[n].Linear) {
              rope[n].Path = !rope[n].Path;
              rope[n].Position = -1;
            }
          }
        }

        for (int length = 0; length < rope[n].Linear; length++) {
          int point = rope[n].Position;
          point = point - length * (rope[n].Path - !rope[n].Path);
          if (rope[n].Cycle && !rope[n].Pause) {
            if (point >= NUM_LEDS) point -= NUM_LEDS;
            if (point < 0) point += NUM_LEDS;
          }

          if (point < NUM_LEDS && point >= 0) {
            CRGB color = leds[point];
            CRGB clr = rope[n].Color;
            clr.r = scale(clr.r, length, rope[n].Linear, rope[n].Direction);
            clr.g = scale(clr.g, length, rope[n].Linear, rope[n].Direction);
            clr.b = scale(clr.b, length, rope[n].Linear, rope[n].Direction);
            switch (colorMix) {
    case 0: // OR
        color |= clr;
        break;
    case 1: // AND
        color &= clr;
        break;
    case 2: // XOR
        color = blend(color, clr, 128); // Blend 50% of color and 50% of clr
        break;
    case 3: // sum
        color += clr;
        break;
}
            leds[point] = color;
          }
        }
        if (rope[n].Change) {
          rope[n].Change = false;
          rope[n].Position = rope[n].Position + rope[n].Path - !rope[n].Path;
        }
      }
    }
    FastLED.show();
  }
}

Post the wokwi link.

wokwi link

code i have pasted is this

#include <FastLED.h>

#define LED_PIN     5
#define NUM_LEDS    50
#define BRIGHTNESS  255
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB

CRGB leds[NUM_LEDS];

byte
c,
colorMix = 0,
a,
b
;

const byte
len = 50,
speedPot = A1,
mixPot = A4;

const CRGB // GRB
black = CRGB(0, 0, 0),
white = CRGB(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS),
gray = CRGB(BRIGHTNESS / 4, BRIGHTNESS / 4, BRIGHTNESS / 4),
red = CRGB(0, BRIGHTNESS, 0),
green = CRGB(BRIGHTNESS, 0, 0),
blue = CRGB(0, 0, BRIGHTNESS),
yellow = CRGB(BRIGHTNESS, BRIGHTNESS, 0),
cyan = CRGB(BRIGHTNESS, 0, BRIGHTNESS),
magenta = CRGB(0, BRIGHTNESS, BRIGHTNESS);

struct Line {
  const CRGB Color;
  int Position;
  const byte Linear;
  const word Time;
  unsigned long Next;
  const word Pause;
  const boolean Cycle;
  boolean Path;
  boolean Change;
  const boolean Direction;
  boolean Active;
};

Line rope [] = {
  { white,         0,  2,   20, 0, 4000,  true,  true, false,  true,  true },
  {   red, NUM_LEDS/2 - 1, 16,  200, 0,    0, false,  true, false,  true, false },
  {  blue,     NUM_LEDS/2, 16,  200, 0,    0, false, false, false,  true, false },
  { green,   NUM_LEDS - 1,  4,   80, 0, 2000,  true, false, false,  true,  true }
};

const byte N = sizeof(rope) / sizeof(Line);

void clearLEDall(CRGB blank) {
  fill_solid(leds, NUM_LEDS, blank);
  FastLED.show();
}

byte scale(byte scl, byte lngth, byte lnr, boolean drctn) {
  if (!scl) return 0;
  unsigned long S = lnr;
  if (drctn) S -= lngth; else S = ++ lngth;
  S *= S * scl;
  S /= lnr;
  S /= lnr;
  byte result = S;
  if (!result) result = 1;
  return result;
}

void setup() {
  pinMode(A0, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  pinMode(A5, OUTPUT);
  digitalWrite(A0, LOW);
  digitalWrite(A2, HIGH);
  digitalWrite(A3, LOW);
  digitalWrite(A5, HIGH);

  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
}

void loop() {
  boolean change = false;

  a = b = 1;
  word s = analogRead(speedPot), m = analogRead(mixPot);
  if (s > 480 && s < 544) digitalWrite(13, HIGH);
  else digitalWrite(13, LOW);
  if (s < 480) a = map(s, 0, 480, 8, 2);
  if (s > 544) b = map(s, 544, 1023, 2, 64);
  colorMix = map(m, 0, 1023, 0, 4);

  unsigned long T = millis();

  if (T > 16384) rope[1].Active = true;
  if (T > 32768) rope[2].Active = true;

  for (byte n = 0; n < N; n++) {
    if (T >= rope[n].Next && rope[n].Active) {
      rope[n].Change = change = true;
      if (!rope[n].Pause)
        rope[n].Next = T + rope[n].Time * a / b;
      else {
        if (rope[n].Position == (NUM_LEDS + rope[n].Linear - 1) && rope[n].Path || rope[n].Position == -rope[n].Linear && !rope[n].Path)
          rope[n].Next = T + rope[n].Pause * a / b;
        else
          rope[n].Next = T + rope[n].Time * a / b;
      }
    }
  }

  if (change) {
    switch (colorMix) {
      case 0: clearLEDall(black); break;
      case 1: clearLEDall(white); break;
      case 2: clearLEDall(gray); break;
      case 3: clearLEDall(black); break;
    }

    for (byte n = 0; n < N; n++) {
      if (rope[n].Active) {
        if (!rope[n].Pause) {
          if (rope[n].Cycle) {
            if (rope[n].Position == NUM_LEDS + rope[n].Linear && rope[n].Path) rope[n].Position -= NUM_LEDS;
            if (rope[n].Position == -rope[n].Linear && !rope[n].Path) rope[n].Position = NUM_LEDS;
          } else {
            if (rope[n].Position == NUM_LEDS + rope[n].Linear && rope[n].Path) {
              rope[n].Path = !rope[n].Path;
              rope[n].Position = NUM_LEDS - 1;
            }
            if (rope[n].Position == -rope[n].Linear && !rope[n].Path) {
              rope[n].Path = !rope[n].Path;
              rope[n].Position = -1;
            }
          }
        } else {
          if (rope[n].Cycle) {
            if (rope[n].Position == NUM_LEDS + rope[n].Linear && rope[n].Path) rope[n].Position = -1;
            if (rope[n].Position == -rope[n].Linear && !rope[n].Path) rope[n].Position = NUM_LEDS;
          } else {
            if (rope[n].Position == NUM_LEDS + rope[n].Linear - 1) {
              rope[n].Path = !rope[n].Path;
              rope[n].Position = NUM_LEDS;
            }
            if (rope[n].Position == -rope[n].Linear) {
              rope[n].Path = !rope[n].Path;
              rope[n].Position = -1;
            }
          }
        }

        for (int length = 0; length < rope[n].Linear; length++) {
          int point = rope[n].Position;
          point = point - length * (rope[n].Path - !rope[n].Path);
          if (rope[n].Cycle && !rope[n].Pause) {
            if (point >= NUM_LEDS) point -= NUM_LEDS;
            if (point < 0) point += NUM_LEDS;
          }

          if (point < NUM_LEDS && point >= 0) {
            CRGB color = leds[point];
            CRGB clr = rope[n].Color;
            clr.r = scale(clr.r, length, rope[n].Linear, rope[n].Direction);
            clr.g = scale(clr.g, length, rope[n].Linear, rope[n].Direction);
            clr.b = scale(clr.b, length, rope[n].Linear, rope[n].Direction);
            switch (colorMix) {
    case 0: // OR
        color |= clr;
        break;
    case 1: // AND
        color &= clr;
        break;
    case 2: // XOR
        color = blend(color, clr, 128); // Blend 50% of color and 50% of clr
        break;
    case 3: // sum
        color += clr;
        break;
}
            leds[point] = color;
          }
        }
        if (rope[n].Change) {
          rope[n].Change = false;
          rope[n].Position = rope[n].Position + rope[n].Path - !rope[n].Path;
        }
      }
    }
    FastLED.show();
  }
}

add more meteors that overlaps this meteor

Which part?

just i want to add 2nd meteor in this effect that overtakes the first meteor at a faster speed.

#include "FastLED.h"
#include <EEPROM.h>
#define NUM_LEDS 50 
CRGB leds[NUM_LEDS];
#define PIN 5 




void setup()
{
  FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
 
}

void loop() { 

  meteorRain(0xff,0x00,0x00,3, 164, true, 30);
 
}


void meteorRain(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {  
  setAll(0,0,0);
  
  for(int i = 0; i < NUM_LEDS+NUM_LEDS; i++) {
    
    
    // fade brightness all LEDs one step
    for(int j=0; j<NUM_LEDS; j++) {
      if( (!meteorRandomDecay) || (random(10)>5) ) {
        fadeToBlack(j, meteorTrailDecay );        
      }
    }
    
    // draw meteor
    for(int j = 0; j < meteorSize; j++) {
      if( ( i-j <NUM_LEDS) && (i-j>=0) ) {
        setPixel(i-j, red, green, blue);
      } 
    }
   
    showStrip();
    delay(SpeedDelay);
  }
}

// used by meteorrain
void fadeToBlack(int ledNo, byte fadeValue) {
 #ifdef ADAFRUIT_NEOPIXEL_H 
    // NeoPixel
    uint32_t oldColor;
    uint8_t r, g, b;
    int value;
    
    oldColor = strip.getPixelColor(ledNo);
    r = (oldColor & 0x00ff0000UL) >> 16;
    g = (oldColor & 0x0000ff00UL) >> 8;
    b = (oldColor & 0x000000ffUL);

    r=(r<=10)? 0 : (int) r-(r*fadeValue/256);
    g=(g<=10)? 0 : (int) g-(g*fadeValue/256);
    b=(b<=10)? 0 : (int) b-(b*fadeValue/256);
    
    strip.setPixelColor(ledNo, r,g,b);
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   leds[ledNo].fadeToBlackBy( fadeValue );
 #endif  
}

// *** REPLACE TO HERE ***



// ***************************************
// ** FastLed/NeoPixel Common Functions **
// ***************************************

// Apply LED color changes
void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

// Set a LED color (not yet visible)
void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H 
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

// Set all LEDs to a given color and apply it (visible)
void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue); 
  }
  showStrip();
}

i have tried but the effect becomes very laggy/flickering, i don't know if it was hardware related issue or the code i write was wrong.

here is the another code that has many meteors, some are faster and some slower but its not working smoothly.

#include <FastLED.h>

#define LED_PIN     5
#define NUM_LEDS    50
#define BRIGHTNESS  255
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB

CRGB leds[NUM_LEDS];

byte
c,
colorMix = 0,
a,
b
;

const byte
len = 50,
speedPot = A1,
mixPot = A4;

const CRGB // GRB
black = CRGB(0, 0, 0),
white = CRGB(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS),
gray = CRGB(BRIGHTNESS / 4, BRIGHTNESS / 4, BRIGHTNESS / 4),
red = CRGB(0, BRIGHTNESS, 0),
green = CRGB(BRIGHTNESS, 0, 0),
blue = CRGB(0, 0, BRIGHTNESS),
yellow = CRGB(BRIGHTNESS, BRIGHTNESS, 0),
cyan = CRGB(BRIGHTNESS, 0, BRIGHTNESS),
magenta = CRGB(0, BRIGHTNESS, BRIGHTNESS);

struct Line {
  const CRGB Color;
  int Position;
  const byte Linear;
  const word Time;
  unsigned long Next;
  const word Pause;
  const boolean Cycle;
  boolean Path;
  boolean Change;
  const boolean Direction;
  boolean Active;
};

Line rope [] = {
  { white,         0,  2,   20, 0, 4000,  true,  true, false,  true,  true },
  {   red, NUM_LEDS/2 - 1, 16,  200, 0,    0, false,  true, false,  true, false },
  {  blue,     NUM_LEDS/2, 16,  200, 0,    0, false, false, false,  true, false },
  { green,   NUM_LEDS - 1,  4,   80, 0, 2000,  true, false, false,  true,  true }
};

const byte N = sizeof(rope) / sizeof(Line);

void clearLEDall(CRGB blank) {
  fill_solid(leds, NUM_LEDS, blank);
  FastLED.show();
}

byte scale(byte scl, byte lngth, byte lnr, boolean drctn) {
  if (!scl) return 0;
  unsigned long S = lnr;
  if (drctn) S -= lngth; else S = ++ lngth;
  S *= S * scl;
  S /= lnr;
  S /= lnr;
  byte result = S;
  if (!result) result = 1;
  return result;
}

void setup() {
  pinMode(A0, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  pinMode(A5, OUTPUT);
  digitalWrite(A0, LOW);
  digitalWrite(A2, HIGH);
  digitalWrite(A3, LOW);
  digitalWrite(A5, HIGH);

  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
}

void loop() {
  boolean change = false;

  a = b = 1;
  word s = analogRead(speedPot), m = analogRead(mixPot);
  if (s > 480 && s < 544) digitalWrite(13, HIGH);
  else digitalWrite(13, LOW);
  if (s < 480) a = map(s, 0, 480, 8, 2);
  if (s > 544) b = map(s, 544, 1023, 2, 64);
  colorMix = map(m, 0, 1023, 0, 4);

  unsigned long T = millis();

  if (T > 16384) rope[1].Active = true;
  if (T > 32768) rope[2].Active = true;

  for (byte n = 0; n < N; n++) {
    if (T >= rope[n].Next && rope[n].Active) {
      rope[n].Change = change = true;
      if (!rope[n].Pause)
        rope[n].Next = T + rope[n].Time * a / b;
      else {
        if (rope[n].Position == (NUM_LEDS + rope[n].Linear - 1) && rope[n].Path || rope[n].Position == -rope[n].Linear && !rope[n].Path)
          rope[n].Next = T + rope[n].Pause * a / b;
        else
          rope[n].Next = T + rope[n].Time * a / b;
      }
    }
  }

  if (change) {
    switch (colorMix) {
      case 0: clearLEDall(black); break;
      case 1: clearLEDall(white); break;
      case 2: clearLEDall(gray); break;
      case 3: clearLEDall(black); break;
    }

    for (byte n = 0; n < N; n++) {
      if (rope[n].Active) {
        if (!rope[n].Pause) {
          if (rope[n].Cycle) {
            if (rope[n].Position == NUM_LEDS + rope[n].Linear && rope[n].Path) rope[n].Position -= NUM_LEDS;
            if (rope[n].Position == -rope[n].Linear && !rope[n].Path) rope[n].Position = NUM_LEDS;
          } else {
            if (rope[n].Position == NUM_LEDS + rope[n].Linear && rope[n].Path) {
              rope[n].Path = !rope[n].Path;
              rope[n].Position = NUM_LEDS - 1;
            }
            if (rope[n].Position == -rope[n].Linear && !rope[n].Path) {
              rope[n].Path = !rope[n].Path;
              rope[n].Position = -1;
            }
          }
        } else {
          if (rope[n].Cycle) {
            if (rope[n].Position == NUM_LEDS + rope[n].Linear && rope[n].Path) rope[n].Position = -1;
            if (rope[n].Position == -rope[n].Linear && !rope[n].Path) rope[n].Position = NUM_LEDS;
          } else {
            if (rope[n].Position == NUM_LEDS + rope[n].Linear - 1) {
              rope[n].Path = !rope[n].Path;
              rope[n].Position = NUM_LEDS;
            }
            if (rope[n].Position == -rope[n].Linear) {
              rope[n].Path = !rope[n].Path;
              rope[n].Position = -1;
            }
          }
        }

        for (int length = 0; length < rope[n].Linear; length++) {
          int point = rope[n].Position;
          point = point - length * (rope[n].Path - !rope[n].Path);
          if (rope[n].Cycle && !rope[n].Pause) {
            if (point >= NUM_LEDS) point -= NUM_LEDS;
            if (point < 0) point += NUM_LEDS;
          }

          if (point < NUM_LEDS && point >= 0) {
            CRGB color = leds[point];
            CRGB clr = rope[n].Color;
            clr.r = scale(clr.r, length, rope[n].Linear, rope[n].Direction);
            clr.g = scale(clr.g, length, rope[n].Linear, rope[n].Direction);
            clr.b = scale(clr.b, length, rope[n].Linear, rope[n].Direction);
            switch (colorMix) {
    case 0: // OR
        color |= clr;
        break;
    case 1: // AND
        color &= clr;
        break;
    case 2: // XOR
        color = blend(color, clr, 128); // Blend 50% of color and 50% of clr
        break;
    case 3: // sum
        color += clr;
        break;
}
            leds[point] = color;
          }
        }
        if (rope[n].Change) {
          rope[n].Change = false;
          rope[n].Position = rope[n].Position + rope[n].Path - !rope[n].Path;
        }
      }
    }
    FastLED.show();
  }
}

If someone writes the corrected code or simply informs me of the potential reasons for these lag issues, it would be greatly appreciated. Perhaps I need to connect additional data pins, or maybe my WS2811 LED strip doesn't support these types of effects. Could it be that the Arduino's processor or memory is causing this lag? Alternatively, could it be that my code is not well-written?

Where is the first one? I count 12 patterns. None look like a meteor.


in my first code, i m calling this whole red thing first meteor

what i want is similar meteor in any other color should arise from left at faster speed than the red meteor and overtakes the red meteor .

Do you see how this "for" loop is counting from zero to the size of your LEDs?

Inside that loop, when the red LEDs are lit, add something like this:

leds[NUM_LEDS - i] = CGRB(255,255,255);

This is not exact, but it is showing that I am coloring the mirror opposite LED white. You will want to employ "fade to black" because my example just turns it white.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.