#include <Adafruit_NeoPixel.h>
// Định nghĩa chân dữ liệu cho WS2812 và số lượng LED
#define PIN 0
#define NUM_LEDS 32
// Khởi tạo dải đèn LED WS2812
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin(); // Khởi động strip
strip.show(); // Xóa trạng thái của các đèn LED
}
// Vòng lặp chính
void loop() {
waveEffect(); // Hiệu ứng rợn sóng nhiều màu
chaseEffect(); // Hiệu ứng nháy đuổi
alternateEffect(); // Hiệu ứng đan xen tới lui
}
// Hiệu ứng 1: Rợn sóng nhiều màu
void waveEffect() {
for (int i = 0; i < 256; i++) {
for (int j = 0; j < strip.numPixels(); j++) {
int color = Wheel(((j * 256 / strip.numPixels()) + i) & 255);
strip.setPixelColor(j, color); // Thiết lập màu từng LED
}
strip.show();
delay(10); // Tốc độ của hiệu ứng
}
}
// Hiệu ứng 2: Nháy đuổi
void chaseEffect() {
for (int i = 0; i < strip.numPixels(); i++) {
// Xóa các LED
strip.setPixelColor(i, strip.Color(255, 0, 0)); // Màu đỏ cho LED hiện tại
strip.setPixelColor(i, strip.Color(255, 0, 255)); // Màu đỏ cho LED hiện tại
strip.show();
delay(100); // Tốc độ của hiệu ứng
}
}
// Hiệu ứng 3: Đan xen tới lui
void alternateEffect() {
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, (i % 2 == 0) ? strip.Color(0, 255, 0) : strip.Color(0, 0, 255)); // Đan xen màu xanh lá và xanh dương
}
strip.show();
delay(500);
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, (i % 2 == 0) ? strip.Color(0, 0, 255) : strip.Color(0, 255, 0)); // Đan xen màu xanh dương và xanh lá
}
strip.show();
delay(500);
}
// Hàm tạo màu xoay vòng
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
}