让我们一起爱米兰
站内搜搜:
移动设备
请扫描二维码
或访问
m.milan100.com
您所在的位置 -> 米兰百分百 -> arduino -> Arduino发射红外信号

Arduino发射红外信号

点击数:1359 发表时间:2016-04-21 09:31:11 作者: 来源链接:
分享到:
分享到微信

上一篇提到了红外编码的原理,此篇用arduino软件模拟发出38K的载波信号配合红外发射头,发出带有载波信号的38K红外光,使arduino变成红外遥控器,配合红外接收就用此可以遥控各家电。
例程1:红外发射模块(取缔红外遥控器)配合arduino制作成红外遥控器,用串口输入一个数字,遥控器发射出一段编码,一边用红外接收模块,显示出接收到的编码(红外接收参照http://www.arduino.cn/thread-1220-1-1.html此贴)
连线示意图:
 

?

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#define ADD 0x00
int IR_S =  8;     //接arduino 8号引脚
void setup()
{
  pinMode(IR_S, OUTPUT);
  Serial.begin(9600); // 9600 波特率
}
void loop()
{
  uint8_t dat,temp;
  if ( Serial.available()) {
    temp = Serial.read();//?
     
    IR_Send38KHZ(280,1);//发送9ms的起始码
    IR_Send38KHZ(140,0);//发送4.5ms的结果码
     
    IR_Sendcode(ADD);//用户识别码
    dat=~ADD;
    IR_Sendcode(dat);//用户识别码反吗
     
    IR_Sendcode(temp);//操作码
    dat=~temp;
    IR_Sendcode(dat);//操作码反码
     
    IR_Send38KHZ(21,1);//发送结束码
  }
  delay(200);
}
void IR_Send38KHZ(int x,int y) //产生38KHZ红外脉冲
{
for(int i=0;i<x;i++)//15=386US
{
           if(y==1)
           {
     digitalWrite(IR_S,1);
            delayMicroseconds(9);
     digitalWrite(IR_S,0);
            delayMicroseconds(9);
           }
           else
           {
     digitalWrite(IR_S,0);
            delayMicroseconds(20);
           }           
}
}
void IR_Sendcode(uint8_t x) //
{
    for(int i=0;i<8;i++)
     {
       if((x&0x01)==0x01)
        {
           IR_Send38KHZ(23,1);
           IR_Send38KHZ(64,0);            
        }
        else
         {
            IR_Send38KHZ(23,1);
            IR_Send38KHZ(21,0); 
         }
       x=x>>1;
     
}


打开串口,随意发送一个数字,红外发射头会发出带编码的38K红外波, 另一方面用一个红外接收头就可以接收到此码,将其解码就会知道遥控器是按的哪个按键。
接收方面,arduino+红外接收模块进行解码 
红外遥控函数库: https://github.com/shirriff/Arduino-IRremote

?

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <IRremote.h>
 
int RECV_PIN = 11;
 
IRrecv irrecv(RECV_PIN);
 
decode_results results;
 
void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); //初始化红外遥控
  pinMode(12,1);
}
 
void loop() {
  if (irrecv.decode(&results)) {
if(results.value==16753245)      //确认接收到的第一排按键1的编码,此码是预先读出来的按键编码。
  {
  digitalWrite(12,1);                //点亮LED
   Serial.println("turn on LED"); //串口显示开灯
  }
  else if(results.value==16736925)   //确认接收到的第一排按键2的编码
  {
   digitalWrite(12,0);            //熄灭LED
    Serial.println("turn off LED");    //串口显示关灯
  }
    irrecv.resume(); // 接收下一个值
  }
}


例程2:arduino模拟SONY遥控器,用arduino串口发送一个字符便可以遥控SONY家电的开关。

/*********红外发射头接arduino的3号数字口**************************/

?

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
#include <IRremote.h>
 
IRsend irsend;
 
void setup()
{
  Serial.begin(9600);
}
 
void loop() {
  if (Serial.read() != -1) {
    for (int i = 0; i < 3; i++) {
      irsend.sendSony(0xa90, 12); // Sony TV power code
      delay(100);
    }
  }
}


0
很 好
0
一 般
0
差 劲
热门新闻
相关文章
上一篇: arduino连接DS1302模块显示或修改时钟clk、dat、rst
下一篇: Arduino 与 Raspberry Pi(树莓派)的内存及性能对比
评论区
匿名

返回首页 | 收藏本页 | 回到顶部
Copyright 2010. 米兰百分百 Powered By Bridge.
京ICP备15050557号