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 no translation for that post!



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

[Solved] LED Effects - Meteor Rain with user defined background color

5 Posts
2 Users
3 Reactions
7,237 Views
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2785
Topic starter  

Here a modified Meteor Rain code, based on an old request by Trace and more recent by Christian

This code is written for an Arduino Uno and FastLED.
I moved to FastLED since Trace prefers this, just like I do, over NeoPixel, and it makes the code a lot shorter.

The idea of this effect is that the user defines a background, for example red, and the user defines the meteor moving over it, for example yellow. 
In the original Meteor rain we use FadeToBlack, but unfortunately, it only fades to black, and not a user defined color.

Do for this to work I needed a new Fade-a-Color-to-Color function.
I found this excellent GitHub code from Kriegsman, from which I copied that function.
I also moved everything over to just FastLED, making the code shorter.

The result:

 

Code combined and tested:

#include "FastLED.h"
#define NUM_LEDS 60
CRGB leds[NUM_LEDS];
#define PIN 6

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

void loop() 
{
  meteorRain(CRGB(0x10,0x00,0x00), CRGB(0xff,0xff,0x00),10 ,64 ,true, 30);
} 

void meteorRain(CRGB ColorBackground, CRGB ColorMeteor, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) 
{  
  // set background color
  fill_solid( leds, NUM_LEDS, ColorBackground );

  for(int i = 0; i < NUM_LEDS+NUM_LEDS; i++) 
  {
    // fade color to background color for all LEDs
for(int j=0; j < NUM_LEDS; j++) {
if( (!meteorRandomDecay) || (random(10) > 5) ) {
leds[j] = fadeTowardColor(leds[j], ColorBackground, meteorTrailDecay );
}
}

// draw meteor
for(int j = 0; j < meteorSize; j++) {
if( ( i-j < NUM_LEDS) && (i-j >= 0) ) {
leds[i-j]= ColorMeteor;
}
} FastLED.show(); delay(SpeedDelay); } } // Functions from Kriegsman example CRGB fadeTowardColor( CRGB& cur, const CRGB& target, uint8_t amount) { nblendU8TowardU8( cur.red, target.red, amount); nblendU8TowardU8( cur.green, target.green, amount); nblendU8TowardU8( cur.blue, target.blue, amount); return cur; } // function used by "fadeTowardColor" void nblendU8TowardU8( uint8_t& cur, const uint8_t target, uint8_t amount) { if( cur == target) return; if( cur < target ) { uint8_t delta = target - cur; delta = scale8_video( delta, amount); cur += delta; } else { uint8_t delta = cur - target; delta = scale8_video( delta, amount); cur -= delta; } }

   
jackmtaco and TheChug reacted
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2785
Topic starter  

Here the INO file (in case the code got corrupted in the post).

 


   
ReplyQuote
(@kylegap)
Active Member
Joined: 5 years ago
Posts: 8
 

Wow man, that's LEGEND. I'll test it on my house tonight!


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2785
Topic starter  

Thanks! Let me know how this works for you 😉 👍 


   
TheChug reacted
ReplyQuote
(@kylegap)
Active Member
Joined: 5 years ago
Posts: 8
 

@hans Works perfectly. THank you!

I made a modification so the meteor go from one side, then comes back from the other. Seems like there is a tiny color glitch after the meteor came back, not sure why but it's fine anyway. Here's the code:

#include "FastLED.h"
#define NUM_LEDS 136
CRGB leds[NUM_LEDS];
#define PIN 6

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

void loop()
{
meteorRain(CRGB(0x10,0x18,0x10), CRGB(0xFF,0x11,0x26),10 ,64 ,true, 30);
meteorRainReverse(CRGB(0x10,0x18,0x10), CRGB(0xFF,0x11,0x26),10 ,64 ,true, 30);
}

void meteorRain(CRGB ColorBackground, CRGB ColorMeteor, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay)
{
// set background color
fill_solid( leds, NUM_LEDS, ColorBackground );

for(int i = 0; i < NUM_LEDS+NUM_LEDS; i++)
{
// fade color to background color for all LEDs
for(int j=0; j < NUM_LEDS; j++) {
if( (!meteorRandomDecay) || (random(10) > 5) ) {
leds[j] = fadeTowardColor(leds[j], ColorBackground, meteorTrailDecay );
}
}

// draw meteor
for(int j = 0; j < meteorSize; j++) {
if( ( i-j < NUM_LEDS) && (i-j >= 0) ) {
leds[i-j]= ColorMeteor;
}
}

FastLED.show();
delay(SpeedDelay);
}
}

void meteorRainReverse(CRGB ColorBackground, CRGB ColorMeteor, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay)
{
// set background color
fill_solid( leds, NUM_LEDS, ColorBackground );

for(int i = 0; i < NUM_LEDS+NUM_LEDS; i++)
{
// fade color to background color for all LEDs
for(int j=0; j < NUM_LEDS; j++) {
if( (!meteorRandomDecay) || (random(10) > 5) ) {
leds[j] = fadeTowardColor(leds[j], ColorBackground, meteorTrailDecay );
}
}

// draw meteor
for(int j = 0; j < meteorSize; j++) {
if( ( i-j < NUM_LEDS) && (i-j >= 0) ) {
leds[NUM_LEDS-i-j]= ColorMeteor;
}
}

FastLED.show();
delay(SpeedDelay);
}
}

// Functions from Kriegsman example
CRGB fadeTowardColor( CRGB& cur, const CRGB& target, uint8_t amount)
{
nblendU8TowardU8( cur.red, target.red, amount);
nblendU8TowardU8( cur.green, target.green, amount);
nblendU8TowardU8( cur.blue, target.blue, amount);
return cur;
}

// function used by "fadeTowardColor"
void nblendU8TowardU8( uint8_t& cur, const uint8_t target, uint8_t amount)
{
if( cur == target) return;

if( cur < target ) {
uint8_t delta = target - cur;
delta = scale8_video( delta, amount);
cur += delta;
} else {
uint8_t delta = cur - target;
delta = scale8_video( delta, amount);
cur -= delta;
}
}


   
ReplyQuote
Share: