Star
You must be signed in to star a gist
Matrix LED Effect using FastLED
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Matrix effect by Jeremy Williams Modified by crees for use with multiple 8x8 tiles and columns | |
// Designed for Game Frame | |
// http://www.ledseq.com | |
#include <FastLED.h> | |
// LED setup | |
#define kMatrixWidth 16 // add all tiles pixels H & V | |
#define kMatrixHeight 32 | |
#define DATA_PIN 2 // First Pixel Pin 2 on Teensy 3.2 | |
#define DATA_PIN2 14 //Second Pixel Pin 14 On Teensy 3.2 | |
//#define CLOCK_PIN 4 | |
#define NUM_LEDS 512 | |
#define LED_TYPE WS2812 | |
#define COLOR_ORDER GBR | |
uint8_t ledBrightness = 10; | |
CRGB leds[NUM_LEDS]; | |
void setup() | |
{ | |
// LED Init | |
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, 0, NUM_LEDS/2).setDither(0); // Connect to First Column | |
FastLED.addLeds<LED_TYPE, DATA_PIN2, COLOR_ORDER>(leds, NUM_LEDS/2, NUM_LEDS/2).setDither(0); // Connect to Second Column | |
FastLED.setBrightness(ledBrightness); | |
FastLED.show(); | |
} | |
// convert x/y cordinates to LED index on zig-zag grid | |
uint16_t getIndex(uint16_t x, uint16_t y) | |
{ | |
uint16_t index; | |
if (y == 0) | |
{ | |
index = x; | |
} | |
else if (y % 2 == 0) | |
{ | |
index = y * kMatrixWidth + x; | |
} | |
else | |
{ | |
index = ((y * kMatrixWidth) + (kMatrixWidth-1)) - x; | |
} | |
return index; | |
} | |
void loop() | |
{ | |
EVERY_N_MILLIS(75) // falling speed | |
{ | |
// move code downward | |
// start with lowest row to allow proper overlapping on each column | |
for (int8_t row=kMatrixHeight-1; row>=0; row--) | |
{ | |
for (int8_t col=0; col<kMatrixWidth; col++) | |
{ | |
if (leds[getIndex(col, row)] == CRGB(175,255,175)) | |
{ | |
leds[getIndex(col, row)] = CRGB(27,130,39); // create trail | |
if (row < kMatrixHeight-1) leds[getIndex(col, row+1)] = CRGB(175,255,175); | |
} | |
} | |
} | |
// fade all leds | |
for(int i = 0; i < NUM_LEDS; i++) { | |
if (leds[i].g != 255) leds[i].nscale8(192); // only fade trail | |
} | |
// check for empty screen to ensure code spawn | |
bool emptyScreen = true; | |
for(int i = 0; i < NUM_LEDS; i++) { | |
if (leds[i]) | |
{ | |
emptyScreen = false; | |
break; | |
} | |
} | |
// spawn new falling code | |
if (random8(3) == 0 || emptyScreen) // lower number == more frequent spawns | |
{ | |
int8_t spawnX = random8(kMatrixWidth); | |
leds[getIndex(spawnX, 0)] = CRGB(175,255,175 ); | |
} | |
FastLED.show(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment