Arduino红外遥控按键控制彩虹灯程序
时间: 2023-08-13 14:20:34 浏览: 60
好的,以下是一个简单的Arduino红外遥控按键控制彩虹灯的程序:
```
#include <IRremote.h> // 引入红外遥控库
#include <Adafruit_NeoPixel.h> // 引入RGB LED灯带库
#define IR_PIN 2 // 红外接收器模块连接的引脚
#define LED_PIN 6 // RGB LED灯带连接的引脚
#define LED_COUNT 30 // RGB LED灯带的灯珠数量
IRrecv irrecv(IR_PIN);
decode_results results;
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
irrecv.enableIRIn(); // 启动红外接收器
strip.begin(); // 启动RGB LED灯带
strip.show(); // 显示灯光
}
void loop() {
if (irrecv.decode(&results)) { // 检测到遥控器信号
switch (results.value) { // 根据遥控器按键的值来判断执行哪种灯光效果
case 0xFF30CF: // 红色
colorWipe(strip.Color(255, 0, 0), 50);
break;
case 0xFF18E7: // 绿色
colorWipe(strip.Color(0, 255, 0), 50);
break;
case 0xFF7A85: // 蓝色
colorWipe(strip.Color(0, 0, 255), 50);
break;
case 0xFF10EF: // 闪烁
theaterChaseRainbow(50);
break;
case 0xFF38C7: // 呼吸灯
rainbowCycle(20);
break;
default:
break;
}
irrecv.resume(); // 重置红外接收器
}
}
void colorWipe(uint32_t color, int wait) {
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, color);
strip.show();
delay(wait);
}
}
void theaterChaseRainbow(int wait) {
for (int j = 0; j < 256; j++) {
for (int q = 0; q < 3; q++) {
for (int i = 0; i < strip.numPixels(); i += 3) {
strip.setPixelColor(i + q, Wheel((i + j) % 255));
}
strip.show();
delay(wait);
for (int i = 0; i < strip.numPixels(); i += 3) {
strip.setPixelColor(i + q, 0);
}
}
}
}
void rainbowCycle(int wait) {
uint16_t i, j;
for (j = 0; j < 256 * 5; j++) {
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + 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);
}
if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
```
这个程序使用了红外遥控库和RGB LED灯带库,其中`colorWipe()`函数用于实现颜色填充灯光效果,`theaterChaseRainbow()`函数用于实现彩虹闪烁灯光效果,`rainbowCycle()`函数用于实现彩虹呼吸灯灯光效果,`Wheel()`函数用于实现彩虹色彩的变化。当检测到遥控器信号时,程序会根据遥控器按键的值来判断执行哪种灯光效果。
希望这个程序能够帮助到您!