ws2812十种灯光代码
时间: 2023-07-02 11:13:56 浏览: 598
WS2812流水灯
4星 · 用户满意度95%
下面是给出十种常见的WS2812灯光效果的代码示例:
1. 颜色渐变:
```C++
void colorWipe(uint32_t color, int wait) {
for(int i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, color);
strip.show();
delay(wait);
}
}
```
2. 彩虹流动:
```C++
void rainbow(int wait) {
for(int j=0; j<256; j++) {
for(int i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}
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);
}
}
```
3. 呼吸灯:
```C++
void breathe(int wait) {
int max_bright = 255;
int min_bright = 0;
for(int i=min_bright; i<max_bright; i++) {
for(int j=0; j<strip.numPixels(); j++) {
strip.setPixelColor(j, strip.Color(i, i, i));
}
strip.show();
delay(wait);
}
for(int i=max_bright; i>min_bright; i--) {
for(int j=0; j<strip.numPixels(); j++) {
strip.setPixelColor(j, strip.Color(i, i, i));
}
strip.show();
delay(wait);
}
}
```
4. 闪烁:
```C++
void blink(uint32_t color, int wait, int blink_times) {
for(int i=0; i<blink_times; i++) {
for(int j=0; j<strip.numPixels(); j++) {
strip.setPixelColor(j, color);
}
strip.show();
delay(wait);
for(int j=0; j<strip.numPixels(); j++) {
strip.setPixelColor(j, strip.Color(0, 0, 0));
}
strip.show();
delay(wait);
}
}
```
5. 像素化:
```C++
void pixelate(uint32_t color, int wait) {
int pixel_size = 5;
for(int i=0; i<strip.numPixels(); i+=pixel_size) {
for(int j=i; j<i+pixel_size; j++) {
strip.setPixelColor(j, color);
}
strip.show();
delay(wait);
for(int j=i; j<i+pixel_size; j++) {
strip.setPixelColor(j, strip.Color(0, 0, 0));
}
strip.show();
}
}
```
6. 火焰效果:
```C++
void flame(int wait) {
for(int i=0; i<strip.numPixels(); i++) {
int flicker = random(150);
strip.setPixelColor(i, strip.Color(flicker+100, 0, 0));
}
strip.show();
delay(wait);
}
```
7. 音乐节奏:
```C++
void music(int wait) {
int bass = analogRead(A0);
int treble = analogRead(A1);
int mid = analogRead(A2);
int total = (bass+treble+mid)/3;
for(int i=0; i<strip.numPixels(); i++) {
int brightness = map(total, 0, 1023, 0, 255);
strip.setPixelColor(i, strip.Color(brightness, 0, 0));
}
strip.show();
delay(wait);
}
```
8. 星空效果:
```C++
void stars(int wait) {
for(int i=0; i<strip.numPixels(); i++) {
int flicker = random(150);
strip.setPixelColor(i, strip.Color(flicker, flicker, flicker));
}
strip.show();
delay(wait);
}
```
9. 渐变循环:
```C++
void gradientCycle(int wait) {
int r, g, b;
for(int k=0; k<256; k++) {
for(int j=0; j<strip.numPixels(); j++) {
r = sin(0.3*j+k/10.0) * 127 + 128;
g = sin(0.3*j+k/10.0) * 127 + 128;
b = sin(0.3*j+k/10.0) * 127 + 128;
strip.setPixelColor(j, strip.Color(r, g, b));
}
strip.show();
delay(wait);
}
}
```
10. 彩带效果:
```C++
void ribbon(int wait) {
for(int i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(255, 0, 0));
}
strip.show();
delay(wait);
for(int i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(0, 255, 0));
}
strip.show();
delay(wait);
for(int i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(0, 0, 255));
}
strip.show();
delay(wait);
}
```
注意:这些代码只是示例,实际使用时需要根据具体情况进行修改。
阅读全文