Arduino入门教程--第二十课--红外遥控器介绍
这一节我们介绍怎么在Arduino下使用红外遥控器~这里我们会用到“库”,Arduino的一大特点就在于他有很多高手们写好的“库”,能用几行很简单的代码实现很复杂的功能。
“库”可以理解为把一些复杂的代码封装后的函数。Arduino的所用到的语言比如:Serial.begin(9600);delay(1);等也是把底层封装成的一个个库。
这次我们会用到IRremote这个库,这个库支持众多的红外协议,如:NEC, Sony SIRC, Philips RC5, Philips RC6 等。
首先我们先把这次使用到的IRremote库下载回来。
把压缩包解压到Arduino IDE的libraries文件夹中
比如我们这次使用的IRremote可以解压到Arduino_IRremote_master文件夹中。
需要注意的是在库文件夹下要直接显示*.cpp和*.h文件,绝对不可以把这些库文件再套一层二级目录,这样子就会导致IDE无法识别。
接下来我们需要用到一个38K的红外接收器,如果,红外接收器的三个引脚分别为OUT,GND,VCC。
按照下图连接好Arduino。
然后把代码放入IDE编译下载
#include <IRremote.h>// 使用IRRemote函数库
const int irReceiverPin = 2;// 红外接收器的 OUTPUT 引脚接在 PIN2 接口 定义irReceiverPin变量为PIN2接口
IRrecv irrecv(irReceiverPin); // 设置irReceiverPin定义的端口为红外信号接收端口
decode_results results; // 定义results变量为红外结果存放位置
void setup()
{
Serial.begin(9600); // 开启串口,波特率为9600
irrecv.enableIRIn(); // 启动红外解码
}
void loop()
{
if (irrecv.decode(&results)) { // 解码成功,把数据放入results变量中
// 把数据输入到串口
Serial.print("irCode: ");
Serial.print(results.value, HEX); // 显示红外编码
Serial.print(",bits: ");
Serial.println(results.bits); // 显示红外编码位数
irrecv.resume(); // 继续等待接收下一组信号
}
delay(600); //延时600毫秒,做一个简单的消抖
}
然后打开串口监视器,波特率设置为9600,使用遥控器对着红外头按下按键,串口监视器上就会出来当前按键的红外编码与位数。
显示irCode:FFFFFFFF,bits:32是因为遥控器上没有做中断,误码导致,只要按一下松开,再按一下松开就没问题。 我试成功了。解压后文件夹应改为IRremote,如果是Arduino_IRremote_master的话Arduino IDE会报错的。不过我用的是Mac系统,不知道Windows下面文件夹为Arduino_IRremote_master是否可以。另外,我按一下遥控器的键,Serial Monitor里面有时候会出来好几条数据,我后来把delay(600)改为delay(2000)就没个这问题了。不过我想改成delay(1000)应该也可以,只是这个时间不能太短。 代码不能通过检验 cyq456 发表于 2012-12-7 23:21 static/image/common/back.gif
代码不能通过检验
{:soso_e103:} 不能编译吗?是不是库没装好。 写得很好.:D 38K的红外接收器?我手上有两个长得不同的红外接收器,怎样掉它的库,然后用到的语句一样吗? tgyfish 发表于 2012-11-21 21:31 static/image/common/back.gif
38K的红外接收器?我手上有两个长得不同的红外接收器,怎样掉它的库,然后用到的语句一样吗?
库一样,语句也一样。。只是接收头引脚定义可能会有区别。 事实证明离得远了就挂了 tgyfish 发表于 2012-11-28 18:32 static/image/common/back.gif
事实证明离得远了就挂了
恩,遥控器发射功率是很小的,1米以外就可能无出现很大误码,如果远距离,需要大功率发射端才行。 #include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
}
}
The IRrecv class performs the decoding, and is initialized with enableIRIn(). The decode() method is called to see if a code has been received; if so, it returns a nonzero value and puts the results into the decode_results structure. (For details of this structure, see the examples/IRrecvDump sketch.) Once a code has been decoded, the resume() method must be called to resume receiving codes. Note that decode() does not block; the sketch can perform other operations while waiting for a code because the codes are received by an interrupt routine.
上面是arcfn提供的代码,下面是解释,但是英文不好,用翻译软件翻译出来的又有点乱,有没有详细一点的解释吖? 本帖最后由 laofuz1024 于 2012-12-1 21:08 编辑
主要还是这一句看得不太明白
irrecv.decode(&results);
还有这一句的等待,是直程序停下来等待吗?就好像delay()无限长,到接收到下一个数据
irrecv.resume(); 11楼的兄弟是对的,我在xp系统下,要把解压文件夹名改成IRremote,和其它库文件使用是一样的,文件夹名和里面的库文件同名才行。 我和9楼兄弟一样,没有编译过。at java.util.regex.Pattern$BranchConn.match(Pattern.java:4078)
类似于此很长一串报错。 也成功了,是直接拷贝网页代码出错,直接输入就没有问题了。谢谢了。 请问'IRrecv’does not name atype是什么错误?