简体   繁体   English

如何正确接收和发送来自 Arduino 的原始 IR 数据

[英]How to correctly receive and send raw IR data from Arduino

Following is my code to read the raw IR data from Arduino:以下是我从 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("");
}

Using that I can get this from a remote controller:使用它我可以从遥控器得到这个:

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,};

So on sending this data I can use this instruction:因此,在发送此数据时,我可以使用此指令:

irsend.sendRaw(raw,100,38);

The problem is I cannot get any respond from the device I need to control.问题是我无法从需要控制的设备中得到任何响应。 I already check my IR transmitter my reading is the same above.我已经检查了我的红外发射器,我的读数与上面相同。

Im I missing something?我错过了什么吗?

You should take multiple reading of the signal, then average their values to remove little bias error.您应该多次读取信号,然后平均它们的值以消除很小的偏差误差。 Be sure your emitter and receiver use the same light wavelength and to work at 38KHz (you are transmitting modulating at 38KHz. If your emitter does modulation for you, then you should not use IRremote).确保您的发射器和接收器使用相同的光波长并在 38KHz 下工作(您以 38KHz 调制传输。如果您的发射器为您进行调制,那么您不应该使用 IRremote)。

Also, what do you expect as "response"?另外,您期望什么是“响应”? Normally, IR communication is one-way.通常,IR 通信是单向的。

You are reaching the 100 bytes limit of RAWBUF, so you probably need to increase that number.您已达到 RAWBUF 的 100 字节限制,因此您可能需要增加该数字。 Try going up to 400.尝试达到 400。

Open in IRemote.h and modify this line (about #122)在IRemote.h中打开并修改这一行(约#122)

#define RAWBUF 100 // Length of raw duration buffer

to something bigger更大的东西

#define RAWBUF 400 // Length of raw duration buffer

I'm tying your code with a Samsung air conditioner and I'm receiving 116 bytes我正在将您的代码与三星空调绑定,我收到了 116 个字节

I just started playing with Arduino and was trying to replicate the code from my Pentax remote by doing what you did, but it wasn't working.我刚开始玩 Arduino,并试图通过执行您所做的操作来复制 Pentax 遥控器中的代码,但它不起作用。 Then I found this article: http://www.righto.com/2009/08/multi-protocol-infrared-remote-library.html .然后我找到了这篇文章: http : //www.righto.com/2009/08/multi-protocol-infrared-remote-library.html I says: "There are two differences between the raw buffers for sending and for receiving. The send buffer values are in microseconds, while the receive buffer values are in 50 microsecond ticks."我说:“发送和接收的原始缓冲区之间有两个区别。发送缓冲区值以微秒为单位,而接收缓冲区值以 50 微秒为单位。”

So all you need to do is multiply every element in your raw array by 50 and send those values in irsend.sendRaw(raw,100,38) .因此,您需要做的就是将原始数组中的每个元素乘以 50,然后将这些值发送到irsend.sendRaw(raw,100,38) Worked for me.为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM