ldirko

Digital Rain

Aug 24th, 2020 (edited)
1,251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Digital Rain implementation
  2. //fastled 16x16 matrix demo
  3. //Yaroslaw Turbin 24.08.2020
  4. //https://vk.com/ldirko
  5. //https://www.reddit.com/user/ldirko/
  6.  
  7. #include "FastLED.h"
  8.  
  9. // Matrix size
  10. #define NUM_ROWS 16
  11. #define NUM_COLS 16
  12. // LEDs pin
  13. #define DATA_PIN 3
  14. // LED brightness
  15. #define BRIGHTNESS 255
  16. #define NUM_LEDS NUM_ROWS * NUM_COLS
  17. // Define the array of leds
  18. CRGB leds[NUM_LEDS];
  19. byte rain[NUM_LEDS];
  20. byte counter = 1;
  21. int speed = 1;
  22.  
  23. void setup() {
  24. FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  25. FastLED.setBrightness(BRIGHTNESS);
  26. raininit();
  27. }
  28.  
  29. void loop() {
  30. EVERY_N_MILLISECONDS(100) {
  31. updaterain();
  32. FastLED.show();
  33. }
  34. EVERY_N_MILLISECONDS(30) {changepattern();}
  35. } //loop
  36.  
  37. void changepattern () {
  38. int rand1 = random16 (NUM_LEDS);
  39. int rand2 = random16 (NUM_LEDS);
  40. if ((rain[rand1] == 1) && (rain[rand2] ==0) )    //simple get two random dot 1 and 0 and swap it,
  41.     {rain[rand1] = 0; rain[rand2] = 1;}           //this will not change total number of dots
  42. } //changepattern
  43.  
  44. void raininit() {                               //init array of dots. run once
  45. for (int i = 0; i < NUM_LEDS; i++) {
  46. if (random8(20) == 0) { rain[i] = 1; }       //random8(20) number of dots. decrease for more dots
  47.                  else { rain[i] = 0; }                
  48. }
  49. } //raininit
  50.  
  51. void updaterain() {
  52. for (byte i = 0; i < NUM_COLS; i++) {
  53. for (byte j = 0; j < NUM_ROWS; j++) {
  54. byte layer = rain[XY(i, ((j + speed + random8(2) + NUM_ROWS) % NUM_ROWS))];   //fake scroll based on shift coordinate
  55.                                                                               // random8(2) add glitchy look
  56. if (layer) { leds[XY((NUM_COLS - 1) - i, (NUM_ROWS - 1) - j)] = CHSV(110, 255, BRIGHTNESS); }  
  57. }}
  58.  
  59. speed ++;
  60. fadeToBlackBy(leds, NUM_LEDS, 40);
  61. blurRows(leds, NUM_COLS, NUM_ROWS, 16);      //if you want
  62. }//updaterain
  63.  
  64. uint16_t XY (uint8_t x, uint8_t y) { return (y * NUM_COLS + x);}   
  65.  
  66.  
Add Comment
Please, Sign In to add comment