Hi Hans,
Thank you very much, it's exactly what I need and your explanations are very clear.
I replaced 20 times by 53 times and 64 by 26 to have 10% and a slower fade to black. Like you I used a spreadsheet.
Hereafter there is the working code.
Inside there are the fire effect and the theater effect that I keep to use after on a 200 LED strip.
// Code source: Hans Luijten
// Website: https://www.tweaking4all.com
#include <Adafruit_NeoPixel.h>
#include <EEPROM.h>
//#include <FastLED.h> *Not used for the moment
#define NUM_LEDS 100
#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
// REPLACE FROM HERE
void loop() {
int i;
int j;
int k;
{
fadeInRainbow(5);
}
for(i=0; i<random (5,15); i++) {
rainbowCycleWithSparkle(50,20);
}
{
fadeOutRainbow(20);
}
for(j=0; j<random (0,4); j++) {
meteorRain(0xff,0xff,0xff,10, 64, true, 30);
}
{
meteorRainTransition(0xff,0xff,0xff,10, 64, true, 30);
}
{
colorWipeReversed(0xff,0xff,0xff, 10);
// colorWipeReversed(0x00,0x00,0x00, 20);
}
for(k=0; k<random (3000,20000); k++) {
Sparkle(0xff, 0xff, 0xff, 0);
}
}
// *************************
// ** LEDEffect Functions **
// *************************
//fadeInRainbow
void fadeInRainbow(int SpeedDelay) {
byte *c;
uint16_t i, j;
for(j = 0; j < 256; j=j+1) {
for(i=0; i< NUM_LEDS; i++) {
c=Wheel((i * 256 / NUM_LEDS) & 255);
setPixel(i, *c*(j/256.0), *(c+1)*(j/256.0), *(c+2)*(j/256.0));
}
showStrip();
delay(SpeedDelay);
}
}
//Sparkle
void Sparkle(byte red, byte green, byte blue, int SpeedDelay) {
int Pixel = random(NUM_LEDS);
setPixel(Pixel,red,green,blue);
showStrip();
delay(SpeedDelay);
setPixel(Pixel,0,0,0);
}
//colorWipeReversed
void colorWipeReversed(byte red, byte green, byte blue, int SpeedDelay) {
for(uint16_t i=NUM_LEDS; i>0; i--) {
setPixel(i, red, green, blue);
showStrip();
delay(SpeedDelay);
}
}
//RainbowCycleWithSparkle
void rainbowCycleWithSparkle(int SparkleDelay, int SpeedDelay) {
byte *c;
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i< NUM_LEDS; i++) {
c=Wheel(((i * 256 / NUM_LEDS) + j) & 255);
setPixel(i, *c, *(c+1), *(c+2));
}
showStrip();
delay(SpeedDelay);
int Pixel = random(NUM_LEDS);
setPixel(Pixel,0xff,0xff,0xff);
showStrip();
delay(SparkleDelay);
}
}
// used by rainbowCycleWithSparkle
byte * Wheel(byte WheelPos) {
static byte c[3];
if(WheelPos < 85) {
c[0]=WheelPos * 3;
c[1]=255 - WheelPos * 3;
c[2]=0;
} else if(WheelPos < 170) {
WheelPos -= 85;
c[0]=255 - WheelPos * 3;
c[1]=0;
c[2]=WheelPos * 3;
} else {
WheelPos -= 170;
c[0]=0;
c[1]=WheelPos * 3;
c[2]=255 - WheelPos * 3;
}
return c;
}
//fadeOutRainbow
void fadeOutRainbow(int SpeedDelay) {
// 53 times reduce the color brightness by 10%, reaching close to zero (black)
for(int j=1; j<53; j++) {
for(int i=0; i< NUM_LEDS; i++) {
fadeToBlack(i, 26); // 26 / 256 = 10% brightness reduction
}
showStrip();
delay(SpeedDelay);
}
}
//theaterChaseRainbow
// not used for the moment
void theaterChaseRainbow(int SpeedDelay) {
byte *c;
for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
for (int q=0; q < 3; q++) {
for (int i=0; i < NUM_LEDS; i=i+3) {
c = Wheel( (i+j) % 255);
setPixel(i+q, *c, *(c+1), *(c+2)); //turn every third pixel on
}
showStrip();
delay(SpeedDelay);
for (int i=0; i < NUM_LEDS; i=i+3) {
setPixel(i+q, 0,0,0); //turn every third pixel off
}
}
}
}
//Fire
// not used for the moment
void Fire(int Cooling, int Sparking, int SpeedDelay) {
static byte heat[NUM_LEDS];
int cooldown;
// Step 1. Cool down every cell a little
for( int i = 0; i < NUM_LEDS; i++) {
cooldown = random(0, ((Cooling * 10) / NUM_LEDS) + 2);
if(cooldown>heat[i]) {
heat[i]=0;
} else {
heat[i]=heat[i]-cooldown;
}
}
// Step 2. Heat from each cell drifts 'up' and diffuses a little
for( int k= NUM_LEDS - 1; k >= 2; k--) {
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
}
// Step 3. Randomly ignite new 'sparks' near the bottom
if( random(255) < Sparking ) {
int y = random(7);
heat[y] = heat[y] + random(160,255);
//heat[y] = random(160,255);
}
// Step 4. Convert heat to LED colors
for( int j = 0; j < NUM_LEDS; j++) {
setPixelHeatColor(j, heat[j] );
}
showStrip();
delay(SpeedDelay);
}
// used by Fire
void setPixelHeatColor (int Pixel, byte temperature) {
// Scale 'heat' down from 0-255 to 0-191
byte t192 = round((temperature/255.0)*191);
// calculate ramp up from
byte heatramp = t192 & 0x3F; // 0..63
heatramp <<= 2; // scale up to 0..252
// figure out which third of the spectrum we're in:
if( t192 > 0x80) { // hottest
setPixel(Pixel, 255, 255, heatramp);
} else if( t192 > 0x40 ) { // middle
setPixel(Pixel, 255, heatramp, 0);
} else { // coolest
setPixel(Pixel, heatramp, 0, 0);
}
}
//meteorRain
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);
}
}
//meteorRainTransition
void meteorRainTransition(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {
setAll(0,0,0);
// old:
// for(int i = 0; i < NUM_LEDS+NUM_LEDS; i++) {
// new:
for(int i = 0; i < 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();
}