ldirko

Simple Metaballs

Jul 21st, 2020 (edited)
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Metaballs  
  2. //16x16 rgb led matrix demo
  3. //Yaroslaw Turbin 20.07.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.  
  20. void setup() {
  21.   FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  22.   FastLED.setBrightness(BRIGHTNESS);
  23. }
  24.  
  25. void loop() {
  26.  
  27.  uint8_t bx1 = beatsin8(15, 0, NUM_COLS - 1, 0, 0);
  28.   uint8_t by1 = beatsin8(18, 0, NUM_ROWS - 1, 0, 0);
  29.   uint8_t bx2 = beatsin8(28, 0, NUM_COLS - 1, 0, 32);
  30.   uint8_t by2 = beatsin8(23, 0, NUM_ROWS - 1, 0, 32);
  31.   uint8_t bx3 = beatsin8(30, 0, NUM_COLS - 1, 0, 64);
  32.   uint8_t by3 = beatsin8(24, 0, NUM_ROWS - 1, 0, 64);
  33.   uint8_t bx4 = beatsin8(17, 0, NUM_COLS - 1, 0, 128);
  34.   uint8_t by4 = beatsin8(25, 0, NUM_ROWS - 1, 0, 128);
  35.   uint8_t bx5 = beatsin8(19, 0, NUM_COLS - 1, 0, 170);
  36.   uint8_t by5 = beatsin8(21, 0, NUM_ROWS - 1, 0, 170);
  37.  
  38.   for (int i = 0; i < NUM_COLS; i++)    {
  39.     for (int j = 0; j < NUM_ROWS; j++) {
  40.  
  41.       byte  sum =  dist(i, j, bx1, by1);
  42.       sum = qadd8(sum, dist(i, j, bx2, by2));
  43.       sum = qadd8(sum, dist(i, j, bx3, by3));
  44.       sum = qadd8(sum, dist(i, j, bx4, by4));
  45.       sum = qadd8(sum, dist(i, j, bx5, by5));
  46.      
  47.       leds[XY (i, j)] =  ColorFromPalette(HeatColors_p , sum+220, BRIGHTNESS);
  48.     }}
  49.  
  50. blur2d(leds,NUM_COLS, NUM_ROWS, 32 );
  51. FastLED.show();
  52.  
  53. } //loop
  54.  
  55. byte dist (uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2)  {
  56. int a = y2-y1;
  57. int b = x2-x1;
  58. a *= a;
  59. b *= b;
  60. byte dist = 220/ sqrt16(a+b);
  61. return dist;
  62. }
  63.  
  64. uint16_t XY (uint8_t x, uint8_t y) {return (y * NUM_COLS + x);}
Add Comment
Please, Sign In to add comment