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


#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);

byte mode = 0;

bool lastReading = HIGH;
bool stableState = HIGH;

unsigned long lastChangeTime = 0;
const unsigned long debounce = 80;


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

void setup() {

  pinMode(BUTTON_PIN, INPUT_PULLUP);

  strip.begin();
  strip.setBrightness(100);
  strip.clear();
  strip.show();
}


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

void loop() {

  checkButton();

  if (mode == 0) rainbow();
  if (mode == 1) colorWave();
  if (mode == 2) breathing();
  if (mode == 3) centerPulse();
  if (mode == 4) knightRider();
}


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

void checkButton() {

  bool reading = digitalRead(BUTTON_PIN);

  // Изменилось состояние кнопки
  if (reading != lastReading) {

    lastChangeTime = millis();
  }

  // Состояние стабильно
  if (millis() - lastChangeTime > debounce) {

    if (reading != stableState) {

      stableState = reading;

      // Нажатие
      if (stableState == LOW) {

        mode++;

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

        // Сразу очищаем старый режим
        strip.clear();
        strip.show();

        // Выводим новый режим сразу
        showNewMode();
      }
    }
  }

  lastReading = reading;
}


// =====================================================
// ПОКАЗ НОВОГО РЕЖИМА
// =====================================================

void showNewMode() {

  strip.clear();

  if (mode == 0) {

    for (int i = 0; i < LED_COUNT; i++) {
      strip.setPixelColor(i, strip.Color(255, 0, 0));
    }

  }

  if (mode == 1) {

    for (int i = 0; i < LED_COUNT; i++) {
      strip.setPixelColor(i, strip.Color(0, 255, 0));
    }

  }

  if (mode == 2) {

    for (int i = 0; i < LED_COUNT; i++) {
      strip.setPixelColor(i, strip.Color(0, 0, 255));
    }

  }

  if (mode == 3) {

    for (int i = 0; i < LED_COUNT; i++) {
      strip.setPixelColor(i, strip.Color(255, 0, 255));
    }

  }

  if (mode == 4) {

    for (int i = 0; i < LED_COUNT; i++) {
      strip.setPixelColor(i, strip.Color(255, 100, 0));
    }
  }

  strip.show();
  delay(150);
}


// =====================================================
// MODE 0 — RAINBOW
// =====================================================

void rainbow() {

  static uint16_t hue = 0;

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

    strip.setPixelColor(
      i,
      strip.gamma32(
        strip.ColorHSV(
          hue + i * 10000
        )
      )
    );
  }

  strip.show();

  hue += 300;

  delay(20);
}


// =====================================================
// MODE 1 — COLOR WAVE
// =====================================================

void colorWave() {

  static uint16_t hue = 0;

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

    strip.setPixelColor(
      i,
      strip.gamma32(
        strip.ColorHSV(
          hue + i * 9000
        )
      )
    );
  }

  strip.show();

  hue += 700;

  delay(30);
}


// =====================================================
// MODE 2 — BREATHING
// =====================================================

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 += 8000;
  }

  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 3 — CENTER PULSE
// =====================================================

void centerPulse() {

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

  strip.clear();

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

  if (step == 0) {

    // LED 4 — центр
    strip.setPixelColor(3, color);
  }

  if (step == 1) {

    // LED 1 + LED 2
    strip.setPixelColor(0, color);
    strip.setPixelColor(1, color);
  }

  if (step == 2) {

    // LED 3 + LED 5
    strip.setPixelColor(2, color);
    strip.setPixelColor(4, color);
  }

  if (step == 3) {

    // LED 6
    strip.setPixelColor(5, color);
  }

  strip.show();

  step++;

  if (step > 3) {

    step = 0;
    hue += 5000;
  }

  delay(150);
}


// =====================================================
// MODE 4 — KNIGHT RIDER
// =====================================================

void knightRider() {

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

  static uint16_t hue = 0;

  strip.clear();

  strip.setPixelColor(
    position,
    strip.gamma32(
      strip.ColorHSV(hue)
    )
  );

  // Хвост
  int tail1 = position - direction;

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

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

  int tail2 = position - direction * 2;

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

    strip.setPixelColor(
      tail2,
      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);
}