极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 30348|回复: 12

使用红外线库文件IRremote完美控制315m超再生模块

[复制链接]
发表于 2015-1-31 00:11:55 | 显示全部楼层 |阅读模式
本帖最后由 peckbyte 于 2015-1-31 12:19 编辑

买了2对315m超再生模块,一直都找不到好的库来编码解码,后来在网上看到一位大侠提示用红外线arduino的类库可以操作315m,于是花了些时间研究了一下,发现可以完美的使用在315m超再生模块。

IRremote库主要的改动主要有一下几处:
1.删除了其他的编码解码模式,只保留了常见的NEC模式,这也是大多数家电红外线遥控器最常见的模式;
2.在IRremoteInt.h文件中改动:#define TOLERANCE 50  // percent tolerance in measurements <<---增加了容错率
3.关键改动,多次试验发现解码不成功,发现红外线发射时,arduino接收的数据BUFFER[0]会有额外的一个数据,所以解码程序是从buffer[1]处开始分析数据,而315模块没有额外的这个数据,所以我们要改动解码程序,buffer[0]开始解码。

实验用例子:发送程序(注意,库文件我改了个名字:IRremote315)
  1. #include <IRremote315.h>
  2. #include <IRremoteInt315.h>



  3. IRsend irsend;                          // 定义 IRsend 物件来发射红外线讯号

  4. void setup()
  5. {
  6.   pinMode(3, OUTPUT);   

  7. }

  8. void loop()
  9. {
  10.   Serial.print("SendIR: ");
  11.   irsend.sendNEC(0x765585, 32);   // 记得换成你遥控器的红外线编码
  12.   delay(3000);             // 等待3秒
  13. }
复制代码


接收程序:
  1. #include <IRremote315.h>
  2. #include <IRremoteInt315.h>



  3. int RECV_PIN = 11;
  4. int LED1 = 2;
  5. long on1  = 0x00FF906F;
  6. int sta=1;
  7. IRrecv irrecv(RECV_PIN);
  8. decode_results results;
  9. // Dumps out the decode_results structure.
  10. // Call this after IRrecv::decode()
  11. // void * to work around compiler issue
  12. //void dump(void *v) {
  13. //  decode_results *results = (decode_results *)v
  14. void dump(decode_results *results) {
  15.   int count = results->rawlen;
  16.   if (results->decode_type == UNKNOWN)
  17.     {
  18.      Serial.println("Could not decode message");
  19.     }
  20.   else
  21.    {
  22.     if (results->decode_type == NEC)
  23.       {
  24.        Serial.print("Decoded NEC: ");
  25.       }
  26.    
  27.      Serial.print(results->value, HEX);
  28.      Serial.print(" (");
  29.      Serial.print(results->bits, DEC);
  30.      Serial.println(" bits)");
  31.    }
  32.      Serial.print("Raw (");
  33.      Serial.print(count, DEC);
  34.      Serial.print("): ");

  35.   for (int i = 0; i < count; i++)
  36.      {
  37.       if ((i % 2) == 1) {
  38.       Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
  39.      }
  40.     else  
  41.      {
  42.       Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
  43.      }
  44.     Serial.print(" ");
  45.      }
  46.       Serial.println("");
  47.      }

  48. void setup()
  49. {
  50.   pinMode(RECV_PIN, INPUT);   
  51.   pinMode(LED1,OUTPUT);
  52.   pinMode(13, OUTPUT);
  53.   Serial.begin(9600);
  54.   
  55.   irrecv.enableIRIn(); // Start the receiver
  56. }
  57. int on = 0;
  58. unsigned long last = millis();

  59. void loop()
  60. {
  61.   if (irrecv.decode(&results))
  62.    {
  63.     // If it's been at least 1/4 second since the last
  64.     // IR received, toggle the relay
  65.     if (millis() - last > 250)
  66.       {
  67.        on = !on;
  68. //       digitalWrite(8, on ? HIGH : LOW);
  69.        digitalWrite(13, on ? HIGH : LOW);
  70.        dump(&results);
  71.       }
  72.     if (results.value == on1 )
  73.        {
  74.          if(sta==1)
  75.          {digitalWrite(LED1, HIGH);
  76.          sta=0;
  77.          }
  78.          else
  79.          {digitalWrite(LED1, LOW);
  80.          sta=1;
  81.          }
  82.         
  83.        }

  84.     last = millis();      
  85.     irrecv.resume(); // Receive the next value
  86.   }
  87. }
复制代码




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2015-1-31 05:31:40 | 显示全部楼层
本帖最后由 sleept 于 2015-1-31 05:33 编辑

你的编码芯片 是不是PT2262 ? 你试过其他型号的发射芯片是否也能解码?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-1-31 12:20:39 | 显示全部楼层
sleept 发表于 2015-1-31 05:31
你的编码芯片 是不是PT2262 ? 你试过其他型号的发射芯片是否也能解码?

这种便宜的315m模块没有解码芯片,就是用arduino编程解码的。
回复 支持 反对

使用道具 举报

发表于 2015-1-31 12:43:54 | 显示全部楼层
不容易,有现成的库
回复 支持 反对

使用道具 举报

发表于 2015-1-31 13:37:24 | 显示全部楼层
我有好多这个东西,不知道干嘛用的,
回复 支持 反对

使用道具 举报

发表于 2015-1-31 17:15:18 | 显示全部楼层
学慧放弃 发表于 2015-1-31 13:37
我有好多这个东西,不知道干嘛用的,

这个就是遥控器上面的  摩托车呀  卷帘门呀 那类的  
回复 支持 反对

使用道具 举报

发表于 2015-1-31 21:18:17 | 显示全部楼层
另外有一套叫Virtual Wire的也不錯
現在整併進RadioHead,ASK模組
回复 支持 反对

使用道具 举报

发表于 2015-4-9 13:38:35 | 显示全部楼层
学习了。
http://arduino.surenpi.com
回复 支持 反对

使用道具 举报

发表于 2015-5-7 15:55:54 | 显示全部楼层
mark,占坑学习
回复 支持 反对

使用道具 举报

发表于 2015-7-5 22:40:29 | 显示全部楼层
支持折腾 ,顶起楼主!!!
回复 支持 反对

使用道具 举报

发表于 2015-7-7 15:40:01 | 显示全部楼层
学习一下一下
回复 支持 反对

使用道具 举报

发表于 2015-10-14 12:46:51 | 显示全部楼层
可以对这个函数库做一下代码分析吗?
回复 支持 反对

使用道具 举报

发表于 2016-3-14 21:03:52 | 显示全部楼层
我也买了两套,打算用来造遥控灯。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-26 01:05 , Processed in 0.067649 second(s), 29 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表