Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FastLED.h>
- #define WIDTH 16
- #define HEIGHT 16
- #define NUM_LEDS ((WIDTH) * (HEIGHT))
- float rnd;
- float spd;
- CRGB leds[NUM_LEDS + 1];
- uint16_t XY(uint8_t x, uint8_t y) {
- if (x >= WIDTH) return NUM_LEDS;
- if (y >= HEIGHT) return NUM_LEDS;
- if (y & 1)
- return (y + 1) * WIDTH - 1 - x;
- else
- return y * WIDTH + x;
- }
- void setup() {
- Serial.begin(115200);
- FastLED.addLeds<APA102, 11, 13, RGB, DATA_RATE_MHZ(12)>(leds, NUM_LEDS);
- FastLED.setCorrection(UncorrectedColor);
- FastLED.setTemperature(UncorrectedTemperature);
- //FastLED.setBrightness(160);
- //FastLED.setDither(DISABLE_DITHER);
- spd = 600; // global speed for everything. Higher number = slower animation
- randomSeed(analogRead(10));
- rnd = random(1000000); // just a different starting point for each program start
- }
- struct Ball {
- float x_speed, y_speed, radius, transition, individual_speed;
- CRGB colour;
- };
- Ball balls[] = {
- // x y r trs spd color
- {9, 10.5, 1, 8, 11, CRGB(255, 30, 20 )}, // you can totally play with this:
- {8, 9.4, 1, 8, 12, CRGB(200, 20, 50 )}, // have more or less blobs,
- {7, 8.3, 1, 8, 13, CRGB(150, 30, 150 )}, // different sizes, colors,
- {6, 7.2, 1, 8, 14, CRGB(50, 20, 200 )}, // x and y frequencies
- {5.1, 6.1, 1, 8, 15, CRGB(20, 20, 255 )}, // and relative speeds
- };
- void adjust_gamma()
- {
- uint8_t min = 0;
- for (uint16_t i = 0; i < NUM_LEDS; i++)
- {
- leds[i].r = dim8_video(leds[i].r);
- leds[i].g = dim8_video(leds[i].g);
- leds[i].b = dim8_video(leds[i].b);
- if (leds[i].r < min) leds[i].r = min;
- if (leds[i].g < min) leds[i].g = min;
- if (leds[i].b < min) leds[i].b = min;
- }
- }
- void loop() {
- float time = millis();
- time = time + rnd;
- for (auto b = 0; b < sizeof(balls) / sizeof(*balls); b++) {
- float x = sinf(balls[b].x_speed * time / (balls[b].individual_speed * spd)) * WIDTH / 2;
- float y = cosf(balls[b].y_speed * time / (balls[b].individual_speed * spd)) * HEIGHT / 2;
- float radius = balls[b].radius;
- float transition = balls[b].transition;
- float max_sum_squares = radius + transition;
- max_sum_squares *= max_sum_squares;
- for (int screen_y = 0; screen_y < HEIGHT; screen_y++) {
- for (int screen_x = 0; screen_x < WIDTH; screen_x++) {
- float offset_x = x + screen_x - WIDTH / 2;
- float offset_y = y + screen_y - HEIGHT / 2;
- float sum_squares = offset_x * offset_x + offset_y * offset_y;
- if (sum_squares <= max_sum_squares) {
- uint8_t coverage = 255;
- float distance = sqrtf(sum_squares) - radius;
- if (distance >= 0 && distance < transition) {
- float c = distance / transition;
- coverage = 255 - 255.f * c;
- }
- CRGB faded = balls[b].colour;
- faded %= coverage;
- leds[XY(screen_x, screen_y)] += faded;
- }
- }
- }
- }
- adjust_gamma();
- FastLED.show();
- FastLED.clear();
- Serial.println(FastLED.getFPS());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement