#include "FastLED.h" // How many leds in your strip? #define NUM_LEDS 120 // For led chips like Neopixels, which have a data line, ground, and power, you just // need to define DATA_PIN. For led chipsets that are SPI based (four wires - data, clock, // ground, and power), like the LPD8806, define both DATA_PIN and CLOCK_PIN #define DATA_PIN D4 #define BUTTON_PIN D3 #define BUTTON_VCC D2 // Define the array of leds CRGB leds[NUM_LEDS]; static int ticks = 0; static void setPixel(float x, CHSV color) { #if 0 || ALTERNATE_WITH_UNANTIALIASED if(ticks & 0x080) { leds[(int)x] = color; return; } #endif CHSV color2 = color; float t = x - floor(x); color2.value *= t; color.value *= 1.0 - t; int xI = (int)floor(x); if(xI >= 0 && x < NUM_LEDS) leds[xI] += color; xI++; if(xI >= 0 && x < NUM_LEDS) leds[xI] += color2; } float randomf(float low, float high) { #define rfk 1000113 float x = random(0, rfk) * (high - low) / (float)rfk + low; return x; } float migrate(float x, float amount, float towards) { float del = towards - x; if(abs(del) <= amount) x = towards; else if(del < 0) x -= amount; else x += amount; return x; } class Sparkle { public: int active; float x; float rate; CHSV color; Sparkle() { this->x = 0; this->rate = 1; this->color = CHSV(0,255,255); } Sparkle(float x, float rate, int hue) { this->x = x; this->rate = rate; this->color.hue = hue; this->color.saturation = 255; this->color.value = 255; this->active = 1; } int tick(float moveAmount) { if(this->active) { this->x += moveAmount; if(x < 0 || x >= NUM_LEDS) { active = 0; } else { setPixel(this->x, this->color); } } return this->active; } int tick() { this->tick(this->rate); return this->active; } int tickTowards(float amount, float towards) { this->x = migrate(this->x, amount, towards); this->tick(0); return this->active; } }; #define SPARKLE_COUNT 50 Sparkle sparkles[SPARKLE_COUNT]; void setup() { Serial.begin(57600); Serial.println("resetting"); LEDS.addLeds(leds,NUM_LEDS); LEDS.setBrightness(180); for(int i = 0; i < SPARKLE_COUNT; i++) { sparkles[i] = Sparkle(0, randomf(0.03, 0.3), random(0,256)); sparkles[i].active = 0; } pinMode(BUTTON_PIN, INPUT_PULLUP); // set pin to input pinMode(BUTTON_VCC, OUTPUT); // used to ground the button ha ha digitalWrite(BUTTON_VCC, 0); } void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } } void clear() { for(int i = 0; i < NUM_LEDS; i++) { leds[i] = CRGB(0,0,0); } } bool wasButton; int buttonDest = NUM_LEDS/2; void loop() { ticks++; clear(); bool isButton = digitalRead(BUTTON_PIN); if(isButton && !wasButton) buttonDest = random(10, NUM_LEDS - 10); wasButton = isButton; if(!isButton) { for(int ix = 0; ix < SPARKLE_COUNT; ix++) { sparkles[ix].tickTowards(2.3, buttonDest); } FastLED.show(); delay(10); return; } for(int ix = 0; ix < SPARKLE_COUNT; ix++) { Sparkle &s = sparkles[ix]; s.tick(); if(s.active == 0 && random(1,20) == 2) { int dir = random(0, 2); s.x = randomf(0,2); s.x = dir ? s.x : NUM_LEDS - 1 - s.x; float maxRate = 0.4; if(random(1,20) == 2) maxRate = 2; s.rate = randomf(0.05, maxRate) * (dir ? +1 : -1); s.color.hue = random(0,256); s.color.value = random(40,220); s.active = 1; } } FastLED.show(); delay(10); }