Загрузка данных


#include <Adafruit_NeoPixel.h>

#define LED_PIN     2
#define LED_COUNT   6
#define BUTTON_PIN  4

Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

// =====================================================
// НАСТРОЙКИ
// =====================================================

#define BRIGHTNESS 100

byte mode = 0;

bool lastButton = HIGH;
bool buttonState = HIGH;

unsigned long lastDebounce = 0;
const unsigned long debounceTime = 50;


// =====================================================
// ФИЗИЧЕСКОЕ РАСПОЛОЖЕНИЕ
// =====================================================
//
//       1     2
//
//     3   4   5
//
//         6
//
// Индексы программы:
// LED1 = 0
// LED2 = 1
// LED3 = 2
// LED4 = 3
// LED5 = 4
// LED6 = 5
//
// =====================================================


// =====================================================
// SETUP
// =====================================================

void setup() {

  strip.begin();
  strip.setBrightness(BRIGHTNESS);
  strip.clear();
  strip.show();

  pinMode(BUTTON_PIN, INPUT_PULLUP);
}


// =====================================================
// LOOP
// =====================================================

void loop() {

  checkButton();

  switch (mode) {

    case 0:
      rainbow();
      break;

    case 1:
      colorWave();
      break;

    case 2:
      breathing();
      break;

    case 3:
      centerPulse();
      break;

    case 4:
      knightRider();
      break;
  }
}


// =====================================================
// КНОПКА EV
// =====================================================

void checkButton() {

  bool reading = digitalRead(BUTTON_PIN);

  if (reading != lastButton) {
    lastDebounce = millis();
  }

  if ((millis() - lastDebounce) > debounceTime) {

    if (reading != buttonState) {

      buttonState = reading;

      if (buttonState == LOW) {

        mode++;

        if (mode >= 5) {
          mode = 0;
        }

        strip.clear();
        strip.show();
      }
    }
  }

  lastButton = reading;
}


// =====================================================
// MODE 1
// ПЛАВНАЯ РАДУГА
// =====================================================

void rainbow() {

  static uint16_t hue = 0;

  for (int i = 0; i < LED_COUNT; i++) {

    uint16_t pixelHue =
      hue + (i * 65536L / LED_COUNT);

    strip.setPixelColor(
      i,
      strip.gamma32(strip.ColorHSV(pixelHue))
    );
  }

  strip.show();

  hue += 300;

  delay(15);
}


// =====================================================
// MODE 2
// ЦВЕТНАЯ ВОЛНА
// =====================================================

void colorWave() {

  static uint16_t hue = 0;

  for (int i = 0; i < LED_COUNT; i++) {

    uint16_t pixelHue =
      hue + (i * 9000);

    strip.setPixelColor(
      i,
      strip.gamma32(strip.ColorHSV(pixelHue))
    );
  }

  strip.show();

  hue += 500;

  delay(25);
}


// =====================================================
// MODE 3
// ДЫХАНИЕ
// =====================================================

void breathing() {

  static int brightness = 0;
  static int direction = 1;

  static uint16_t hue = 0;

  brightness += direction * 2;

  if (brightness >= 100) {

    brightness = 100;
    direction = -1;
  }

  if (brightness <= 0) {

    brightness = 0;
    direction = 1;

    hue += 7000;
  }

  strip.setBrightness(brightness);

  uint32_t color =
    strip.gamma32(strip.ColorHSV(hue));

  for (int i = 0; i < LED_COUNT; i++) {

    strip.setPixelColor(i, color);
  }

  strip.show();

  delay(20);
}


// =====================================================
// MODE 4
// ЦЕНТР → КРАЯ
// =====================================================

void centerPulse() {

  static int step = 0;
  static uint16_t hue = 0;

  strip.clear();

  uint32_t color =
    strip.gamma32(strip.ColorHSV(hue));

  // Центр:
  //
  //       1  2
  //     3  4  5
  //       6
  //
  // Сначала центральные LED
  // потом верхний/средний ряд
  // потом нижний

  if (step == 0) {

    // Центр второго ряда
    strip.setPixelColor(3, color);
  }

  else if (step == 1) {

    // Верх
    strip.setPixelColor(0, color);
    strip.setPixelColor(1, color);
  }

  else if (step == 2) {

    // Боковые второго ряда
    strip.setPixelColor(2, color);
    strip.setPixelColor(4, color);
  }

  else if (step == 3) {

    // Нижний
    strip.setPixelColor(5, color);
  }

  strip.show();

  step++;

  if (step > 3) {

    step = 0;
    hue += 3500;
  }

  delay(160);
}


// =====================================================
// MODE 5
// БЕГУЩИЙ ОГОНЁК
// =====================================================

void knightRider() {

  static int position = 0;
  static int direction = 1;

  static uint16_t hue = 0;

  strip.clear();

  // Основной LED
  strip.setPixelColor(
    position,
    strip.gamma32(strip.ColorHSV(hue))
  );

  // След
  int previous = position - direction;

  if (previous >= 0 && previous < LED_COUNT) {

    strip.setPixelColor(
      previous,
      strip.ColorHSV(hue, 255, 60)
    );
  }

  int previous2 = position - direction * 2;

  if (previous2 >= 0 && previous2 < LED_COUNT) {

    strip.setPixelColor(
      previous2,
      strip.ColorHSV(hue, 255, 20)
    );
  }

  strip.show();

  position += direction;

  if (position >= LED_COUNT - 1) {

    position = LED_COUNT - 1;
    direction = -1;

    hue += 6000;
  }

  if (position <= 0) {

    position = 0;
    direction = 1;

    hue += 6000;
  }

  delay(80);
}