Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Metaballs
- //16x16 rgb led matrix demo
- //Yaroslaw Turbin 20.07.2020
- //https://www.reddit.com/user/ldirko/
- //https://www.reddit.com/user/sutaburosu/
- //thx to u/sutaburosu/ for incredible speedup with square table!!!
- #include "FastLED.h"
- // Matrix size
- #define NUM_ROWS 16
- #define NUM_COLS 16
- #define NUM_LEDS (NUM_ROWS * NUM_COLS)
- // LEDs pin
- #define DATA_PIN 3
- // LED brightness
- #define BRIGHTNESS 255
- // Define the array of leds
- CRGB leds[NUM_LEDS];
- DEFINE_GRADIENT_PALETTE( lava) {
- 0, 255, 30, 0,
- 32, 0, 0, 0,
- 128, 0, 0, 0,
- 150, 180, 180, 0,
- 255, 255, 50, 0
- };
- CRGBPalette16 myPal = lava;
- byte sqrt_LUT[NUM_ROWS][NUM_COLS];
- void setup() {
- Serial.begin(250000);
- FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
- FastLED.setBrightness(BRIGHTNESS);
- for (uint8_t i = 0; i < NUM_COLS; i++)
- for (uint8_t j = 0; j < NUM_ROWS; j++)
- sqrt_LUT[i][j] = dist(0, 0, i, j);
- }
- void loop() {
- uint8_t bx1 = beatsin8(15, 0, NUM_COLS - 1, 0, 0);
- uint8_t by1 = beatsin8(18, 0, NUM_ROWS - 1, 0, 0);
- uint8_t bx2 = beatsin8(28, 0, NUM_COLS - 1, 0, 32);
- uint8_t by2 = beatsin8(23, 0, NUM_ROWS - 1, 0, 32);
- uint8_t bx3 = beatsin8(30, 0, NUM_COLS - 1, 0, 64);
- uint8_t by3 = beatsin8(24, 0, NUM_ROWS - 1, 0, 64);
- uint8_t bx4 = beatsin8(17, 0, NUM_COLS - 1, 0, 128);
- uint8_t by4 = beatsin8(25, 0, NUM_ROWS - 1, 0, 128);
- uint8_t bx5 = beatsin8(19, 0, NUM_COLS - 1, 0, 170);
- uint8_t by5 = beatsin8(21, 0, NUM_ROWS - 1, 0, 170);
- for (uint8_t i = 0; i < NUM_COLS; i++) {
- for (uint8_t j = 0; j < NUM_ROWS; j++) {
- uint16_t sum = distLUT(i, j, bx1, by1);
- sum = qadd8(sum, distLUT(i, j, bx2, by2));
- sum = qadd8(sum, distLUT(i, j, bx3, by3));
- sum = qadd8(sum, distLUT(i, j, bx4, by4));
- sum = qadd8(sum, distLUT(i, j, bx5, by5));
- byte col = constrain(sum, 128, 255);
- leds[XY (i, j)] = ColorFromPalette(myPal, col, BRIGHTNESS);
- }
- }
- FastLED.show();
- static int frame = 0;
- if (frame++ % 32 == 0)
- Serial.println(FastLED.getFPS());
- } //loop
- byte distLUT (uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2) {
- byte a = abs8(y2 - y1);
- byte b = abs8(x2 - x1);
- return sqrt_LUT[a][b];
- }
- byte dist (uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2) {
- byte a = y2 - y1;
- byte b = x2 - x1;
- a *= a;
- b *= b;
- byte dist = 220;
- if (a + b > 0)
- dist = 220.0 / sqrt16(a + b);
- return dist;
- }
- uint16_t XY (uint8_t x, uint8_t y) { return (y * NUM_COLS + x);}
Add Comment
Please, Sign In to add comment