用于电视、DVD/蓝光或机顶盒的遥控器使用红外发射器和接收器。
请注意,有许多不同类型的红外接收器,因此在提供连接时要小心,引脚(VCC、GND、OUT)可能会在您使用的模型中被打乱。
/*
* IRremote: IRrecvDemo - demonstrates receiving 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
*/
#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
// In case the interrupt driver crashes on setup, give a clue
// to the user what's going on.
Serial.println("Enabling IRin");
irrecv.enableIRIn(); // Start the receiver
Serial.println("Enabled IRin");
}
void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
}
delay(100);
}
现在让我们将 LED 连接到Arduino的数字引脚之一,并编辑上面的示例以使用红外遥控器打开/关闭 LED。
/*
IRremote: IRrecvDemo - demonstrates receiving 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
*/
#include
int RECV_PIN = 11;
int codeON = 16744575;
int codeOFF = 16711935;
int code;
int LED = 9;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
// In case the interrupt driver crashes on setup, give a clue
// to the user what's going on.
Serial.println("Enabling IRin");
irrecv.enableIRIn(); // Start the receiver
Serial.println("Enabled IRin");
pinMode(LED, OUTPUT); digitalWrite(LED, LOW);
}
void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, DEC);
code = results.value, DEC;
irrecv.resume(); // Receive the next value
}
if (code == codeON)
digitalWrite(LED, HIGH);
else if (code == codeOFF)
digitalWrite(LED, LOW);
delay(100);
}
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
全部0条评论
快来发表一下你的评论吧 !