#include <FastLED.h>

#define NUM_LEDS 32
#define SPEED 20

CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<WS2812B, 3, GRB>(leds, NUM_LEDS);
}

void loop() {
  static uint8_t position = 0;
  static int8_t dir = 1;
  static CRGB colour = CRGB::Red;

  EVERY_N_MILLISECONDS(SPEED) {
    fadeToBlackBy(leds, NUM_LEDS, 32);
    leds[position % NUM_LEDS] = colour;
    FastLED.show();

    // move to the next pixel
    position += dir;

    // at either end of the hoop, sometimes choose new parameters
    if (position % NUM_LEDS == 0) {
      if (random8(2) == 0) {
        dir = -dir; // swap direction
        if (dir < 0) {
          position--; // when going backwards, start from the last pixel
        }
      }
      if (random8(8) == 0) {
        // choose a new random colour
        colour = CHSV(random8(), 191 + random8(64), 255);
      }
    }
  }
}