#include <FastLED.h>
// Настройки
#define LED_PIN 6
#define LED_NUM 41
// Массив светодиодов
CRGB leds[LED_NUM];
// Глобальные переменные для управления радужными сегментами
uint8_t firstHue = 0;
unsigned long firstLastUpdate = 0;
uint8_t secondHue = 0; // Начальный оттенок для второго сегмента (смещён на 128)
unsigned long secondLastUpdate = 0;
// Функция обновления радужного сегмента (с параметром speed)
void updateRainbowSegment(int startIndex, int ledCount, int speed,
uint8_t& currentHue, unsigned long delayMs,
unsigned long& lastUpdate) {
unsigned long currentTime = millis();
if (currentTime - lastUpdate >= delayMs) {
lastUpdate = currentTime;
const float hueStep = 256.0f / ledCount;
for (int i = 0; i < ledCount; i++) {
int ledIndex = startIndex + i;
if (ledIndex >= LED_NUM) break;
uint8_t ledHue = (uint8_t)((currentHue + i * hueStep));
leds[ledIndex] = CHSV(ledHue, 255, 255);
}
currentHue = (currentHue + speed);
}
}
// Функция обновления радужного сегмента (с параметром speed)
void updateRainbowSegmentEnd(int startIndex, int ledCount, int speed,
uint8_t& currentHue, unsigned long delayMs,
unsigned long& lastUpdate) {
unsigned long currentTime = millis();
if (currentTime - lastUpdate >= delayMs) {
lastUpdate = currentTime;
const float hueStep = 256.0f / ledCount;
for (int i = 0; i < ledCount; i++) {
int ledIndex = startIndex +ledCount - 1 - i;
if (ledIndex >= LED_NUM) break;
uint8_t ledHue = (uint8_t)((currentHue + i * hueStep));
leds[ledIndex] = CHSV(ledHue, 255, 255);
}
currentHue = (currentHue + speed);
}
}
void setup() {
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, LED_NUM);
FastLED.setBrightness(255); // Устанавливаем яркость
}
void loop() {
// Сегмент 1: светодиоды 0–19
// Скорость 3, обновление каждые 40 мс
updateRainbowSegment(0, 20, 6, firstHue, 40, firstLastUpdate);
// Сегмент 2: светодиоды 20–39
// Скорость 5, обновление каждые 60 мс (движется быстрее и с другой задержкой)
updateRainbowSegmentEnd(21, 20, 6, secondHue, 40, secondLastUpdate);
FastLED.show(); // Обновляем ленту
delay(10); // Небольшая задержка для стабильности
}