Advertisement
ldirko

Digital Rain for fibonacci 256

Jan 28th, 2021
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Digital Rain implementation for fibonacci 256
  2. //fastled fibonacci 256 leds  demo
  3. //Yaroslaw Turbin 28.01.2020
  4. //https://vk.com/ldirko
  5. //https://www.reddit.com/user/ldirko/
  6. //https://twitter.com/ldir_ko
  7.  
  8. //https://wokwi.com/arduino/projects/288948170884383245
  9.  
  10. #include "FastLED.h"
  11.  
  12. // Matrix size
  13. #define NUM_ROWS 20
  14. #define NUM_COLS 20
  15. // LEDs pin
  16. #define DATA_PIN 3
  17. // LED brightness
  18. #define BRIGHTNESS 255
  19. #define NUM_LEDS NUM_ROWS * NUM_COLS
  20. // Define the array of leds
  21. CRGB leds[257];
  22. byte rain[NUM_LEDS];
  23.  
  24. void setup() {
  25.   FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  26.   FastLED.setBrightness(BRIGHTNESS);
  27.   raininit();
  28. }
  29.  
  30. void loop() {
  31.   EVERY_N_MILLISECONDS(80) {
  32.     updaterain();
  33.   }
  34.   EVERY_N_MILLISECONDS(15) {
  35.     changepattern();
  36.   }
  37. FastLED.show();
  38.  
  39. } //loop
  40.  
  41. void changepattern () {
  42.   int rand1 = random16 (NUM_LEDS);
  43.   int rand2 = random16 (NUM_LEDS);
  44.   if (rain[rand1] && !rain[rand2]) {rain[rand1] = 0; rain[rand2] = 1;}
  45. } //changepattern
  46.  
  47. void raininit() {     //init array of dots. run once
  48.   for (int i = 0; i < NUM_LEDS; i++) rain[i] = !random8(15)? 1:0;
  49. } //raininit
  50.  
  51. void updaterain() {
  52. static const CRGB hue = CHSV(100,255,255);
  53. static uint16_t speed;
  54.  
  55. for (byte j = 0; j < NUM_ROWS; j++) {
  56.   uint16_t index = (j - speed) % (NUM_ROWS) * NUM_COLS;
  57. for (byte i = 0; i < NUM_COLS; i++) {
  58.       uint16_t ledsindex = XY(i, j);
  59.       leds[ledsindex].fadeToBlackBy(40);
  60.       if (rain[index + i] & ledsindex!=256) leds[ledsindex]=hue;
  61. }}
  62. speed++;
  63. }
  64.  
  65. uint16_t XY(byte x, byte y) {
  66. static const uint16_t FibonPlanarTable[] PROGMEM ={
  67. 256, 256, 256, 256, 256, 256, 256, 256, 247, 213, 234, 255, 256, 256, 256, 256, 256, 256, 256, 256,
  68. 256, 256, 256, 256, 256, 239, 205, 226, 192, 256, 179, 200, 221, 242, 256, 256, 256, 256, 256, 256,
  69. 256, 256, 256, 256, 218, 184, 256, 171, 137, 158, 124, 145, 166, 187, 208, 229, 256, 256, 256, 256,
  70. 256, 256, 256, 252, 197, 163, 150, 116, 256, 103, 256, 111, 132, 153, 174, 195, 250, 256, 256, 256,
  71. 256, 256, 231, 176, 142, 256, 129, 95, 82, 69, 90, 77, 98, 119, 140, 161, 216, 237, 256, 256,
  72. 256, 244, 210, 155, 121, 108, 74, 61, 48, 256, 56, 256, 64, 85, 106, 127, 182, 203, 256, 256,
  73. 256, 223, 189, 134, 100, 87, 53, 40, 27, 35, 256, 43, 51, 72, 93, 114, 148, 169, 224, 256,
  74. 256, 202, 168, 113, 79, 66, 256, 32, 19, 14, 22, 30, 38, 59, 80, 256, 135, 256, 190, 245,
  75. 236, 181, 147, 92, 256, 58, 45, 256, 11, 6, 9, 17, 25, 46, 256, 101, 256, 156, 256, 211,
  76. 215, 160, 126, 256, 71, 256, 37, 24, 3, 256, 1, 4, 12, 33, 67, 256, 122, 256, 177, 232,
  77. 249, 194, 139, 105, 256, 50, 29, 16, 8, 0, 2, 7, 20, 41, 54, 88, 256, 143, 198, 253,
  78. 228, 173, 256, 118, 84, 63, 42, 21, 13, 5, 256, 15, 28, 256, 62, 75, 109, 164, 219, 256,
  79. 256, 207, 152, 256, 97, 76, 55, 34, 26, 18, 10, 23, 36, 49, 83, 96, 130, 185, 240, 256,
  80. 256, 241, 186, 131, 110, 89, 68, 47, 256, 39, 31, 44, 57, 70, 104, 117, 151, 206, 256, 256,
  81. 256, 256, 220, 165, 144, 256, 256, 256, 60, 256, 52, 65, 78, 91, 125, 138, 172, 227, 256, 256,
  82. 256, 256, 254, 199, 178, 123, 102, 81, 94, 73, 86, 256, 99, 112, 146, 159, 193, 248, 256, 256,
  83. 256, 256, 256, 233, 212, 157, 136, 115, 128, 107, 256, 120, 133, 167, 256, 180, 214, 256, 256, 256,
  84. 256, 256, 256, 256, 256, 191, 170, 149, 256, 162, 141, 154, 188, 256, 201, 235, 256, 256, 256, 256,
  85. 256, 256, 256, 256, 256, 246, 225, 204, 183, 196, 175, 209, 256, 222, 256, 256, 256, 256, 256, 256,
  86. 256, 256, 256, 256, 256, 256, 256, 238, 217, 251, 230, 256, 243, 256, 256, 256, 256, 256, 256, 256 };  
  87. uint16_t ledsindex = pgm_read_word (FibonPlanarTable+y*NUM_COLS+x);
  88. return (ledsindex);
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement