“红外调光”的版本间的差异
(Created page with "{{Language| 红外调光}} {| style="width: 800px;" |- | ==目的== 通过红外遥控器控制RGBLED的颜色和亮度。 ==原理== 利用红外接收传感器接收遥控...") |
(→软件调试) |
||
(未显示同一用户的9个中间版本) | |||
第14行: | 第14行: | ||
|模块||数量||功能 | |模块||数量||功能 | ||
|- | |- | ||
− | |[[ | + | |[[mCookie-CoreUSB/zh]]||1||核心板 |
|- | |- | ||
− | |[[ | + | |[[mCookie-Hub/zh]]||1||传感器转接板 |
|- | |- | ||
− | | [[Microduino-IR receiver/zh]]||1||红外接收传感器 | + | |[[Microduino-IR receiver/zh]]||1||红外接收传感器 |
|- | |- | ||
− | | [[Microduino- | + | |[[Microduino-Color led/zh]]||1||彩色LED灯 |
|} | |} | ||
− | + | *其他设备 | |
+ | **红外遥控器 | ||
+ | **USB数据连接线 | ||
+ | **传感器连接线*2 | ||
+ | [[File:IR_Lantern.jpg|600px|center]] | ||
+ | ==准备== | ||
+ | *Setup 1:将彩灯接到Hub的D6引脚。将红外接收传感器接到Hub的D10引脚上。 | ||
+ | [[file:mCookie-color led-IR-hub.JPG|600px|center]] | ||
+ | *Setup 2:将所有设备连接在一起。通过USB数据线将接入电脑。 | ||
+ | [[file:mCookie-color led-IR-pc.JPG|600px|center]] | ||
− | == | + | ==调试== |
− | * | + | *打开Arduino IDE,将下列代码复制到IDE中。 |
− | + | <source lang="cpp"> | |
− | + | #include <IRremote.h> | |
− | + | #include <Adafruit_NeoPixel.h> | |
− | |||
− | |||
− | + | #define PIN 6 | |
− | + | #define RECV_PIN 10 | |
− | |||
− | #define PIN 6 | ||
− | #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 | + | 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(); | ||
+ | } | ||
</source> | </source> | ||
− | [[ | + | *选择正确的板卡和COM端口 |
+ | [[file:upload.JPG|600px|center]] | ||
+ | *编译 | ||
+ | **编译时会提示保存项目,用户可以自己命名保存到你的文件夹即可。 | ||
+ | *下载 | ||
+ | **编译成功后可以直接下载,提示下载成功表示程序下载完毕。 | ||
+ | [[file:upload-ir-color led.JPG|600px|center]] | ||
+ | *结果 | ||
+ | **通过“+”控制亮度增强,“-”控制亮度减弱。 | ||
+ | **数字1-9切换不同的颜色。 | ||
+ | *打开串口监视器,可以看到接收到的红外信号。 | ||
+ | [[file:ir_rec.JPG|600px|center]] | ||
+ | |||
+ | ==软件调试== | ||
+ | *定义了led和红外接收的引脚。用户可以改其他引脚。 | ||
+ | <source lang="cpp"> | ||
+ | #define PIN 6 //LED灯控制引脚 | ||
+ | #define RECV_PIN 10 //红外接收引脚 | ||
+ | </source> | ||
+ | *红外遥控编码定义,此处采用NEC编码格式 | ||
+ | <source lang="cpp"> | ||
+ | //遥控器按键编码定义 | ||
+ | #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 | ||
+ | </source> | ||
+ | *红外遥控器的编码格式,数据十六进制的表示方式:OXFFxxxx,xxxx为图中的四个字。例如:电源的编码为:0XFFA25D。 | ||
+ | [[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的颜色和亮度。 视频 |