// Matrix effect by Jeremy Williams
// Designed for Game Frame
// http://www.ledseq.com
#include <FastLED.h>
// LED setup
#define kMatrixWidth 16
#define kMatrixHeight 16
//#define DATA_PIN 2
//#define CLOCK_PIN 4
#define LED_PIN 2
#define NUM_LEDS 256
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
uint8_t ledBrightness = 100;
CRGB leds[NUM_LEDS];
void setup()
{
// LED Init
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setDither(0);
FastLED.setBrightness(ledBrightness);
FastLED.show();
}
void loop()
{
EVERY_N_MILLIS(100) // 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();
}
}
// 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;
}