Advertisement
StePe

Untitled

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