Hi everybody!
I'm asking for help in tweaking existing LED code.
(Code provided at the bottom)
I'm new to Arduino coding. It's pretty intimidating in the beginning, but achieving Halloween special effects for the niblings is great motivation. The code I found is from Tweaking4all which provided a kind of plug-and-play "module" guidance, so I had to learn a lot to properly "assemble" the modular pieces into a fully functional sketch. That's done, but the LED effect isn't yet correct for the intended application.
Here's what I hope someone can help me achieve:
I'm using code for an LED effect called Meteor Rain. The existing code causes a streak of light to traverse from one side of an LED strip to the other. The change I need is to make the streak of light bounce back to the starting point. (Imagine the game of Pong). That's it! The other change I need help with is telling the streak of light to repeat at random time intervals so it looks more organic. (I'm intending the streak of light to be little ghost sprite creatures).
The existing code works fine on an Arduino UNO, and an Arduino MEGA 2560. I'm intending to load up the code onto an ATTINY85 Digispark Board.
The ATTINY85 boards are still in the mail, so I can't test the code on them, yet.
I've gotten to the point of getting the code to work on my own, but modifying code is something I'm not able to do yet, and Halloween is fast approaching. If anyone could help, I (and the niblings) would be thrilled!
Note: I intentionally left in the comments of "REPLACE FROM HERE" and "REPLACE TO HERE" to clearly indicate where the Meteor Rain code exists to help a reader quickly distinguish the Meteor Rain code from the supporting code. Probably not necessary, but maybe it helps.
//For more information, reference https://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/
#include "FastLED.h"
#define NUM_LEDS 71
CRGB leds[NUM_LEDS];
#define PIN 6
void setup()
{
FastLED.addLeds<WS2813, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
}
// *** REPLACE FROM HERE ***
void loop() {
meteorRain(0xff,0xff,0xff,5, 90, true, 20);
}
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);
}
}
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 ***
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();
}