Skip to content

Instantly share code, notes, and snippets.

@gnkarn
Forked from Jerware/matrixEffect.ino
Created August 26, 2017 03:06
Show Gist options
  • Save gnkarn/908383d5b81444362bc2e5421566fdd1 to your computer and use it in GitHub Desktop.
Save gnkarn/908383d5b81444362bc2e5421566fdd1 to your computer and use it in GitHub Desktop.
Matrix LED Effect using FastLED
// 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 NUM_LEDS 256
#define LED_TYPE APA102
#define COLOR_ORDER BGR
uint8_t ledBrightness = 64;
CRGB leds[NUM_LEDS];
void setup()
{
// LED Init
FastLED.addLeds<LED_TYPE, DATA_PIN, CLOCK_PIN, COLOR_ORDER>(leds, NUM_LEDS).setDither(0);
FastLED.setBrightness(ledBrightness);
FastLED.show();
}
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();
}
}
// 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment