红外调光
Language | English |
---|
目的通过红外遥控器控制RGBLED的颜色和亮度。 原理利用红外接收传感器接收遥控器的红外信号,通过判断不同的红外编码来控制LED的发光颜色和发光亮度。 设备
准备
调试
#include <IRremote.h>
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define RECV_PIN 10
#define INCREASE 0xFFA857
#define DECREASE 0xFFE01F
#define NUM_1 0xFF30CF
#define NUM_2 0xFF18E7
#define NUM_3 0xFF7A85
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
IRrecv irrecv(RECV_PIN);
decode_results results;
boolean red = 0;
boolean green = 0;
boolean blue = 0;
int rate = 0;
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
// Some example procedures showing how to display to the pixels:
if(irrecv.decode(&results))
{
Serial.println(results.value,HEX);
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:
red = 1;
green = 0;
blue = 0;
break;
case NUM_2:
red = 0;
green = 1;
blue = 0;
break;
case NUM_3:
red = 0;
green = 0;
blue = 1;
break;
default:
break;
}
irrecv.resume(); // Receive the next value
colorWipe(strip.Color(red*rate, green*rate, blue*rate), 50);
}
}
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
软件调试
#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的颜色和亮度。 视频 |