#include <FastLED.h>

#define NUM_LEDS 43
#define LED_PIN 4

// Stepper pins
const int dirPin = 2;
const int stepPin = 3;

CRGB leds[NUM_LEDS];

// Motor timing
unsigned long lastStepTime = 0;
const unsigned int stepInterval = 497; // microseconden

// LED timing
unsigned long lastLedTime = 0;
const unsigned long ledInterval = 2; // ongeveer 47/22 ms

int ledPos = 0;
bool forward = true;

void setup() {
  // LEDs
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(50);
  FastLED.clear();
  FastLED.show();

  // Stepper
  pinMode(dirPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  digitalWrite(dirPin, HIGH); // clockwise
}

void loop() {

  // ===== Stepper motor =====
  unsigned long currentMicros = micros();

  if (currentMicros - lastStepTime >= stepInterval) {
    lastStepTime = currentMicros;

    digitalWrite(stepPin, HIGH);
    delayMicroseconds(2); // minimale pulsbreedte
    digitalWrite(stepPin, LOW);
  }

  // ===== LED animatie =====
  unsigned long currentMillis = millis();

  if (currentMillis - lastLedTime >= ledInterval) {
    lastLedTime = currentMillis;

    fill_solid(leds, NUM_LEDS, CRGB::Black);

    leds[ledPos] = CRGB::Yellow;
    leds[11 + ledPos] = CRGB::Yellow;
    leds[22 + ledPos] = CRGB::Yellow;

    if (33 + ledPos < NUM_LEDS) {
      leds[33 + ledPos] = CRGB::Yellow;
    }

    FastLED.show();

    if (forward) {
      ledPos++;
      if (ledPos >= 10) {
        forward = false;
      }
    } else {
      ledPos--;
      if (ledPos <= 0) {
        forward = true;
      }
    }
  }
}