如何从Arduino正确接收和发送原始IR数据

时间:2014-03-30 23:26:15

标签: c embedded arduino infrared

以下是我从Arduino读取原始IR数据的代码:

#define sprint Serial.print 
#define sprintln Serial.println
#include <IRremote.h>

#define IR_RCVR_PIN 11
IRrecv ir_receiver(IR_RCVR_PIN);
decode_results results;

void setup() {
    Serial.begin(9600);
    ir_receiver.enableIRIn(); // Start the receiver
}

void loop() {
   if (ir_receiver.decode(&results)) {
    dump(&results);
    ir_receiver.resume(); // Receive the next value
   }
}

int c = 1;

void dump(decode_results *results) {
   int count = results->rawlen;
   sprintln(c);
   c++;
   sprintln("For IR Scope: ");
   for (int i = 1; i < count; i++) {
       sprint("0x");
       sprint((unsigned int)results->rawbuf[i], HEX);
    sprint(" ");
   }

   sprintln("");
   sprintln("For Arduino sketch: ");
   sprint("unsigned int raw[");
   sprint(count, DEC);
   sprint("] = {");
   for (int i = 1; i < count; i++) {
       sprint("0x");
       sprint((unsigned int)results->rawbuf[i], HEX);
       sprint(",");
    }
    sprint("};");
    sprintln("");
    sprint("irsend.sendRaw(raw,");
    sprint(count, DEC);
    sprint(",38);");
    sprintln("");
    sprintln("");
}

使用它我可以从遥控器获取:

1
For IR Scope: 
0x47 0x1F 0xB 0x17 0xA 0x17 0xB 0x6 0xA 0x6 0xB 0x6 0xA 0x17 0xB 0x6 0xA 0x6 0xB 0x17     
0xA 0x17 0xA 0x7 0xA 0x17 0xA 0x7 0xA 0x6 0xB 0x17 0xA 0x17 0xA 0x6 0xB 0x17 0xA 0x17    
0xB 0x6 0xA 0x6 0xB 0x17 0xA 0x6 0xB 0x6 0xA 0x17 0xB 0x6 0xA 0x6 0xB 0x6 0xA 0x7 0xA     
0x6 0xB 0x6 0xA 0x6 0xB 0x6 0xA 0x7 0xA 0x6 0xB 0x6 0xA 0x7 0xA 0x6 0xB 0x6 0xA 0x6 0xB    
0x6 0xA 0x7 0xA 0x6 0xB 0x6 0xA 0x6 0xB 0x17 0xA 0x6 0xB 0x6 0xA 

For Arduino sketch: 

unsigned int raw[100] =       
{0x47,0x1F,0xB,0x17,0xA,0x17,0xB,0x6,0xA,0x6,0xB,0x6,0xA,0x17,
 0xB,0x6,0xA,0x6,0xB,0x17,0xA,0x17,0xA,0x7,0xA,0x17,0xA,0x7,0xA,0x6,
 0xB,0x17,0xA,0x17,0xA,0x6,0xB,0x17,0xA,0x17,0xB,0x6,0xA,0x6,0xB,0x17,0xA,
 0x6,0xB,0x6,0xA,0x17,0xB,0x6,0xA,0x6,0xB,0x6,0xA,0x7,0xA,0x6,0xB,0x6,0xA,
 0x6,0xB,0x6,0xA,0x7,0xA,0x6,0xB,0x6,0xA,0x7,0xA,0x6,0xB,0x6,0xA,0x6,0xB,
 0x6,0xA,0x7,0xA,0x6,0xB,0x6,0xA,0x6,0xB,0x17,0xA,0x6,0xB,0x6,0xA,};

因此,在发送此数据时,我可以使用此指令:

irsend.sendRaw(raw,100,38);

问题是我无法从我需要控制的设备得到任何响应。我已经检查过我的红外发射器,我的阅读内容与上面相同。

我错过了什么?

3 个答案:

答案 0 :(得分:0)

您应该多次读取信号,然后平均它们的值以消除很小的偏差。确保您的发射器和接收器使用相同的光波长并工作在38KHz(您正在传输调制在38KHz。如果您的发射器为您调制,那么您不应该使用IRremote)。

此外,您对“#34;响应&#34;”的期望是什么?通常,IR通信是单向的。

答案 1 :(得分:0)

您正在达到RAWBUF的100字节限制,因此您可能需要增加该数字。尝试上升到400。

在IRemote.h中打开并修改此行(约#122)

#define RAWBUF 100 // Length of raw duration buffer

更大的东西

#define RAWBUF 400 // Length of raw duration buffer

我用三星空调捆绑你的代码而且我收到的是116字节

答案 2 :(得分:0)

我刚开始玩 Arduino,并试图通过执行您所做的操作从 Pentax 遥控器复制代码,但它不起作用。然后我找到了这篇文章:http://www.righto.com/2009/08/multi-protocol-infrared-remote-library.html。我说:“发送和接收的原始缓冲区有两个区别。发送缓冲区值以微秒为单位,而接收缓冲区值以 50 微秒为单位。”

因此,您需要做的就是将原始数组中的每个元素乘以 50,然后将这些值发送到 irsend.sendRaw(raw,100,38)。为我工作。