“红外调光”的版本间的差异
(→软件调试) |
(→软件调试) |
||
(未显示同一用户的4个中间版本) | |||
第43行: | 第43行: | ||
#define RECV_PIN 10 | #define RECV_PIN 10 | ||
− | #define INCREASE | + | //遥控器按键编码定义 |
− | #define DECREASE | + | #define INCREASE 0XFF02FD //增加+ |
− | #define NUM_1 0xFF30CF | + | #define DECREASE 0xFF9867 //减少- |
− | #define NUM_2 0xFF18E7 | + | #define NUM_0 0xFF6897 //数字0 |
− | #define NUM_3 0xFF7A85 | + | #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( | + | Adafruit_NeoPixel strip = Adafruit_NeoPixel(6, PIN, NEO_GRB + NEO_KHZ800); |
IRrecv irrecv(RECV_PIN); | IRrecv irrecv(RECV_PIN); | ||
decode_results results; | 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() { | void setup() { | ||
+ | // put your setup code here, to run once: | ||
strip.begin(); | strip.begin(); | ||
strip.show(); // Initialize all pixels to 'off' | strip.show(); // Initialize all pixels to 'off' | ||
irrecv.enableIRIn(); // Start the receiver | irrecv.enableIRIn(); // Start the receiver | ||
+ | for (int i = 0; i < 9; i++) | ||
+ | { | ||
+ | colorSetall(color_n[i]); | ||
+ | delay(300); | ||
+ | } | ||
} | } | ||
void loop() { | void loop() { | ||
− | // | + | // put your main code here, to run repeatedly: |
− | if(irrecv.decode(&results)) | + | 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 | + | void colorSetall(uint32_t c) { |
− | for(uint16_t i=0; i<strip.numPixels(); i++) { | + | for (uint16_t i = 0; i < strip.numPixels(); i++) { |
− | + | strip.setPixelColor(i, c); | |
− | |||
− | |||
} | } | ||
+ | strip.show(); | ||
} | } | ||
</source> | </source> | ||
第122行: | 第183行: | ||
*结果 | *结果 | ||
**通过“+”控制亮度增强,“-”控制亮度减弱。 | **通过“+”控制亮度增强,“-”控制亮度减弱。 | ||
− | **数字1- | + | **数字1-9切换不同的颜色。 |
+ | *打开串口监视器,可以看到接收到的红外信号。 | ||
+ | [[file:ir_rec.JPG|600px|center]] | ||
==软件调试== | ==软件调试== | ||
第130行: | 第193行: | ||
#define RECV_PIN 10 //红外接收引脚 | #define RECV_PIN 10 //红外接收引脚 | ||
</source> | </source> | ||
− | * | + | *红外遥控编码定义,此处采用NEC编码格式 |
<source lang="cpp"> | <source lang="cpp"> | ||
− | #define INCREASE 0xFFA857 | + | //遥控器按键编码定义 |
− | #define DECREASE 0xFFE01F | + | #define INCREASE 0xFFA857 //增加+ |
− | #define NUM_1 0xFF30CF | + | #define DECREASE 0xFFE01F //减少- |
− | #define NUM_2 0xFF18E7 | + | #define NUM_0 0xFF6897 //数字0 |
− | #define NUM_3 0xFF7A85 | + | #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 | ||
</source> | </source> | ||
− | *红外遥控器的编码格式, | + | *红外遥控器的编码格式,数据十六进制的表示方式:OXFFxxxx,xxxx为图中的四个字。例如:电源的编码为:0XFFA25D。 |
[[File:ir.jpg|600px|center]] | [[File:ir.jpg|600px|center]] | ||
*接收红外遥控器信号,判断按键状态,控制LED的发光颜色和发光亮度 | *接收红外遥控器信号,判断按键状态,控制LED的发光颜色和发光亮度 | ||
+ | **通过color的大小来控制彩灯亮度,通过pos来控制彩灯颜色。使用了二位数组来实现选择。通过改变color_m[9][3]二维数组里面的颜色值,就可以改变每个按键对应的颜色。 | ||
<source lang="cpp"> | <source lang="cpp"> | ||
− | + | 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]))); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</source> | </source> | ||
2015年12月14日 (一) 12:19的最新版本
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_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
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])));
结果红外遥控按下不同的按键,可以改变LED的颜色和亮度。 视频 |