Skip to content

Instantly share code, notes, and snippets.

@mkfink
Created August 23, 2019 00:33
Show Gist options
  • Save mkfink/15e0a187ee7daf9793bc7a5342c57e81 to your computer and use it in GitHub Desktop.
Save mkfink/15e0a187ee7daf9793bc7a5342c57e81 to your computer and use it in GitHub Desktop.
// Dragon staff LED controller using FastLED
// 2019 Mike Fink
// MIT License
#include <stdlib.h>
#include "FastLED.h"
#include <pixeltypes.h>
#include <stdlib.h>
FASTLED_USING_NAMESPACE
#define BUTTON_PIN 2
#define SPINE_PIN 0
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define SPINE_LEDS 18
#define RING_PIN 4
#define RING_LEDS 12
#define BRIGHTNESS 128
#define FRAMES_PER_SECOND 90
//LED modes
#define RAINBOW 0
#define RAINBOW_NO_HUB 1
#define FASTRAIN 3
#define WHOOSH 4
#define WOBBLE 2
#define MAX_PROG 5 //not a program, roll over program index to 0
CRGB ring_leds[RING_LEDS];
CRGB spine_leds[SPINE_LEDS];
bool gReverseDirection = false;
uint8_t gHue = 0; //rainbow cycle hue index
int program = 0; //program number, compare against defines above
int wHue = 0;
// wobble
int wobdex = 0;
int maxwob = 25; //highest hue (orage-ish)
int minwob = -5; //lowest hue (pink-ish)
int direction = 1; //should be +/- 1
//off-orange-red-pink-off gradient palette to fade through
DEFINE_GRADIENT_PALETTE( whoosh_gp ) {
0, 0, 0, 0,
24, 0, 0, 0,
30, 0, 0, 0,
72, 255, 128, 0,
125, 255, 0, 0,
144, 255, 0, 100,
170, 0, 0, 0,
255, 0, 0, 0};
CRGBPalette16 rainPal = Rainbow_gp;
CRGBPalette16 wPal = whoosh_gp;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(BUTTON_PIN, increment_program, FALLING);
FastLED.addLeds<LED_TYPE,SPINE_PIN,COLOR_ORDER>(spine_leds, SPINE_LEDS).setCorrection(TypicalLEDStrip);
FastLED.addLeds<LED_TYPE,RING_PIN,COLOR_ORDER>(ring_leds, RING_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
}
void loop() {
//reset program if it needs to roll over
if (program == MAX_PROG) {
program = 0;
}
switch (program)
{
case RAINBOW:
rainbow(0);
break;
case RAINBOW_NO_HUB:
rainbow(1);
break;
case WOBBLE:
wobble();;
break;
case FASTRAIN:
palette_cycle(rainPal);
break;
case WHOOSH:
palette_cycle(wPal);
break;
default:
break;
}
}
void rainbow(int option) {
//slowly fade through a rainbow gradient fill
if (option == 1) {
fill_rainbow(spine_leds, SPINE_LEDS, gHue, 4);
fill_solid(ring_leds, RING_LEDS, CHSV(0, 0, 0));
}
else {
fill_rainbow(spine_leds, SPINE_LEDS, gHue, 4);
fill_solid(ring_leds, RING_LEDS, CHSV(gHue, 255, 255));
}
FastLED.show();
EVERY_N_MILLISECONDS(100) {gHue--;}
FastLED.delay(1000/FRAMES_PER_SECOND);
}
void palette_cycle(CRGBPalette16 pal) {
//cycle through an arbitrary gradient palette pretty quickly
for( int i = 0; i < SPINE_LEDS; i++) {
spine_leds[i] = ColorFromPalette(pal, wHue - i * 12);
if (i == 0) {
fill_solid(ring_leds, RING_LEDS, ColorFromPalette(pal, wHue - i * 12));
}
}
FastLED.show();
EVERY_N_MILLISECONDS(10) {wHue++;}
FastLED.delay(10/90);
}
void wobble() {
// fade back and forth through color gradient of a section of the rainbow palette
for( int i = 0; i < SPINE_LEDS; i++) {
spine_leds[i] = ColorFromPalette(rainPal, wobdex - i);
if (i == 0) {
fill_solid(ring_leds, RING_LEDS, ColorFromPalette(rainPal, wobdex - i));
}
}
if (wobdex > maxwob) {
direction = -direction;
} else if (wobdex < minwob) {
direction = -direction;
}
wobdex = wobdex + direction;
FastLED.show();
FastLED.delay(5000/FRAMES_PER_SECOND);
}
void increment_program() {
// Interrupt function to increment LED program number with debounce
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
if (interrupt_time - last_interrupt_time > 200)
{
program++;
}
last_interrupt_time = interrupt_time;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment