红外调光
Language | English |
---|
目的通过红外遥控器控制RGBLED的颜色和亮度。 原理利用红外接收传感器接收遥控器的红外信号,通过判断不同的红外编码来控制LED的发光颜色和发光亮度。 设备
准备
调试
#include <IRremote.h>
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define RECV_PIN 10
//遥控器按键编码定义
#define INCREASE 0XFF02FD //增加+
#define DECREASE 0xFF9867 //减少-
#define NUM_0 0xFF6897 //数字0
#define NUM_1 0xFF30CF //数字1
#define NUM_2 0xFF18E7 //数字2
#define NUM_3 0xFF7A85 //数字3
#define NUM_4 0xFF10EF //数字4
#define NUM_5 0xFF38C7 //数字5
#define NUM_6 0xFF5AA5 //数字6
#define NUM_7 0xFF42BD //数字7
#define NUM_8 0xFF4AB5 //数字8
#define NUM_9 0xFF52AD //数字9
Adafruit_NeoPixel strip = Adafruit_NeoPixel(6, PIN, NEO_GRB + NEO_KHZ800);
IRrecv irrecv(RECV_PIN);
decode_results results;
#define val_max 255
#define val_min 0
int pos = 8, color = 100;
uint32_t color_n[9] =
{
strip.Color(255, 0, 0), strip.Color(248, 141, 30), strip.Color(255, 255, 0), strip.Color(0, 255, 0), strip.Color(0, 127, 255), strip.Color(0, 0, 255), strip.Color(139, 0, 255), strip.Color(255, 255, 255), strip.Color(0, 0, 0)
};
uint32_t color_m[9][3] =
{
{0, 255, 255},
{255, 0, 0},
{248, 141, 30},
{255, 255, 0},
{0, 255, 0},
{0, 127, 255},
{0, 0, 255},
{139, 0, 255},
{255, 255, 255}
};
void setup() {
// put your setup code here, to run once:
strip.begin();
strip.show(); // Initialize all pixels to 'off'
irrecv.enableIRIn(); // Start the receiver
for (int i = 0; i < 9; i++)
{
colorSetall(color_n[i]);
delay(300);
}
}
void loop() {
// put your main code here, to run repeatedly:
if (irrecv.decode(&results))
{
Serial.println(results.value, HEX);
switch (results.value)
{
case INCREASE:
color += 50;
if (color > 250)
color = 250;
break;
case DECREASE:
color -= 50;
if (color < 0)
color = 0;
break;
case NUM_1:
color = 100;
pos = 0;
break;
case NUM_2:
pos = 1;
color = 100;
break;
case NUM_3:
pos = 2;
color = 100;
break;
case NUM_4:
pos = 3;
color = 100;
break;
case NUM_5:
pos = 4;
color = 100;
break;
case NUM_6:
pos = 5;
color = 100;
break;
case NUM_7:
pos = 6;
color = 100;
break;
case NUM_8:
pos = 7;
color = 100;
break;
case NUM_9:
pos = 8;
color = 100;
break;
default:
break;
}
irrecv.resume(); // Receive the next value
colorSetall(strip.Color(map(color, val_min, val_max, 0, color_m[pos][0]),
map(color, val_min, val_max, 0, color_m[pos][1]),
map(color, val_min, val_max, 0, color_m[pos][2])));
}
}
void colorSetall(uint32_t c) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
}
strip.show();
}
软件调试
#define PIN 6 //LED灯控制引脚
#define RECV_PIN 10 //红外接收引脚
#define INCREASE 0xFFA857 //增加+
#define DECREASE 0xFFE01F //减少-
#define NUM_1 0xFF30CF //数字1
#define NUM_2 0xFF18E7 //数字2
#define NUM_3 0xFF7A85 //数字3
if(irrecv.decode(&results)) //接收红外编码
{
switch(results.value) //判断按键
{
case INCREASE: //亮度增强
rate += 10;
if(rate > 255)
rate = 255;
break;
case DECREASE: //亮度减弱
rate -= 10;
if(rate < 0)
rate = 0;
break;
case NUM_1: //按键1,显示红色
red = 1;
green = 0;
blue = 0;
break;
case NUM_2: //按键2,显示绿色
red = 0;
green = 1;
blue = 0;
break;
case NUM_3: //按键3,显示蓝色
red = 0;
green = 0;
blue = 1;
break;
default:
break;
}
irrecv.resume(); //接收下一个信号
strip.setPixelColor(0, strip.Color(red*rate, green*rate, blue*rate));
strip.show();
}
结果红外遥控按下不同的按键,可以改变LED的颜色和亮度。 视频 |