//Digital Rain implementation for fibonacci 256 
//fastled fibonacci 256 leds  demo
//Yaroslaw Turbin 28.01.2020
//https://vk.com/ldirko
//https://www.reddit.com/user/ldirko/
//https://twitter.com/ldir_ko

//https://wokwi.com/arduino/projects/288948170884383245

#include "FastLED.h"

// Matrix size
#define NUM_ROWS 20
#define NUM_COLS 20
// LEDs pin
#define DATA_PIN 3
// LED brightness
#define BRIGHTNESS 255
#define NUM_LEDS NUM_ROWS * NUM_COLS
// Define the array of leds
CRGB leds[257];
byte rain[NUM_LEDS];

static const uint16_t FibonPlanarTable[] PROGMEM ={

256, 256, 256, 256, 256, 256, 256, 256, 183, 205, 206, 207, 256, 256, 256, 256, 256, 256, 256, 256, 
256, 256, 256, 256, 256, 182, 185, 184, 204, 256, 211, 210, 209, 208, 256, 256, 256, 256, 256, 256, 
256, 256, 256, 256, 181, 186, 256, 203, 213, 212, 225, 226, 227, 228, 229, 230, 256, 256, 256, 256, 
256, 256, 256, 159, 180, 187, 202, 214, 256, 224, 256, 238, 237, 236, 235, 234, 231, 256, 256, 256, 
256, 256, 160, 179, 188, 256, 201, 215, 223, 240, 239, 247, 248, 249, 250, 251, 233, 232, 256, 256, 
256, 158, 161, 178, 189, 200, 216, 222, 241, 256, 246, 256, 3, 4, 5, 6, 252, 253, 256, 256, 
256, 157, 162, 177, 190, 199, 217, 221, 242, 245, 256, 2, 22, 21, 20, 19, 7, 8, 254, 256, 
256, 156, 163, 176, 191, 198, 256, 218, 220, 244, 1, 23, 26, 27, 28, 256, 18, 256, 9, 255, 
135, 155, 164, 175, 256, 192, 197, 256, 219, 243, 24, 25, 48, 47, 256, 29, 256, 17, 256, 10, 
136, 154, 165, 256, 174, 256, 193, 196, 195, 256, 0, 49, 50, 51, 46, 256, 30, 256, 16, 11, 
134, 137, 153, 166, 256, 173, 172, 194, 171, 256, 98, 74, 73, 72, 52, 45, 256, 31, 15, 12, 
133, 138, 256, 152, 167, 168, 169, 170, 147, 146, 256, 97, 75, 256, 71, 53, 44, 32, 14, 256, 
256, 132, 139, 256, 151, 150, 149, 148, 145, 123, 122, 99, 96, 76, 70, 54, 43, 33, 13, 256, 
256, 111, 131, 140, 141, 142, 143, 144, 256, 124, 121, 100, 95, 77, 69, 55, 42, 34, 256, 256, 
256, 256, 112, 130, 129, 256, 256, 256, 125, 256, 120, 101, 94, 78, 68, 56, 41, 35, 256, 256, 
256, 256, 110, 113, 114, 128, 127, 126, 118, 119, 102, 256, 93, 79, 67, 57, 40, 36, 256, 256, 
256, 256, 256, 109, 108, 115, 116, 117, 104, 103, 256, 92, 80, 66, 256, 58, 39, 256, 256, 256, 
256, 256, 256, 256, 256, 107, 106, 105, 256, 90, 91, 81, 65, 256, 59, 38, 256, 256, 256, 256, 
256, 256, 256, 256, 256, 86, 87, 88, 89, 83, 82, 64, 256, 60, 37, 256, 256, 256, 256, 256, 
256, 256, 256, 256, 256, 256, 256, 85, 84, 62, 63, 256, 61, 256, 256, 256, 256, 256, 256, 256

};  

void setup() {
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
  raininit();
}

void loop() {
  EVERY_N_MILLISECONDS(80) {
    updaterain();
  }
  EVERY_N_MILLISECONDS(15) {
    changepattern();
  }
FastLED.show();

} //loop

void changepattern () {
  int rand1 = random16 (NUM_LEDS);
  int rand2 = random16 (NUM_LEDS);
  if (rain[rand1] && !rain[rand2]) {rain[rand1] = 0; rain[rand2] = 1;}
} //changepattern

void raininit() {     //init array of dots. run once
  for (int i = 0; i < NUM_LEDS; i++) rain[i] = !random8(15)? 1:0; 
} //raininit


void updaterain() {

static int speed = 1;

for (byte j = 0; j < NUM_ROWS; j++) {
   int yindex=(j + speed) % NUM_ROWS*NUM_COLS;
      for (byte i = 0; i < NUM_COLS; i++) {
         byte layer = rain[yindex+i];   
         if (layer) leds[XY_fibon((NUM_COLS - 1) - i, (NUM_ROWS - 1) - j)].setHue (100);
        
}}

fadeToBlackBy(leds, 256, 30);
speed ++;
}//updaterain

uint16_t XY_fibon(byte x, byte y) { 
uint16_t ledsindex = pgm_read_word (FibonPlanarTable+y*NUM_COLS+x);
return (ledsindex);
}
FPS: 0
Power: 0.00W