Skip to content

Instantly share code, notes, and snippets.

@jasoncoon
Last active November 16, 2015 02:32
Show Gist options
  • Save jasoncoon/ac9f3a4a3e0c2f02bfed to your computer and use it in GitHub Desktop.
Save jasoncoon/ac9f3a4a3e0c2f02bfed to your computer and use it in GitHub Desktop.
// Falling Fairies pattern by Jason Coon, November 2015
// https://gist.github.com/jasoncoon/ac9f3a4a3e0c2f02bfed
#include <FastLED.h>
#define LED_PIN 3
#define COLOR_ORDER GRB
#define CHIPSET WS2811
#define BRIGHTNESS 128
// Using FastLED's XYMatrix mapping. For more information:
// https://github.com/FastLED/FastLED/blob/master/examples/XYMatrix/XYMatrix.ino
// Params for width and height
const uint8_t kMatrixWidth = 16;
const uint8_t kMatrixHeight = 16;
// Param for different pixel layouts
const bool kMatrixSerpentineLayout = true;
#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
CRGB leds[NUM_LEDS];
struct Fairy {
uint8_t x = 0;
uint8_t y = 0;
uint8_t hue = 0;
};
const uint8_t fairyCount = 10;
Fairy fairies[fairyCount];
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;
}
void setup() {
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalSMD5050);
FastLED.setBrightness( BRIGHTNESS );
// set all the fairies to random positions
for (uint8_t i = 0; i < fairyCount; i++) {
fairies[i].x = random(0, kMatrixWidth);
fairies[i].y = random(0, kMatrixHeight);
fairies[i].hue = random(0, 256);
}
}
void loop() {
fadeToBlackBy(leds, NUM_LEDS, 16);
// update fairies
EVERY_N_MILLISECONDS(120) {
for (uint8_t i = 0; i < fairyCount; i++) {
fairies[i].y++;
fairies[i].x += random(0, 3) - 1;
fairies[i].y = fairies[i].y % kMatrixWidth;
fairies[i].x = fairies[i].x % kMatrixHeight;
fairies[i].hue++;
}
}
// draw fairies
for (uint8_t i = 0; i < fairyCount; i++) {
leds[XY(fairies[i].x, fairies[i].y)] = CHSV(fairies[i].hue, 255, 255);
}
FastLED.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment