#include "FastLED.h"
#include "TaskScheduler.h"

#define LED_COUNT 100 // number of LED pixels in the circle
#define LED_PIN 25 // pin the LED strip is connected to

CRGB leds[LED_COUNT]; // create an array of LED pixels

Scheduler runner; // create a task scheduler
static int length = 10;

// define a task that animates the pixels moving around the circle
Task animatePixels(0, TASK_FOREVER, []() {
  static int i = 0;
  static int speed = 5;
  static int delay = 50;

  int start = i;
  int end = (i + length) % LED_COUNT;
  for (int j = 0; j < LED_COUNT; j++) {
    if (j >= start && j <= end) {
      leds[j] = CRGB::Red; // set the moving pixels to red
    } else {
      leds[j] = CRGB::Black; // set the non-moving pixels to black
    }
  }
  FastLED.show(); // update the LED strip
  i = (i + 1) % LED_COUNT; // increment the index for the next iteration
});

// define a task that shortens and lengthens the length of the moving pixels
Task changeLength(5000, TASK_FOREVER, []() {
  static int speed = 5;

  for (int i = 0; i < speed; i++) {
    length++;
    if (length > LED_COUNT / 2) {
      length = LED_COUNT / 2;
    }
  }
  for (int i = 0; i < speed; i++) {
    length--;
    if (length < 1) {
      length = 1;
    }
  }
});

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, LED_COUNT); // initialize FastLED
  runner.init(); // initialize the task scheduler
  runner.addTask(animatePixels); // add the animatePixels task to the scheduler
  runner.addTask(changeLength); // add the changeLength task to the scheduler
  animatePixels.enable(); // enable the animatePixels task
  changeLength.enable(); // enable the changeLength task
}

void loop() {
  runner.execute(); // run the tasks in the scheduler
}