#include <FastLED.h>
#define LED_PIN 3
#define COLOR_ORDER GRB
#define CHIPSET WS2811
#define BRIGHTNESS 254
const uint8_t kMatrixWidth = 25;
const uint8_t kMatrixHeight = 8;
const bool kMatrixSerpentineLayout = true;
uint16_t XY( uint8_t x, uint8_t y)
{
uint16_t i;
if( kMatrixSerpentineLayout == false) {
i = (y * kMatrixWidth) + x;
}
if( kMatrixSerpentineLayout == true) {
if( y & 0x01) {
// Odd rows run backwards
uint8_t reverseX = (kMatrixWidth - 1) - x;
i = (y * kMatrixWidth) + reverseX;
} else {
// Even rows run forwards
i = (y * kMatrixWidth) + x;
}
}
return i;
}
#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
CRGB leds_plus_safety_pixel[ NUM_LEDS + 1];
CRGB* const leds( leds_plus_safety_pixel + 1);
uint16_t XYsafe( uint8_t x, uint8_t y)
{
if( x >= kMatrixWidth) return -1;
if( y >= kMatrixHeight) return -1;
return XY(x,y);
}
class Fairylight {
int stripX; //X Origin
int stripY; //Y Origin
uint8_t hue; //fairy color
public:
Fairylight(int xPos, int yPos, uint8_t lightColor) {
stripX = xPos;
stripY = yPos;
hue = lightColor;
}
void Update() {
int newX = stripX+random(-1,2);
int newY = stripY+random(-1,2);
int randTail = hue + random(-40,40);
//Serial.println(randTail);
leds[ XYsafe(stripX, stripY)] = CHSV(randTail,255,255);
//loop back around
if(newX>kMatrixWidth) newX=0;
if(newX<0) newX=kMatrixWidth;
if(newY>kMatrixHeight) newY=0;
if(newY<0) newY=kMatrixHeight;
stripX = newX;
stripY = newY;
leds[ XYsafe(newX, newY)] += CHSV(hue, 255, 255);
if(random(50)==5) leds[ XYsafe(newX, newY)]=CRGB::White;
}
};
Fairylight lights[] = {
Fairylight( random(0,kMatrixWidth), random(0,kMatrixHeight), random(0, 255) ),
Fairylight( random(0,kMatrixWidth), random(0,kMatrixHeight), random(0, 255) ),
Fairylight( random(0,kMatrixWidth), random(0,kMatrixHeight), random(0, 255) ),
Fairylight( random(0,kMatrixWidth), random(0,kMatrixHeight), random(0, 255) ),
Fairylight( random(0,kMatrixWidth), random(0,kMatrixHeight), random(0, 255) ),
Fairylight( random(0,kMatrixWidth), random(0,kMatrixHeight), random(0, 255) ),
Fairylight( random(0,kMatrixWidth), random(0,kMatrixHeight), random(0, 255) ),
Fairylight( random(0,kMatrixWidth), random(0,kMatrixHeight), random(0, 255) ),
Fairylight( random(0,kMatrixWidth), random(0,kMatrixHeight), random(0, 255) ),
Fairylight( random(0,kMatrixWidth), random(0,kMatrixHeight), random(0, 255) )
};
// Demo that USES "XY" follows code below
void loop()
{
Fairies(3,30,20);
}
void setup() {
Serial.begin(115200);
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalSMD5050);
FastLED.setBrightness( BRIGHTNESS );
}
void Fairies(int fairyNum, int flightSpeed, int fadeSpeed) {
EVERY_N_MILLISECONDS(flightSpeed) {
fadeToBlackBy(leds, NUM_LEDS, fadeSpeed);
for (int i = 0; i < fairyNum; i++) {
lights[i].Update();
}
FastLED.show();
};
}