Skip to content

Instantly share code, notes, and snippets.

@gelicia
Created August 13, 2026 00:33
Show Gist options
  • Select an option

  • Save gelicia/739370da0306143a9ba8a69d0e23d5fb to your computer and use it in GitHub Desktop.

Select an option

Save gelicia/739370da0306143a9ba8a69d0e23d5fb to your computer and use it in GitHub Desktop.
mask_fastled.ino
// running on a dfrobot beetle
#include <FastLED.h>
#define LED_PIN_SIDES 10 // sides
#define LED_PIN_EYES 9 // eyes
#define LED_PIN_CENTER 11 // center
#define BUTTON_PIN A0
#define LED_COUNT_SIDES 47
#define LED_COUNT_EYES 16
#define LED_COUNT_CENTER 28
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define BRIGHTNESS 50
CRGB leds_sides[LED_COUNT_SIDES];
CRGB leds_eyes[LED_COUNT_EYES];
CRGB leds_center[LED_COUNT_CENTER];
#define UPDATES_PER_SECOND 100
int mode = 1;
int storeSensorVal = 1;
uint8_t startIndex = 0;
int numModes = 3; // mode 0 breathe purple, mode 1 solid purple, mode 2 rainbow
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
delay( 3000 ); // power-up safety delay
FastLED.addLeds<LED_TYPE, LED_PIN_SIDES, COLOR_ORDER>(leds_sides, LED_COUNT_SIDES);
FastLED.addLeds<LED_TYPE, LED_PIN_EYES, COLOR_ORDER>(leds_eyes, LED_COUNT_EYES);
FastLED.addLeds<LED_TYPE, LED_PIN_CENTER, COLOR_ORDER>(leds_center, LED_COUNT_CENTER);
FastLED.setBrightness(BRIGHTNESS);
FastLED.clearData();
}
void loop() {
int sensorVal = digitalRead(BUTTON_PIN);
Serial.println(sensorVal);
if (sensorVal == 0 && storeSensorVal == 1) {
mode++;
if (mode == numModes) {
mode = 0;
}
}
storeSensorVal = sensorVal;
static uint8_t startIndex = 0;
startIndex = startIndex + 1; /* motion speed */
FillLEDsWithMode(startIndex);
FastLED.show();
FastLED.delay(1);
}
void FillLEDsWithMode(uint8_t colorIndex)
{
if (mode == 0) { // breathe purple
float val = (exp(sin(millis() / 2000.0 * PI)) - 0.36787944) * 108.0;
CRGB origColor = CRGB(197,0,246);
CRGB fadedColor = origColor.fadeToBlackBy(val);
fill_solid(leds_sides, LED_COUNT_SIDES, fadedColor );
fill_solid(leds_eyes, LED_COUNT_EYES, fadedColor );
fill_solid(leds_center, LED_COUNT_CENTER, fadedColor );
} else if ( mode == 1 ) { // solid purple
fill_solid(leds_sides, LED_COUNT_SIDES, CRGB(197,0,246) );
fill_solid(leds_eyes, LED_COUNT_EYES, CRGB(197,0,246) );
fill_solid(leds_center, LED_COUNT_CENTER, CRGB(197,0,246) );
} else { // rainbow
uint8_t colorIndexSides = colorIndex;
uint8_t colorIndexEyes = colorIndex;
uint8_t colorIndexCenter = colorIndex;
for ( int i = 0; i < LED_COUNT_SIDES; i++) {
uint8_t idx = map(i, 0, LED_COUNT_SIDES, 0, 255) + colorIndexSides;
leds_sides[i] = ColorFromPalette( RainbowColors_p, idx, BRIGHTNESS, NOBLEND);
colorIndexSides += 3;
}
for ( int i = 0; i < LED_COUNT_EYES; i++) {
uint8_t idx = map(i, 0, LED_COUNT_EYES, 0, 255) + colorIndexEyes;
leds_eyes[i] = ColorFromPalette(RainbowColors_p, idx, BRIGHTNESS, NOBLEND);
colorIndexEyes += 3;
}
for ( int i = 0; i < LED_COUNT_CENTER; i++) {
uint8_t idx = map(i, 0, LED_COUNT_CENTER, 0, 255) + colorIndexCenter;
leds_center[i] = ColorFromPalette( RainbowColors_p, idx, BRIGHTNESS, NOBLEND);
colorIndexCenter += 3;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment