#include <FastLED.h>
#define LED_PIN 25 // Пин управления светодиодами (GPIO 25)
#define BUTTON_PIN 4 // Пин кнопки (на GND)
#define NUM_LEDS 10 // Всего 10 светодиодов
#define BRIGHTNESS 120 // Яркость
CRGB leds[NUM_LEDS];
CRGB realLeds[NUM_LEDS];
// --- ЧЕТКИЕ И НОРМАЛЬНЫЕ ЦВЕТА ---
const CRGB pureColors[] = {
CRGB::Red, // 0. Красный
CRGB::Green, // 1. Зеленый
CRGB::Blue, // 2. Синий
CRGB::White, // 3. Белый
CRGB::Yellow, // 4. Желтый
CRGB::Cyan, // 5. Голубой
CRGB::Magenta, // 6. Розовый
CRGB::Orange, // 7. Оранжевый
CRGB::Purple // 8. Фиолетовый
};
const int totalColors = 9;
int currentColorIndex = 0;
int currentMode = 0; // 0 - авто-смена каждую секунду, 1 - статика кнопкой
const int totalModes = 2;
unsigned long lastColorChangeTime = 0;
const unsigned long colorInterval = 1000; // 1 секунда
unsigned long buttonPressStartTime = 0;
bool buttonIsPressed = false;
bool longPressHandled = false;
const unsigned long longPressTime = 500;
void setup() {
Serial.begin(115200);
Serial.println("System started!"); // Теперь используется Serial, никаких cout!
FastLED.addLeds<WS2812B, LED_PIN, GRB>(realLeds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
handleButton();
if (currentMode == 0) {
if (millis() - lastColorChangeTime >= colorInterval) {
lastColorChangeTime = millis();
currentColorIndex = (currentColorIndex + 1) % totalColors;
}
fill_solid(leds, NUM_LEDS, pureColors[currentColorIndex]);
}
else if (currentMode == 1) {
fill_solid(leds, NUM_LEDS, pureColors[currentColorIndex]);
}
// Перенос в физический порядок под вашу плату
for (int i = 0; i < NUM_LEDS; i++) {
realLeds[getPhysicalIndex(i)] = leds[i];
}
FastLED.show();
FastLED.delay(20);
}
int getPhysicalIndex(int logicalIndex) {
const int mapTable[10] = {3, 2, 1, 0, 4, 5, 6, 7, 8, 9};
return mapTable[logicalIndex];
}
void handleButton() {
bool reading = (digitalRead(BUTTON_PIN) == LOW);
if (reading && !buttonIsPressed) {
buttonIsPressed = true;
buttonPressStartTime = millis();
longPressHandled = false;
}
else if (reading && buttonIsPressed) {
if (!longPressHandled && (millis() - buttonPressStartTime >= longPressTime)) {
currentMode = (currentMode + 1) % totalModes;
longPressHandled = true;
Serial.print("Mode changed to: ");
Serial.println(currentMode);
}
}
else if (!reading && buttonIsPressed) {
buttonIsPressed = false;
if (!longPressHandled) {
currentColorIndex = (currentColorIndex + 1) % totalColors;
Serial.print("Color changed to index: ");
Serial.println(currentColorIndex);
}
}
}