#include "FastLED.h"
#define NUM_LEDS 60
CRGB leds[NUM_LEDS];
#define PIN1 2
void setup()
{
FastLED.addLeds<WS2811, PIN2, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
randomSeed(analogRead(0));
}
void loop()
{
int myVar=random(0,6);
if (myVar <= 3)
{meteorRain1(CRGB(80,0,0), CRGB(0,80,0),random(25,35) ,70 ,true, random(5,15));
delay(200);
}
else
{
meteorRain2(CRGB(80,80,80), CRGB(00,00,80),random(20,30) ,70 ,true, random(5,15));
delay(200);
}
}
void meteorRain1(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);
}
for(int i = NUM_LEDS+NUM_LEDS; i >=0; 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 meteorRain2(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);
}
for(int i = NUM_LEDS+NUM_LEDS; i >=0; 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;
}
}
I have two meteorRain functions, meteorRain1 and meteorRain2, randomly being picked. Their major difference is in the two CRGB parameters.
I want to add two more meteorRain functions, each with another different pair of CRGB parameters.
The easy way, and less elegant, is to simply copy and paste two more blocks of meteorRain functions and change the parameters, and of course modify my random pick operation.( Modify my selection criteria and add a few else/if.)
I imagine the more professional method is to have four sets of parameters(six elements each) and randomly insert(?) one into the one meteorRain function.
Am I clear, and thinking properly?
I am not asking the code be written, just some direction.
Do I start with learning about arrays? Will that journey include passing the selected set of paramters into the meteorRain function?