LED Lichtspiel (ESP8266 + USB 5V) – Hộp đèn WS2812B chuyển màu
Mẫu hộp đèn LED “Lichtspiel” dùng 2 WS2812/WS2812B điều khiển bằng ESP8266 (D4/GPIO2) cấp nguồn USB 5V. Hiệu ứng chọn màu ngẫu nhiên rồi fade lần lượt giữa 2 LED theo chu kỳ 10 giây.
Mô tả
Do cái vòng cổ chó có đèn LED mua sẵn bị dài quá nên tụi mình cắt ngắn lại... Mà cái thanh (sợi thủy tinh?) dẻo đó bỏ thì uổng...
Vậy nên mình làm một cái hộp đơn giản và dán ở đáy 2 LED màu WS2812 / WS2812B. Mình điều khiển chúng bằng một con ESP8266 (PIN D4), con này cũng vừa khít trong cái hộp...
Kết quả là chương trình sẽ chọn ngẫu nhiên một màu rồi “fade” chậm trên LED thứ nhất. Vài giây sau LED thứ hai mới chuyển theo. Sau 10 giây thì toàn bộ quá trình bắt đầu lại từ đầu... Nhẹ nhàng, nhìn cũng dễ thương — ai thích kiểu này thì hợp... :-)
Lưu ý: dây USB nên luồn vào hộp TRƯỚC khi hàn...
Visual Studio Code: chương trình C++:
; PlatformIO Project Configuration File ; ; Build options: build flags, source filter ; Upload options: custom upload port, speed and extra flags ; Library options: dependencies, extra library storages ; Advanced options: extra scripting ; ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html
[env:nodemcuv2] platform = espressif8266 board = nodemcuv2 framework = arduino
lib_deps = fastled/FastLED @ ^3.6.0
build_flags = -Wno-unused-variable
include
define LED_PIN 2 // D4 trên ESP8266 là GPIO2
define NUM_LEDS 2
define BRIGHTNESS 150 // 0 tới 255
define LED_TYPE WS2812B
define COLOR_ORDER GRB
CRGB leds[NUM_LEDS]; CRGB targetColor; CRGB sourceColors[NUM_LEDS];
// Điều khiển thời gian unsigned long lastCycleStart = 0; const unsigned long cycleInterval = 10000; // Chu kỳ tổng 10 giây const unsigned long fadeDuration = 3000; // Thời gian fade 3 giây
void setup() { // Trễ an toàn ngắn delay(1000); FastLED.addLeds(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); FastLED.setBrightness(BRIGHTNESS);
// Màu khởi đầu là đen fill_solid(leds, NUM_LEDS, CRGB::Black);FastLED.show();// Set timer ban đầu lastCycleStart = millis();targetColor = CHSV(random8(), 255, 255); // Màu ngẫu nhiên đầu tiên
}
void loop() { unsigned long currentMillis = millis(); unsigned long elapsed = currentMillis - lastCycleStart;
// --- PHA 1: LED 0 fade ---if (elapsed <= fadeDuration) { fract8 amount = map(elapsed, 0, fadeDuration, 0, 255); leds[0] = blend(sourceColors[0], targetColor, amount);} // --- PHA 2: LED 1 fade (bắt đầu khi LED 0 xong) ---else if (elapsed <= (fadeDuration* 2)) { leds[0] = targetColor; // Đảm bảo LED 0 ở màu cuối unsigned long fadeStartLED2 = fadeDuration; fract8 amount = map(elapsed, fadeStartLED2, fadeDuration* 2, 0, 255); leds[1] = blend(sourceColors[1], targetColor, amount);}// --- PHA 3: Chờ tới khi đủ 10 giây ---else if (elapsed >= cycleInterval) { // Reset chu kỳ: chọn màu ngẫu nhiên mới lastCycleStart = currentMillis; sourceColors[0] = leds[0]; sourceColors[1] = leds[1]; targetColor = CHSV(random8(), 255, 255); // Chọn hue ngẫu nhiên (HSV)}FastLED.show();
}
Danh mục: Household
Giấy phép
Tác phẩm này được cấp phép theo
Creative Commons — Attribution — Noncommercial — Share AlikeCC-BY-NC-SA
File mô hình
Chưa có bản in nào được khoe. Hãy là người đầu tiên!
Chưa có bình luận nào. Hãy là người đầu tiên!