错误是

D:\Tools\Arduino\libraries\RobotIRremote\src\IRremoteTools.cpp:5:16: error: ‘TKD2’ was not declared in this scope

int RECV_PIN = TKD2; // the pin the IR receiver is connected to

      ^

1
exit status 1
为开发板 Arduino/Genuino Uno 编译时出错。

提示我。TKD2没找到。
然后还一个问题说是找到两个IRremote的库。使用了其中一个库。那个我们可以吧他给的路径的不是arduino ide安装目录的那个库删掉。使用arduino ide里的library提供的IRemote库来写 然而还是有这个错误。

错误是

D:\Tools\Arduino\libraries\RobotIRremote\src\IRremoteTools.cpp:5:16: error: ‘TKD2’ was not declared in this scope

int RECV_PIN = TKD2; // the pin the IR receiver is connected to
^
1
exit status 1
为开发板 Arduino/Genuino Uno 编译时出错。

我使用的是arduino.cc 官方提供的遥控器例子。
https://www.arduino.cc/en/Tutorial/RobotRemoteControl

他这个例子东西太多。很多我没必要的。我只是要他的遥控接收按键编码。就够了。但是提示library里的一个cpp有错误。
看似是arduino ide提供的library里的一个cpp有错误。于是乎。打开看看。
打开错误提示的这个cpp文件。
D:\Tools\Arduino\libraries\RobotIRremote\src\IRremoteTools.cpp

#include "IRremote.h"
#include "IRremoteTools.h"
#include <Arduino.h>

int RECV_PIN = TKD2; // the pin the IR receiver is connected to
IRrecv irrecv(RECV_PIN); // an instance of the IR receiver object
decode_results results; // container for received IR codes

void beginIRremote(){
    irrecv.enableIRIn(); // Start the receiver
}

bool IRrecived(){
    return irrecv.decode(&results);
}

void resumeIRremote(){
    irrecv.resume(); // resume receiver
}

unsigned long getIRresult(){
    return results.value;
}

我们发现他有个 TKD2 这个东西。但是上面没有找到具体的声明或者define 然后我们找他的h头文件。

#ifndef IRREMOTETOOLS_H
#define IRREMOTETOOLS_H

extern void beginIRremote();

extern bool IRrecived();

extern void resumeIRremote();

extern unsigned long getIRresult();

#endif

也没有 TKD2 的声明

然后继续来找 D:\Tools\Arduino\libraries\RobotIRremote\src\IRremoteTools.cpp
里面的引入的几个.h头文件。看看有没有 TKD2 这个东西。
然而。我吧

#include "IRremote.h"
#include "IRremoteTools.h"

这两个都找了。就是没有 TKD2 这个东西。 至于

#include <Arduino.h>

这个就不用去找了。因为这是arduino的环境带的东西。外部的library肯定不会有某个变量或者define放到这里面去。
所以 我们得到的结论是 这个arduinio ide官方提供的这个库代码有问题???? 表骗我。我读书少。但是就我们找出来的原因 就是他官方提供的库文件代码错误。。。。好吧。

我到官方找了反馈问题的地方。先在里面搜索了一下。有人说 1.6.7里面就有这个问题了。
然而。他们用的 要么是不知所以然的东西。要么不是用官方教程写的代码。然后一些留言回复之类的也看的不知所云。
当然这里我要说说一下。我这里得到的这个错误。是按照arduino.cc 官方教程里的代码写的 原版复制粘贴。并检查基本语法错误都没问题。编译就出这个错误

error: 'TKD2' was not declared in this scope

arduino.cc 官方遥控器库使用教程
https://www.arduino.cc/en/Tutorial/RobotRemoteControl

我再次直接复制粘贴。还是有这个错误。

哎。我们就来自己解决吧 也比较简单。

我们有两种方法来解决这个问题。

第一种:

修改报错文件的这个cpp
D:\Tools\Arduino\libraries\RobotIRremote\src\IRremoteTools.cpp

#include "IRremote.h"
#include "IRremoteTools.h"
#include <Arduino.h>

int RECV_PIN = TKD2; // the pin the IR receiver is connected to
IRrecv irrecv(RECV_PIN); // an instance of the IR receiver object
decode_results results; // container for received IR codes

