Skip to content

Instantly share code, notes, and snippets.

@rahulbot
Created April 4, 2025 13:14
Show Gist options
  • Select an option

  • Save rahulbot/de351b365bb9238a76f6236beff45c34 to your computer and use it in GitHub Desktop.

Select an option

Save rahulbot/de351b365bb9238a76f6236beff45c34 to your computer and use it in GitHub Desktop.
An example of fading an LED strip in the background so that the time-based animation doesn't interrupt the main flow of the program. This allows you to keep checkin motors, sensors, etc. while the animation runs.
/**
* An example of fading an LED strip in the background so that the time-based animation
* doesn't interrupt the main flow of the program. This allows you to keep checkin motors,
* sensors, etc. while the animation runs.
* @author Rahul Bhargava
*/
#include <Adafruit_NeoPixel.h>
#define DEBUG_LED_PIN 13
#define TRIGGER_PIN 7
#define CELEBRATION_LIGHTS_PIN 9
#define CELEBRATION_DURATION_MS 4000
#define NUM_CELEBRATION_LEDS 16
uint32_t celebrationColors[NUM_CELEBRATION_LEDS];
Adafruit_NeoPixel celebrationLights(NUM_CELEBRATION_LEDS, CELEBRATION_LIGHTS_PIN);
unsigned long timeToStopCelebrating = 0;
bool celebrating = false;
bool triggered = false;
void setup() {
// debug setup
pinMode(DEBUG_LED_PIN, OUTPUT);
digitalWrite(DEBUG_LED_PIN, LOW);
Serial.begin(115200);
// control setup
pinMode(TRIGGER_PIN, INPUT_PULLUP);
// LED strip setup
pinMode(CELEBRATION_LIGHTS_PIN, OUTPUT);
celebrationLights.begin();
celebrationLights.clear();
celebrationLights.show();
// ready for action
Serial.println("<Celebration Demo Ready>");
}
void loop() {
triggered = !digitalRead(TRIGGER_PIN);
if (triggered && !celebrating) {
Serial.println("Celebrate!");
startCelebrating();
}
if (celebrating) {
updateCelebration();
}
}
// a set of colors we can use in the animation to get a little variety
uint32_t celebrationColorOptions[6] = {
celebrationLights.Color(195, 10, 2),
celebrationLights.Color(235, 22, 7),
celebrationLights.Color(185, 26, 12),
celebrationLights.Color(135, 7, 2),
celebrationLights.Color(225, 2, 11),
celebrationLights.Color(205, 4, 2)
};
// kick off the LED strip animation by setting up some randomization of colors
void startCelebrating() {
celebrating = true;
// randomize a little for cooler effects
for (int p=0;p<NUM_CELEBRATION_LEDS;p++) {
int colorIdx = random(0, 6);
celebrationColors[p] = celebrationColorOptions[colorIdx];
}
// track when to stop
timeToStopCelebrating = millis() + CELEBRATION_DURATION_MS;
}
// manage the animation based on time since we started celebrating
void updateCelebration() {
// time is up, stop celebrating
if (millis() >= timeToStopCelebrating) {
celebrationLights.clear();
celebrationLights.show();
celebrating = false;
return;
}
// grab the current time
int currentTime = timeToStopCelebrating - millis();
// figure out how far into the celebration duration we are (1 to 0)
float progress = (float) currentTime / (float) CELEBRATION_DURATION_MS;
// fade each LED (they are different colors)
for(int p=0; p < NUM_CELEBRATION_LEDS; p++) {
uint32_t fullColor = celebrationColors[p];
uint32_t fadedColor = colorFade(fullColor, progress);
celebrationLights.setPixelColor(p, fadedColor);
}
celebrationLights.show();
}
// helper function to fade a color a little bit
uint32_t colorFade(uint32_t targetColor, float fadePct) {
// Extract RGB components from the targetColor
uint8_t targetRed = (targetColor >> 16) & 0xFF;
uint8_t targetGreen = (targetColor >> 8) & 0xFF;
uint8_t targetBlue = targetColor & 0xFF;
// Calculate faded colors
uint8_t fadedRed = (uint8_t)(targetRed * fadePct);
uint8_t fadedGreen = (uint8_t)(targetGreen * fadePct);
uint8_t fadedBlue = (uint8_t)(targetBlue * fadePct);
// Compose the faded color
uint32_t fadedColor = ((uint32_t)fadedRed << 16) | ((uint32_t)fadedGreen << 8) | fadedBlue;
return fadedColor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment