Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is most likely no translated for the post!





LED Effects- modifi...
 
Share:
Notifications
Clear all

LED Effects- modified meteor rain chasing effect


(@linoarthur)
New Member
Joined: 4 months ago
Posts: 3
Topic starter  

Hello guys, im completely new to arduino programing leds and i found this amazing forum, i simply modified the meteor rain parameters to make it look like a flash passing through and some fade behind it, my question is can i make this loop restart quicker ? i mean when all the effect go through the strip it takes like 2sec to restart. here is the code. thank you very much for any help

#include

#define PIN 2
#define NUM_LEDS 60
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}
// ---------------------------------------------------
void loop() {
  meteorRain(0xff, 0x00, 0x00, 4, 22, true, 8);
}

void meteorRain(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {
  ;

  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);
  }
}

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 & 0x00ff0000 UL) >> 16;
  g = (oldColor & 0x0000ff00 UL) >> 8;
  b = (oldColor & 0x000000ff UL);

  r = (r <= 10) ? 0 : (int) r - (r * fadeValue / 250);
  g = (g <= 10) ? 0 : (int) g - (g * fadeValue / 250);
  b = (b <= 10) ? 0 : (int) b - (b * fadeValue / 250);

  strip.setPixelColor(ledNo, r, g, b);
  #endif
  #ifndef ADAFRUIT_NEOPIXEL_H
  // FastLED
  leds[ledNo].fadeToBlackBy(fadeValue);
  #endif
}
// -----------------------------------------
void showStrip() {
  #ifdef ADAFRUIT_NEOPIXEL_H
  // NeoPixel
  strip.show();
  #endif
  #ifndef ADAFRUIT_NEOPIXEL_H
  // FastLED
  FastLED.show();
  #endif
}

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
}

void setAll(byte red, byte green, byte blue) {
  for (int i = 0; i < NUM_LEDS; i++) {
    setPixel(i, red, green, blue);
  }
  showStrip();
}

ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1758
 

Hi linoarthur!

Welcome to the forum 😉 

First thing; to make things faster, I'd recommend switching to the FastLED library. It's more mature, offers more functions and ... is just faster than NeoPixel.
Because of the better functions, it can make your code a lot shorter as well!

I wrote my project at the time with both NeoPixel and FastLED in mind, and for that I had to write a few small "generic" functions.
FadeToBlack for example is much faster and easier to use in FastLED.

For example, remove the functions fadeToBlack, setPixelColor, showStrip and setPixel and replace all the calls to these functions like so:

fadeToBlack(LedNo, FadeValue) -> leds[ledNo].fadeToBlackBy( fadeValue );

showStrip() -> FastLED.show();

setPixel(Pixel, red, green, blue) -> leds[Pixel] = CRGB(red, green, blue);

setAll(red, green, blue) -> fill_solid( leds, NUM_LEDS, CRGB( red, green, blue) );

 

Next, your delay maybe caused by several things, but first thing I'd look at is the meteorTrailDecay value.
It may take more time than expected when the LEDs are almost dark.


ReplyQuote
(@linoarthur)
New Member
Joined: 4 months ago
Posts: 3
Topic starter  

Thanks Hans i'll change that, and for the delay, meteorTrailDecay only modify how long the trail is showing and SpeedDelay how fast the effect goes, what i want is at the end of that speedDelay it start faster again, actually the effect take like 5secs to go all the strip, and take like 2s to restart, i would like to reduce that like it goes round and round leaving the trace behind when the effect goes just like this meteor.

 

Thanks again.


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1758
 

The "start" you mention, actually does not exist. There is nothing to "initialize".
What we need to look at is the "ending" - this probably chews op the 2 seconds ...

Just as an experiment, try this: change this line

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

to this (remove "+ NUM_LEDS")

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

This may speed up the "finishing". Downside is that some of the last pieces of the trail do not disappear all that elegantly.
So you could look at fine tuning this to maybe something like this:

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

Give it a try and see what happens 😊 


ReplyQuote
(@linoarthur)
New Member
Joined: 4 months ago
Posts: 3
Topic starter  

Thank you SO much man, this works perfectly. thanks for the support

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

ReplyQuote



 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1758
 

Awesome! And you're most welcome! 👍 


ReplyQuote

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: