2015年7月27日 星期一

Arduino IR Remote 程式-1

Arduino IR Remote 程式-1

參考   http://yehnan.blogspot.tw/2014/09/arduinoerror-tkd2-was-not-declared.html

1) 編譯錯誤 錯誤問題

#include <IRremote.h>
void setup() {
}
void loop() {
}

就會出現底下的編譯錯誤訊息(若選擇「Arduino Robot Control」以外的板子):

D:\arduino-1.6.5-r2-windows\libraries\RobotIRremote\IRremoteTools.cpp:5:16: error: 'TKD2' was not declared in this scope
 int RECV_PIN = TKD2; // the pin the IR receiver is connected to
  
原因在於,Arduino IDE 1.5.5 r2開始加入一套縮減版的IRremote程式庫,名為RobotIRremote,更動了不少東西,但有些地方卻沒變(譬如檔名),以至於起衝突。

此錯誤的詳細情況,大致如此:想使用自行安裝的IRremote,但Arduino IDE卻以內建的RobotIRremote為優先;RobotIRremote的檔案IRremoteTools.cpp需要TKD2的腳位定義,而只有「Arduino Robot Control」板子的腳位定義檔裡才有TKD2這玩意兒,如此一來,就會發生上述的錯誤訊息。

2) 移除 D:\arduino-1.6.5-r2-windows\libraries\RobotIRremote 相關的Robot目錄檔案


IRremote.h 程式

/*
 * IRremote
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.htm http://arcfn.com
 *
 * Interrupt code based on NECIRrcv by Joe Knapp
 * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556
 * Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/
 */

#ifndef IRremote_h
#define IRremote_h

// The following are compile-time library options.
// If you change them, recompile the library.
// If DEBUG is defined, a lot of debugging output will be printed during decoding.
// TEST must be defined for the IRtest unittests to work.  It will make some
// methods virtual, which will be slightly slower, which is why it is optional.
// #define DEBUG
// #define TEST

// Results returned from the decoder
class decode_results {
public:
  int decode_type; // NEC, SONY, RC5, UNKNOWN
  unsigned long value; // Decoded value
  int bits; // Number of bits in decoded value
  volatile unsigned int *rawbuf; // Raw intervals in .5 us ticks
  int rawlen; // Number of records in rawbuf.
};

// Values for decode_type
#define NEC 1
#define SONY 2
#define RC5 3
#define RC6 4
#define DISH 5
#define SHARP 6
#define UNKNOWN -1

// Decoded value for NEC when a repeat code is received
#define REPEAT 0xffffffff

// main class for receiving IR
class IRrecv
{
public:
  IRrecv(int recvpin);
  void blink13(int blinkflag);
  int decode(decode_results *results);
  void enableIRIn();
  void resume();
private:
  // These are called by decode
  int getRClevel(decode_results *results, int *offset, int *used, int t1);
  long decodeNEC(decode_results *results);
  long decodeSony(decode_results *results);
  long decodeRC5(decode_results *results);
  long decodeRC6(decode_results *results);
  long decodeHash(decode_results *results);
  int compare(unsigned int oldval, unsigned int newval);

}
;

// Only used for testing; can remove virtual for shorter code
#ifdef TEST
#define VIRTUAL virtual
#else
#define VIRTUAL
#endif

class IRsend
{
public:
  IRsend() {}
  void sendNEC(unsigned long data, int nbits);
  void sendSony(unsigned long data, int nbits);
  void sendRaw(unsigned int buf[], int len, int hz);
  void sendRC5(unsigned long data, int nbits);
  void sendRC6(unsigned long data, int nbits);
  void sendDISH(unsigned long data, int nbits);
  void sendSharp(unsigned long data, int nbits);
  // private:
  void enableIROut(int khz);
  VIRTUAL void mark(int usec);
  VIRTUAL void space(int usec);
}
;

// Some useful constants

#define USECPERTICK 50  // microseconds per clock interrupt tick
#define RAWBUF 76 // Length of raw duration buffer

// Marks tend to be 100us too long, and spaces 100us too short
// when received due to sensor lag.
#define MARK_EXCESS 100

#endif



// ************************************************************************

#include <IRremote.h>

int RECV_PIN = 2; 

// 使用數位腳位2接收紅外線訊號  Timer2
//預設是 Timer2,發射器要接 pin 3,假如你不想用 pin 3 當作紅外線發射的腳位,
//你有一個選擇,可以換成 Timer1,這樣就可以改用 pin 9 來接紅外線發射器


IRrecv irrecv(RECV_PIN); // 初始化紅外線訊號輸入
decode_results results; // 儲存訊號的結構

void setup()
{
  Serial.begin(9600);
  irrecv.blink13(true); // 設為true的話,當收到訊號時,腳位13的LED便會閃爍
  irrecv.enableIRIn(); // 啟動接收
}

void loop() {
  if (irrecv.decode(&results)) { // 接收紅外線訊號並解碼
    Serial.print("results value is "); // 輸出解碼後的資料
    Serial.print(results.value, HEX);
    Serial.print(", bits is ");
    Serial.print(results.bits);
    Serial.print(", decode_type is ");
    Serial.println(results.decode_type);
    irrecv.resume(); // 準備接收下一個訊號
  }
}



沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...