写一段Arduino代码,实现用红外遥控切换ws2812灯带的九种模式
时间: 2024-03-22 20:42:17 浏览: 173
好的,以下是一份实现用红外遥控切换ws2812灯带的九种模式的Arduino代码:
```
#include <IRremote.h>
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUM_LEDS 60
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
int receiver = 11;
IRrecv irrecv(receiver);
decode_results results;
void setup() {
Serial.begin(9600);
irrecv.enableIRIn();
strip.begin();
strip.show(); // initialize all pixels to off
}
void loop() {
if (irrecv.decode(&results)) {
switch (results.value) {
case 0xFFA25D: // button 1
rainbowCycle(20);
break;
case 0xFF629D: // button 2
theaterChaseRainbow(50);
break;
case 0xFFE21D: // button 3
colorWipe(strip.Color(255, 0, 0), 50);
break;
case 0xFF22DD: // button 4
colorWipe(strip.Color(0, 255, 0), 50);
break;
case 0xFF02FD: // button 5
colorWipe(strip.Color(0, 0, 255), 50);
break;
case 0xFFC23D: // button 6
theaterChase(strip.Color(255, 0, 0), 50);
break;
case 0xFF6897: // button 7
theaterChase(strip.Color(0, 255, 0), 50);
break;
case 0xFF9867: // button 8
theaterChase(strip.Color(0, 0, 255), 50);
break;
case 0xFFB04F: // button 9
colorWipe(strip.Color(0,0,0), 50);
break;
}
irrecv.resume(); // Receive the next value
}
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t color, int wait) {
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, color);
strip.show();
delay(wait);
}
}
// Slightly different, this one makes the rainbow cycle backwards
void reverseRainbowCycle(int wait) {
for (int j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
for (int i = strip.numPixels() - 1; i >= 0; i--) {
strip.setPixelColor(i, Wheel((i + j) & 255));
}
strip.show();
delay(wait);
}
}
// theater chase lights with rainbow effect
void theaterChaseRainbow(int wait) {
for (int j = 0; j < 256; j++) { // cycle all 256 colors in the wheel
for (int q = 0; q < 3; q++) {
for (int i = 0; i < strip.numPixels(); i = i + 3) {
strip.setPixelColor(i + q, Wheel((i + j) % 255)); // turn every third pixel on
}
strip.show();
delay(wait);
for (int i = 0; i < strip.numPixels(); i = i + 3) {
strip.setPixelColor(i + q, 0); // turn every third pixel off
}
}
}
}
// theater chase lights
void theaterChase(uint32_t color, int wait) {
for (int j = 0; j < 10; j++) { // do 10 cycles of chasing
for (int q = 0; q < 3; q++) {
for (int i = 0; i < strip.numPixels(); i = i + 3) {
strip.setPixelColor(i + q, color); // turn every third pixel on
}
strip.show();
delay(wait);
for (int i = 0; i < strip.numPixels(); i = i + 3) {
strip.setPixelColor(i + q, 0); // turn every third pixel off
}
}
}
}
// rainbow cycle
void rainbowCycle(int wait) {
for (int j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i + j) & 255));
}
strip.show();
delay(wait);
}
}
// Input a value 0 to 255 to get a color value.
// The colors are a transition r - g - b - back to r.
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);
}
}
```
这份代码使用了Adafruit的NeoPixel库来控制WS2812灯带,并使用IRremote库来接收红外遥控的输入。在主循环中,我们使用switch语句来根据接收到的红外代码值来选择不同的模式。其中,button 1到button 9 分别对应了彩虹循环、彩虹跑、红色扫描、绿色扫描、蓝色扫描、红色跑马灯、绿色跑马灯、蓝色跑马灯和关闭灯带这九种模式。每种模式都封装在自己的函数中,其中一些函数使用了WS2812灯带的基本功能,如颜色填充和清除,而其他函数则使用了一些更具创意的算法,如彩虹循环和跑马灯效果。
阅读全文