找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 6145|回復(fù): 3
打印 上一主題 下一主題
收起左側(cè)

Arduino紅外遙控解碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
對(duì)遙控器發(fā)射出來(lái)的編碼脈沖進(jìn)行解碼,根據(jù)解碼結(jié)果執(zhí)行相應(yīng)的動(dòng)作。返樣大家就可以用遙控器遙控你的器件了,讓它聽(tīng)你的指揮。


把 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. }
復(fù)制代碼

所有資料51hei提供下載:
20.紅外遙控器解碼.rar (575.72 KB, 下載次數(shù): 37)



分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:275311 發(fā)表于 2019-1-29 22:26 | 只看該作者
有指定什么板嗎?
回復(fù)

使用道具 舉報(bào)

板凳
ID:275311 發(fā)表于 2019-1-29 22:32 | 只看該作者
程序有沒(méi)有指定什么板嗎?
能和接收代碼寫(xiě)在一起嗎?
回復(fù)

使用道具 舉報(bào)

地板
ID:807182 發(fā)表于 2020-7-24 16:23 | 只看該作者
nano和uno都可以
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表