StePe

Untitled

May 22nd, 2022
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. // Main code by u/sutaburosu
  2. // tweaks by u/StefanPetrick
  3.  
  4. #include <FastLED.h>
  5.  
  6. #define WIDTH 16
  7. #define HEIGHT 16
  8. #define NUM_LEDS ((WIDTH) * (HEIGHT))
  9.  
  10. float rnd;
  11. float spd;
  12.  
  13. CRGB leds[NUM_LEDS + 1];
  14.  
  15. uint16_t XY(uint8_t x, uint8_t y) {
  16. if (x >= WIDTH) return NUM_LEDS;
  17. if (y >= HEIGHT) return NUM_LEDS;
  18. if (y & 1)
  19. return (y + 1) * WIDTH - 1 - x;
  20. else
  21. return y * WIDTH + x;
  22. }
  23.  
  24.  
  25. void setup() {
  26. Serial.begin(115200);
  27. FastLED.addLeds<APA102, 11, 13, RGB, DATA_RATE_MHZ(12)>(leds, NUM_LEDS);
  28. FastLED.setCorrection(UncorrectedColor);
  29. FastLED.setTemperature(UncorrectedTemperature);
  30. //FastLED.setBrightness(160);
  31. //FastLED.setDither(DISABLE_DITHER);
  32.  
  33. spd = 600; // global speed for everything. Higher number = slower animation
  34.  
  35. randomSeed(analogRead(10));
  36. rnd = random(1000000); // just a different starting point for each program start
  37. }
  38.  
  39. struct Ball {
  40. float x_speed, y_speed, radius, transition, individual_speed;
  41. CRGB colour;
  42. };
  43.  
  44. Ball balls[] = {
  45.  
  46. // x y r trs spd color
  47.  
  48. {9, 10.5, 1, 8, 11, CRGB(255, 30, 20 )}, // you can totally play with this:
  49. {8, 9.4, 1, 8, 12, CRGB(200, 20, 50 )}, // have more or less blobs,
  50. {7, 8.3, 1, 8, 13, CRGB(150, 30, 150 )}, // different sizes, colors,
  51. {6, 7.2, 1, 8, 14, CRGB(50, 20, 200 )}, // x and y frequencies
  52. {5.1, 6.1, 1, 8, 15, CRGB(20, 20, 255 )}, // and relative speeds
  53. };
  54.  
  55. void adjust_gamma()
  56. {
  57. uint8_t min = 0;
  58. for (uint16_t i = 0; i < NUM_LEDS; i++)
  59. {
  60. leds[i].r = dim8_video(leds[i].r);
  61. leds[i].g = dim8_video(leds[i].g);
  62. leds[i].b = dim8_video(leds[i].b);
  63.  
  64. if (leds[i].r < min) leds[i].r = min;
  65. if (leds[i].g < min) leds[i].g = min;
  66. if (leds[i].b < min) leds[i].b = min;
  67. }
  68. }
  69.  
  70. void loop() {
  71.  
  72. float time = millis();
  73. time = time + rnd;
  74. for (auto b = 0; b < sizeof(balls) / sizeof(*balls); b++) {
  75. float x = sinf(balls[b].x_speed * time / (balls[b].individual_speed * spd)) * WIDTH / 2;
  76. float y = cosf(balls[b].y_speed * time / (balls[b].individual_speed * spd)) * HEIGHT / 2;
  77. float radius = balls[b].radius;
  78. float transition = balls[b].transition;
  79. float max_sum_squares = radius + transition;
  80. max_sum_squares *= max_sum_squares;
  81.  
  82. for (int screen_y = 0; screen_y < HEIGHT; screen_y++) {
  83. for (int screen_x = 0; screen_x < WIDTH; screen_x++) {
  84. float offset_x = x + screen_x - WIDTH / 2;
  85. float offset_y = y + screen_y - HEIGHT / 2;
  86. float sum_squares = offset_x * offset_x + offset_y * offset_y;
  87. if (sum_squares <= max_sum_squares) {
  88. uint8_t coverage = 255;
  89. float distance = sqrtf(sum_squares) - radius;
  90. if (distance >= 0 && distance < transition) {
  91. float c = distance / transition;
  92. coverage = 255 - 255.f * c;
  93. }
  94. CRGB faded = balls[b].colour;
  95. faded %= coverage;
  96. leds[XY(screen_x, screen_y)] += faded;
  97. }
  98. }
  99. }
  100. }
  101. adjust_gamma();
  102. FastLED.show();
  103. FastLED.clear();
  104.  
  105. Serial.println(FastLED.getFPS());
  106. }
Advertisement
Add Comment
Please, Sign In to add comment