So, still struggling with the “two differently timed animations at the same time on the same strip” issue.
There is a sketch/function (1), see below, that very slowly lights an APA102C strip with colours fetched from a palette knifed palette (sunrise, day, sunset). That works well. From Mark Kriegsman’s example, I have a sketch/function (2), see below, that adjustably darkens/brightens an APA102C strip (albeit with preset colours). The question is, how I can use the programmatic concept of (2) to influence what is being written to the entire strip with (1), so as not to break the timing of (1) or (2). It’s, as I understand, not an overlay or a mix, but simply a brightness modulation issue.
(1) with EVERY_N_MILLISECONDS over 10 minutes (later several hours) cycles the whole strip through a palette by writing to leds via fill_solid every interval, and now, with the functionality of (2), I would want to randomly modulate (darken/brighten) what is already in leds, but with a different timing.
- On a side note, I realised that random(8) is not really random?
- I saw that FastLED.delay() as currently used in (2) does slow the lava-lamp effect down, with 1000 milliseconds for example, but then the darkening/brightening becomes very “steppy” : (
Any help or hints much appreciated!
(1 - only the function that does the work)
void sky() { // Static variables local to the function, but persist between calls
static const float transitionDuration = 10; // Minutes (10 minutes: one step = 2343.75 milliseconds ~ 2.3 seconds)
static const float interval = ((float)(transitionDuration * 60) / 256) * 1000; // Steps in milliseconds
static byte paletteIndex = 0; // Current gradient palette colour
CRGB colour = ColorFromPalette(activePalette, paletteIndex, maxBrightness, LINEARBLEND); // Or use a built-in palette
fill_solid(leds, ledCount, colour); // Set whole strip to colour fetched from the palette
EVERY_N_MILLISECONDS(interval) { // Timed palette traversal
if (paletteIndex < 240) { // Don’t use 255, see GradientPalette behaving unexpected · Issue #515 · FastLED/FastLED · GitHub
paletteIndex++;
}
}
}
(2 - working sketch)
#include “FastLED.h”
const byte pinData = 3;
const byte pinClock = 4;
const byte ledCount = 144;
byte maxBrightness = 255; // Can be changed on the fly; 10k potentiometer, etc.
#define DARKEST_COLOUR CRGB(24,32,24)
#define BRIGHTEST_COLOUR CRGB(178,186,178)
#define DARKEN_LED CRGB(7,7,7)
#define BRIGHTEN_LED CRGB(19,19,19)
enum {ledConstant, ledBrightens, ledDarkens}; // A flag for the LED states
byte ledState[ledCount];
struct CRGB leds[ledCount];
void setup() {
LEDS.addLeds<APA102, pinData, pinClock, BGR>(leds, ledCount);
LEDS.setBrightness(maxBrightness);
LEDS.setCorrection(Candle); // Candle too warm, Tungsten40W too cold - in-between correction how?
memset(ledState, ledConstant, sizeof(ledState)); // Set all LEDs to constant state
fill_solid(leds, ledCount, DARKEST_COLOUR);
}
void loop()
{
shimmer();
FastLED.show();
FastLED.delay(50); // Higher number = slower, but introduces “steppiness” : (
}
void shimmer()
{
for ( byte i = 0; i < ledCount; i++) { // Go through the entire strip
if ( ledState[i] == ledConstant) { // If LED is in constant state…
if ( random8() < 3) { // …randomly select it based on a threshold (higher number = busier shimmer)…
ledState[i] = ledBrightens; // …and set it to brightening state
}
} else if ( ledState[i] == ledBrightens ) { // If LED is in brightening state...
if ( leds[i] >= BRIGHTEST_COLOUR ) { // ...and reached brightest colour...
ledState[i] = ledDarkens; // ...set it to darkening state
} else {
leds[i] += BRIGHTEN_LED; // If not, continue brightening it
}
} else {
if ( leds[i] <= DARKEST_COLOUR ) { // If LED reached darkest colour...
leds[i] = DARKEST_COLOUR; // ...keep it there...
ledState[i] = ledConstant; // ...and set it to constant state
} else {
leds[i] -= DARKEN_LED; // If not, continue darkening it
}
}
}
}