|
大神奈何col在教程区有一篇使用IRremote库红外遥控家里的电器,
里面提到了用IR库的RAW解码空调遥控器,让我受益匪浅。
但是空调的编码长度太长,原本库里面rawbuf缓冲区100的长度远远不够,如果按照文章改成255,可以解决一部分空调的解码了。但是有些“高帅富”空调编码长度更长,255也不够用,比如我家里的一台三菱变频机,具体表现是解码后读出的RAW指令长度正好是255,这就说明缓存溢出了。
这种情况下,单纯修改irremote.h里面的rawbuf已经没用了,因为库中缓存上限就是255,如果改成300之类的,收到的RAW指令长度反而会很小,指令是无效的。
还好,国外的大神也就是irremote库的编辑者AnalysIR 意识到了这个问题,所以在他的网站上发表了针对长空调指令的获取代码。
当然不愿意看的童鞋可以使用我修改过的代码,我的代码把数组里面的负号去掉了,另外注意红外接收管的信号脚是接在2号口的:
[mw_shl_code=c,true]/*
Author: AnalysIR
Revision: 1.0
This code is provided to overcome an issue with Arduino IR libraries
It allows you to capture raw timings for signals longer than 255 marks & spaces.
Typical use case is for long Air conditioner signals.
You can use the output to plug back into IRremote, to resend the signal.
This Software was written by AnalysIR.
Usage: Free to use, subject to conditions posted on blog below.
Please credit AnalysIR and provide a link to our website/blog, where possible.
Copyright AnalysIR 2014
Please refer to the blog posting for conditions associated with use.
http://www.analysir.com/blog/201 ... ol-signals-arduino/
Connections:
IR Receiver Arduino
V+ -> +5v
GND -> GND
Signal Out -> Digital Pin 2
(If using a 3V Arduino, you may connect V+ to +3V)
*/
#define LEDPIN 13
//you may increase this value on Arduinos with greater than 2k SRAM
#define maxLen 800
volatile unsigned int irBuffer[maxLen]; //stores timings - volatile because changed by ISR
volatile unsigned int x = 0; //Pointer thru irBuffer - volatile because changed by ISR
void setup() {
Serial.begin(9600); //change BAUD rate as required
attachInterrupt(0, rxIR_Interrupt_Handler, CHANGE);//set up ISR for receiving IR signal
}
void loop() {
// put your main code here, to run repeatedly:
//Serial.println(F("Press the button on the remote now - once only"));
delay(5000); // pause 5 secs
if (x) { //if a signal is captured
digitalWrite(LEDPIN, HIGH);//visual indicator that signal received
Serial.println();
Serial.print(F("Raw: (")); //dump raw header format - for library
Serial.print((x - 1));
Serial.print(F(") "));
detachInterrupt(0);//stop interrupts & capture until finshed here
for (int i = 1; i < x; i++) { //now dump the times
//if (!(i & 0x1)) Serial.print(F("-"));
Serial.print(irBuffer - irBuffer[i - 1]);
Serial.print(F(","));
}
x = 0;
Serial.println();
Serial.println();
digitalWrite(LEDPIN, LOW);//end of visual indicator, for this time
attachInterrupt(0, rxIR_Interrupt_Handler, CHANGE);//re-enable ISR for receiving IR signal
}
}
void rxIR_Interrupt_Handler() {
if (x > maxLen) return; //ignore if irBuffer is already full
irBuffer[x++] = micros(); //just continually record the time-stamp of signal transitions
}[/mw_shl_code]
我运行这个代码以后,用空调的遥控器按了下开机,显示的raw代码长度竟然有583位之多!
接下来把这个长数组黏贴到红外发送程序中,就可以实现遥控各种空调的计划了。
最后说一句,这个新代码的最大指令长度为800位,如果还不够,只能再去抱AnalysIR的大腿了。
|
|