void beginIRremote(){
    irrecv.enableIRIn(); // Start the receiver
}

bool IRrecived(){
    return irrecv.decode(&results);
}

void resumeIRremote(){
    irrecv.resume(); // resume receiver
}

unsigned long getIRresult(){
    return results.value;
}

里面的TKD2 改成11 或者你用来插红外接收头的数据接收端口的IO口 改成像下面这样

#include "IRremote.h"
#include "IRremoteTools.h"
#include <Arduino.h>

int RECV_PIN = 11; // the pin the IR receiver is connected to
IRrecv irrecv(RECV_PIN); // an instance of the IR receiver object
decode_results results; // container for received IR codes

void beginIRremote(){
    irrecv.enableIRIn(); // Start the receiver
}

bool IRrecived(){
    return irrecv.decode(&results);
}

void resumeIRremote(){
    irrecv.resume(); // resume receiver
}

unsigned long getIRresult(){
    return results.value;
}

我这里改成了 11

当然。这样要是需要修改端口的时候。还要修改arduino ide的library的库的cpp文件。这有点扯淡。来上第二种改法。

第二种方法。是用C++的语法来写。

当然。你可以直接copy我的代码就够了。
修改
D:\Tools\Arduino\libraries\RobotIRremote\src\IRremoteTools.h

#ifndef IRREMOTETOOLS_H
#define IRREMOTETOOLS_H

extern void beginIRremote();

extern bool IRrecived();

extern void resumeIRremote();

extern unsigned long getIRresult();

#endif

把这个h文件修改成

#ifndef IRREMOTETOOLS_H
#define IRREMOTETOOLS_H

extern void beginIRremote( int receivePin );

extern bool IRrecived();

extern void resumeIRremote();

extern unsigned long getIRresult();

#endif

就是把 extern void beginIRremote( int receivePin );
这个方法 里添加了个参数
然后我们修改

D:\Tools\Arduino\libraries\RobotIRremote\src\IRremoteTools.cpp

#include "IRremote.h"
#include "IRremoteTools.h"
#include <Arduino.h>

int RECV_PIN = TKD2; // the pin the IR receiver is connected to
IRrecv irrecv(RECV_PIN); // an instance of the IR receiver object
decode_results results; // container for received IR codes

void beginIRremote(){
    irrecv.enableIRIn(); // Start the receiver
}

bool IRrecived(){
    return irrecv.decode(&results);
}

void resumeIRremote(){
    irrecv.resume(); // resume receiver
}

unsigned long getIRresult(){
    return results.value;
}

改成

#include "IRremote.h"
#include "IRremoteTools.h"
#include <Arduino.h>

//int RECV_PIN = TKD2; // the pin the IR receiver is connected to
//IRrecv irrecv(RECV_PIN); // an instance of the IR receiver object
IRrecv *irr;
decode_results results; // container for received IR codes

void beginIRremote( int receivePin ){
    //irrecv.enableIRIn(); // Start the receiver
    irr = new IRrecv( receivePin );
    irr->enableIRIn(); 
}

bool IRrecived(){
    //return irrecv.decode(&results);
    return irr->decode(&results);
}

void resumeIRremote(){
    //irrecv.resume(); // resume receiver
    irr->resume();
}

unsigned long getIRresult(){
    return results.value;
}

好了。修改好之后。我们来看我们的使用的代码

#include <IRremote.h>
#include <IRremoteTools.h>

void setup() {
  Serial.begin(9600);

  beginIRremote(11); // Start the receiver
}

void loop() {
  if (IRrecived()) {
    unsigned long res = getIRresult();
    Serial.println( res );
    resumeIRremote(); // resume receiver
  }
}

然后编译

项目使用了 4,772 字节,占用了 (14%) 程序存储空间。最大为 32,256 字节。
全局变量使用了432字节,(21%)的动态内存,余留1,616字节局部变量。最大为2,048字节。
OK 改好了。

再次给出arduino.cc 官方的遥控器库的使用教程地址
https://www.arduino.cc/en/Tutorial/RobotRemoteControl