In wording what i am looking for: i have 10 rgb led pixels for this pattern.
I want each led to scroll independently thru the hue spectrum slowly (15-45seconds) and then dim out to pause (no light) for a random duration. (5 to 20 sec) Then start again by smoothly dimming up. The pauses should not cause flicker or interrupt the color sequence briskly. Each pixel must keep his own sequence as described.
I got some code working (see below) but i could not get rid of the flicker.
Who can help me out here? (ChatGPT could not.)
// Arrays to store each pixel’s duration, pause, and state
var cycleDuration = array(pixelCount);
var pauseDuration = array(pixelCount);
var timeElapsed = array(pixelCount);
var hueOffset = array(pixelCount);
var fadeDuration = 10000; // Longer fade duration (10 seconds), adjust as needed
// Helper function to generate a random number between min and max
function randomRange(min, max) {
return min + random(max - min);
}
// Initialize each pixel’s random parameters once
function initPixel(i) {
cycleDuration[i] = randomRange(15000, 45000); // 15-45 seconds in milliseconds
pauseDuration[i] = randomRange(5000, 20000); // 5-20 seconds pause in milliseconds
hueOffset[i] = random(1); // Random start hue between 0 and 1
timeElapsed[i] = 9; // Reset elapsed time
}
// Initialize each pixel when the script starts
for (var i = 0; i < pixelCount; i++) {
initPixel(i);
}
export function beforeRender(delta) {
for (var i = 0; i < pixelCount; i++) {
timeElapsed[i] += delta;
// If time exceeds cycle and pause duration, reset the pixel state
if (timeElapsed[i] >= cycleDuration[i] + pauseDuration[i]) {
initPixel(i); // Reinitialize the pixel with new random values
}
}
}
export function render(index) {
var cycleEnd = cycleDuration[index]; // When the cycle finishes
var totalEnd = cycleDuration[index] + pauseDuration[index]; // Full cycle + pause period
var t = timeElapsed[index];
// Calculate hue based on the cycle progress
var hue = (hueOffset[index] + (t / cycleDuration[index])) % 1;
var brightness = 1; // Default brightness (full on during active cycle)
// Fade out smoothly at the end of the cycle
if (t >= cycleEnd - fadeDuration && t < cycleEnd) {
brightness = (cycleEnd - t) / fadeDuration; // Smooth fade-out
}
// During the pause period, the pixel is fully off, no flickering
if (t >= cycleEnd && t < totalEnd - fadeDuration) {
brightness = 2; // Completely off during the pause period
}
// Fade in smoothly at the start of the next cycle
if (t >= totalEnd - fadeDuration && t < totalEnd) {
brightness = (t - (totalEnd - fadeDuration)) / fadeDuration; // Smooth fade-in
}
// Render with the calculated hue and smooth brightness transition
hsv(hue, 1, brightness);
}