找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6224|回复: 3
收起左侧

Arduino红外遥控解码

[复制链接]
ID:372242 发表于 2018-7-16 00:58 | 显示全部楼层 |阅读模式
对遥控器发射出来的编码脉冲进行解码,根据解码结果执行相应的动作。返样大家就可以用遥控器遥控你的器件了,让它听你的指挥。
0.png

把 IRremote 文件夹放到 编译器安装目录下的\Arduino\libraries里。
例如我的:C:\Program Files\Arduino\libraries


Arduino源程序如下:
  1. #include <IRremote.h>
  2. int RECV_PIN = 11;
  3. int LED1 = 2;
  4. int LED2 = 3;
  5. int LED3 = 4;
  6. int LED4 = 5;
  7. int LED5 = 6;
  8. int LED6 = 7;
  9. long on1  = 0x00FFA25D;
  10. long off1 = 0x00FFE01F;
  11. long on2 = 0x00FF629D;
  12. long off2 = 0x00FFA857;
  13. long on3 = 0x00FFE21D;
  14. long off3 = 0x00FF906F;
  15. long on4 = 0x00FF22DD;
  16. long off4 = 0x00FF6897;
  17. long on5 = 0x00FF02FD;
  18. long off5 = 0x00FF9867;
  19. long on6 = 0x00FFC23D;
  20. long off6 = 0x00FFB047;
  21. IRrecv irrecv(RECV_PIN);
  22. decode_results results;
  23. // Dumps out the decode_results structure.
  24. // Call this after IRrecv::decode()
  25. // void * to work around compiler issue
  26. //void dump(void *v) {
  27. //  decode_results *results = (decode_results *)v
  28. void dump(decode_results *results) {
  29.   int count = results->rawlen;
  30.   if (results->decode_type == UNKNOWN)
  31.     {
  32.      Serial.println("Could not decode message");
  33.     }
  34.   else
  35.    {
  36.     if (results->decode_type == NEC)
  37.       {
  38.        Serial.print("Decoded NEC: ");
  39.       }
  40.     else if (results->decode_type == SONY)
  41.       {
  42.        Serial.print("Decoded SONY: ");
  43.       }
  44.     else if (results->decode_type == RC5)
  45.       {
  46.        Serial.print("Decoded RC5: ");
  47.       }
  48.     else if (results->decode_type == RC6)
  49.       {
  50.        Serial.print("Decoded RC6: ");
  51.       }
  52.      Serial.print(results->value, HEX);
  53.      Serial.print(" (");
  54.      Serial.print(results->bits, DEC);
  55.      Serial.println(" bits)");
  56.    }
  57.      Serial.print("Raw (");
  58.      Serial.print(count, DEC);
  59.      Serial.print("): ");

  60.   for (int i = 0; i < count; i++)
  61.      {
  62.       if ((i % 2) == 1) {
  63.       Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
  64.      }
  65.     else  
  66.      {
  67.       Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
  68.      }
  69.     Serial.print(" ");
  70.      }
  71.       Serial.println("");
  72.      }

  73. void setup()
  74. {
  75.   pinMode(RECV_PIN, INPUT);   
  76.   pinMode(LED1, OUTPUT);
  77.   pinMode(LED2, OUTPUT);
  78.   pinMode(LED3, OUTPUT);
  79.   pinMode(LED4, OUTPUT);
  80.   pinMode(LED5, OUTPUT);
  81.   pinMode(LED6, OUTPUT);  
  82.   pinMode(13, OUTPUT);
  83.   Serial.begin(9600);
  84.   
  85.   irrecv.enableIRIn(); // Start the receiver
  86. }

  87. int on = 0;
  88. unsigned long last = millis();

  89. void loop()
  90. {
  91.   if (irrecv.decode(&results))
  92.    {
  93.     // If it's been at least 1/4 second since the last
  94.     // IR received, toggle the relay
  95.     if (millis() - last > 250)
  96.       {
  97.        on = !on;
  98. //       digitalWrite(8, on ? HIGH : LOW);
  99.        digitalWrite(13, on ? HIGH : LOW);
  100.        dump(&results);
  101.       }
  102.     if (results.value == on1 )
  103.        digitalWrite(LED1, HIGH);
  104.     if (results.value == off1 )
  105.        digitalWrite(LED1, LOW);
  106.     if (results.value == on2 )
  107.        digitalWrite(LED2, HIGH);
  108.     if (results.value == off2 )
  109.        digitalWrite(LED2, LOW);
  110.     if (results.value == on3 )
  111.        digitalWrite(LED3, HIGH);
  112.     if (results.value == off3 )
  113.        digitalWrite(LED3, LOW);
  114.     if (results.value == on4 )
  115.        digitalWrite(LED4, HIGH);
  116.     if (results.value == off4 )
  117.        digitalWrite(LED4, LOW);
  118.     if (results.value == on5 )
  119.        digitalWrite(LED5, HIGH);
  120.     if (results.value == off5 )
  121.        digitalWrite(LED5, LOW);
  122.     if (results.value == on6 )
  123.        digitalWrite(LED6, HIGH);
  124.     if (results.value == off6 )
  125.        digitalWrite(LED6, LOW);        
  126.     last = millis();      
  127.     irrecv.resume(); // Receive the next value
  128.   }
  129. }
复制代码

所有资料51hei提供下载:
20.红外遥控器解码.rar (575.72 KB, 下载次数: 37)



回复

使用道具 举报

ID:275311 发表于 2019-1-29 22:26 | 显示全部楼层
有指定什么板吗?
回复

使用道具 举报

ID:275311 发表于 2019-1-29 22:32 | 显示全部楼层
程序有没有指定什么板吗?
能和接收代码写在一起吗?
回复

使用道具 举报

ID:807182 发表于 2020-7-24 16:23 | 显示全部楼层
nano和uno都可以
回复

使用道具 举报

5#
无效楼层,该帖已经被删除
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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