Skip to content

Instantly share code, notes, and snippets.

@Jerware
Last active January 16, 2022 19:15
Show Gist options
  • Select an option

  • Save Jerware/b82ad4768f9935c8acfccc98c9211111 to your computer and use it in GitHub Desktop.

Select an option

Save Jerware/b82ad4768f9935c8acfccc98c9211111 to your computer and use it in GitHub Desktop.

Revisions

  1. Jerware revised this gist May 23, 2017. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions matrixEffect.ino
    Original file line number Diff line number Diff line change
    @@ -61,10 +61,7 @@ void loop()
    if (random8(3) == 0 || emptyScreen) // lower number == more frequent spawns
    {
    int8_t spawnX = random8(kMatrixWidth);
    if (leds[getIndex(spawnX, 0)] != CRGB(175,255,175)) // free pixel
    {
    leds[getIndex(spawnX, 0)] = CRGB(175,255,175 );
    }
    leds[getIndex(spawnX, 0)] = CRGB(175,255,175 );
    }

    FastLED.show();
  2. Jerware revised this gist May 23, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion matrixEffect.ino
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@ void setup()

    void loop()
    {
    EVERY_N_MILLIS(75) // faling speed
    EVERY_N_MILLIS(75) // falling speed
    {
    // move code downward
    // start with lowest row to allow proper overlapping on each column
  3. Jerware created this gist May 23, 2017.
    91 changes: 91 additions & 0 deletions matrixEffect.ino
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    // 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) // faling 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);
    if (leds[getIndex(spawnX, 0)] != CRGB(175,255,175)) // free pixel
    {
    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;
    }