红外线 遥控 空调 测试成功参考了群里的很多文章以及网络上关于红外线的文章,也买了帖子里的那个逻辑调试仪
35块非常非常实惠,如果大家有兴趣在这方面有深入研究的这个是必须的
http://www.geek-workshop.com/forum.php?mod=viewthread&tid=5095&page=1&extra=#pid37445
http://www.geek-workshop.com/forum.php?mod=viewthread&tid=3444
当然,小白如果没兴趣弄那么高深的东西接下去我会教你们如何用arduino iremote现有的库完成
对家里的空调 进行控制
关于红外遥控器的文章很多,我不多叙述了,这里要说的就是空调的遥控器的发射和普通电视不太一样
码会非常长,所以iremote库内的函数无法支持(不是说写的有什么核心问题就是数据位的问题)
其实懂了之后很简单,但是这个问题我也研究了好几天,昨天用逻辑仪分析出结果控制成功,但是这个不适合小白
所以我今天进一步改进了原有的iremote 库的一些参数设置,让小白可以顺利的控制自己家的空调及其他非标设备
第一步 源代码的修改
1 你要找到如下三个文件 IRremote.ccp IRremote.h IRremoteInt.h ,在 arduino 安装目录下的libraries\arduino_iremote_master
也就是你的库文件,如果没有请参考帖子下载
2 修改IRremote.h
1)#define RAWBUF 270 // Length of raw duration buffer
// Marks tend to be 100us too long, and spaces 100us too short
// when received due to sensor lag.
这个默认值是100,因为我们接收的数据会超过100,所以在这里改成270,当然你也可以根据需要继续扩大
3 修改IRremoteInt.h
1)#define _GAP 50000 // Minimum map between transmissions
默认的数据是5000,因为遥控器会连续发两段代码,所以5000这个参数不适用,需要改到50000,否则你只能
接受到前一段代码仍然无法控制你家的空调
2)// information for the interrupt handler
typedef struct {
uint8_t recvpin; // pin for IR data from detector
uint8_t rcvstate; // state machine
uint8_t blinkflag; // TRUE to enable blinking of pin 13 on IR processing
unsigned int timer; // state timer, counts 50uS ticks.
unsigned int rawbuf[RAWBUF]; // raw data
// uint8_t rawlen; // counter of entries in rawbuf
int rawlen;
}
irparams_t;
这段代码我们要修改的 rawlen的定义,在默认库中这个数据被定义为uint8 也就是 一个字节的整数,最大只能到255
所以我们将此参数改成int
第二部 dump 空调的代码
在你的第一部完成之后,我们可以开始dump空调代码了,
1 连线 按照我贴的连接的教程连接好你的红外接收器 ,一般 最左侧的是数据线然后是gnd 和vcc
2 在arduino 中调出IRecvDump 的例程序,可以通过 FILE->EXAMPLES->Arduino_IRremote_master 下找到
3 编译和下载
4 打开com口监控 Tools->Serial Monitor
5 使用你家的空调遥控器对着接收头按一下开关键
6 这个时候串口会跳出你家空调开关的代码
“Raw (264): 13306 3500 -1700 450 -1250 500 -400 450。。。。”
请记住第一个 13306 不是代码头,你需要把这个放到你的数据的最后也就是说你的发送代码是
Raw (264): 3500 -1700 450 -1250 500 -400 450 。。。。。。。13306“
第三部发送你的代码
请注意,由于电路的问题,你的红外发射器可能只能在很短的距离内控制家电,如果要增强效果请增加三极管放大电路
我这里不写放大的那部分,因为不是本文的核心
1 连接好线路 (此时你可以把你的红外 led 连接到你的arduino了)
这个非常简单,如果不用考虑设备电流以及其他问题,可以直接把pin 3 接到红外发射头的 正级(小的那块)
负极 是一个像旗子的一个东西,接地
2 把你接受的数据 放入程序,也就是unsigned int rawCodes_ac_close[264]这个数组【】内的数字根据你接受的数据大小来调整
- /*
- * IRremote: IRrecvDump – dump details of IR codes with IRrecv
- * An IR detector/demodulator must be connected to the input RECV_PIN.
- * Version 0.1 July, 2009
- * Copyright 2009 Ken Shirriff
- * http://arcfn.com
- * JVC and Panasonic protocol added by Kristian Lauszus (Thanks to zenwheel and other people at the original blog post)
- */
- #include <IRremote.h>
- int RECV_PIN = 11;//定义红外接收器的引脚为11
- //unsigned int rawCodes_ac_close[264]={3288,1591,432,1186,432,372,444,383,433,375,443,1184,434,382,434,383,433,384,433,383,433,1187,432,382,433,1186,432,1185,432,382,434,1186,431,1187,432,1186,432,1186,432,1186,432,383,433,1185,432,383,434,383,433,1186,432,383,433,383,433,383,433,384,433,1186,432,1186,432,1186,432,1186,432,383,433,383,433,383,433,384,433,384,433,384,433,383,433,383,433,383,433,383,433,383,433,384,433,384,432,384,432,383,433,383,432,384,432,385,432,385,432,384,432,384,432,385,432,384,432,384,432,384,432,1187,431,384,433,384,432,1187,431,1186,431,1186,431,384,433,27841,3287,1590,432,1186,431,384,434,383,433,383,433,1185,434,375,441,383,433,383,434,383,432,1186,433,382,433,1187,431,1186,432,383,433,1187,431,1186,432,1186,431,1186,432,1186,432,382,432,1187,431,383,433,384,433,1187,432,383,434,383,433,384,433,384,433,384,433,384,433,384,433,384,433,1186,432,1186,431,1187,431,1186,432,383,433,384,433,382,433,1186,430,1188,431,384,433,383,433,384,432,384,432,385,432,384,433,384,432,384,432,384,432,384,432,384,432,385,431,384,432,384,432,385,432,384,432,1188,431,383,433,384,432,1187,431,384,432,384,433,384,432,27841};
- //unsigned int rawCodes_ac_close[264]={3448,1668,453,1244,453,390,466,402,454,393,464,1242,455,400,455,401,454,402,454,402,454,1244,453,401,454,1244,453,1243,453,401,455,1243,452,1244,453,1243,453,1243,453,1243,453,402,454,1243,453,402,455,401,454,1244,453,401,454,402,454,401,454,402,454,1244,453,1244,453,1244,453,1243,453,401,454,402,454,402,454,403,454,402,454,402,454,402,454,402,454,402,454,402,454,402,454,402,454,403,453,402,453,402,454,402,453,403,453,403,452,403,453,403,453,402,453,403,453,403,453,403,453,403,453,1245,452,402,454,402,452,1245,452,1244,452,1244,452,402,454,29191,3446,1668,453,1244,452,402,455,402,454,402,454,1242,455,394,462,401,454,402,455,402,453,1244,454,401,454,1244,452,1244,453,401,454,1244,452,1243,453,1243,452,1244,453,1243,453,401,453,1245,452,402,454,402,454,1244,452,401,455,402,454,402,454,402,454,402,454,402,454,402,454,403,454,1244,453,1244,452,1245,452,1244,453,402,454,402,454,401,454,1244,451,1245,452,402,454,402,454,403,453,403,453,403,453,403,454,403,453,403,453,402,453,403,453,403,453,403,452,403,453,403,453,403,453,403,453,1245,452,402,454,402,453,1245,452,403,453,402,454,402,453,29191};
- unsigned int rawCodes_ac_close[264]={3500,1700,450,1250,500,400,450,400,450,450,450,1250,450,400,500,350,500,400,450,400,500,1250,450,400,500,1250,450,1250,450,400,500,1250,450,1300,450,1250,450,1300,450,1250,450,400,500,1250,450,400,450,400,500,1250,450,400,500,400,450,400,450,450,450,1250,450,1300,450,1250,450,1250,500,400,450,400,450,400,500,400,450,400,500,400,450,400,450,450,450,400,450,400,500,400,450,400,450,400,500,400,450,400,450,450,450,400,450,400,500,400,450,400,450,450,450,400,450,400,500,400,450,400,450,1300,450,400,450,400,500,1250,450,1250,450,1300,450,400,450,29650,3500,1700,450,1300,450,400,450,400,500,400,450,1250,500,400,450,400,450,400,500,400,450,1250,500,400,450,1250,450,1300,450,400,450,1250,500,1250,450,1250,500,1250,450,1250,500,400,450,1250,450,400,500,400,450,1250,500,400,450,400,450,450,450,400,450,400,500,400,450,400,450,400,500,1250,450,1250,500,1250,450,1250,500,400,450,400,450,400,500,1250,450,1250,500,400,450,400,450,450,450,400,450,400,500,350,500,400,450,450,450,400,450,400,450,450,450,400,450,400,500,400,450,400,450,450,450,1250,450,400,500,400,450,1250,450,400,500,400,450,400,500,13306};
- //上面改成你接受到的数据,请注意把接受到的第一个数据放到最后
- IRrecv irrecv(RECV_PIN);
- decode_results results;
- IRsend irsend;
- //test
- void setup()
- {
- Serial.begin(9600);
- irrecv.enableIRIn(); // 初始化红外接收器
- }
- void dump(decode_results *results) {
- int count = results->rawlen;
- if (results->decode_type == UNKNOWN) {
- Serial.print(“Unknown encoding: “);
- }
- else if (results->decode_type == NEC) {
- Serial.print(“Decoded NEC: “);
- }
- else if (results->decode_type == SONY) {
- Serial.print(“Decoded SONY: “);
- }
- else if (results->decode_type == RC5) {
- Serial.print(“Decoded RC5: “);
- }
- else if (results->decode_type == RC6) {
- Serial.print(“Decoded RC6: “);
- }
- else if (results->decode_type == PANASONIC) {
- Serial.print(“Decoded PANASONIC – Address: “);
- Serial.print(results->panasonicAddress,HEX);
- Serial.print(” Value: “);
- }
- else if (results->decode_type == JVC) {
- Serial.print(“Decoded JVC: “);
- }
- Serial.print(results->value, HEX);
- Serial.print(” (“);
- Serial.print(results->bits, DEC);
- Serial.println(” bits)”);
- Serial.print(“Raw (“);
- Serial.print(count, DEC);
- Serial.print(“): “);
- for (int i = 0; i < count; i++) {
- if ((i % 2) == 1) {
- Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
- }
- else {
- Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
- }
- Serial.print(” “);
- }
- Serial.println(“”);
- }
- void loop() {
- irsend.sendRaw(rawCodes_ac_close,264, 40);
- delay(5);
- // irsend.sendRaw(rawCodes_ac_close,264, 40);
- if (irrecv.decode(&results)) {
- Serial.println(results.value, HEX);
- dump(&results);
- // irsend.mark(560);
- // irsend.space(560);
- // irsend.mark(2000);
- // irsend.space(560);
- //rsend.sendRaw(rawCodes_ac_close,6, 32);
- irrecv.resume(); // Receive the next value
- }
- delay(5000);
- }
复制代码
好了,编译下载,通电,用led 对准你家的空调就可以了