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?