如何解决Tone.cpp与IRremote.cpp冲突问题

2014-07-052万
AI 快速预览详细 收起
本文讨论了在Arduino项目中使用Tone库和IRremote库时遇到的冲突问题。具体报错信息为`__vector_7`的多重定义。文章提供了详细的排查步骤和解决方案,包括修改库文件中的中断向量名。通过这些方法,开发者可以快速解决冲突问题,确保项目顺利运行。

在震动报警器示例代码中做了一下扩展,增加一个RGB LED,使报警的时候有红绿蓝三色进行闪烁。震动停止10秒钟之后,报警停止。
上面这些功能都实现了,除了使用倾斜检查来实现震动检测准确率不是特别高,其他的基本上正常。
最后想加入红外遥控器进行控制,但是导入IRremote.h后编译器检查代码时报错,看错误信息是有冲突,于是找到发生冲突的文件,看到冲突确实存在。
发生冲突的两个文件分别是“Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Tone.cpp”和“Arduino.app/Contents/Resources/Java/libraries/IRremote/IRremote.cpp”。
完整的报错信息如下:
core.a(Tone.cpp.o): In function `__vector_7':
/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Tone.cpp:535: multiple definition of `__vector_7'
IRremote/IRremote.cpp.o:/Applications/Arduino.app/Contents/Resources/Java/libraries/IRremote/IRremote.cpp:311: first defined here

完整的代码如下:
  1. #include <IRremote.h>
  2. const float radianValue = 3.14 / 180;//conver angle to radian
  3. const int rLedPin = 8;
  4. const int gLedPin = 9;
  5. const int bLedPin = 10;
  6. const int alarmPin = 7;
  7. boolean isAlarmWork = false;
  8. const int detectionVabrationPin = 3;
  9. int detectionVabrationCount = 0;
  10. unsigned long detectionVabrationTime = 0;
  11. unsigned const long alarmTimeOut = 1000 * 10;
  12. const int infraredPin = 12;
  13. IRrecv irrecv(infraredPin);
  14. decode_results results;
  15. void setup(){
  16.   pinMode(rLedPin, OUTPUT);
  17.   pinMode(gLedPin, OUTPUT);
  18.   pinMode(bLedPin, OUTPUT);
  19.   pinMode(alarmPin, OUTPUT);
  20.   pinMode(detectionVabrationPin, INPUT);
  21.   attachInterrupt(0, detectionVabration, RISING);
  22.   
  23.   Serial.begin(9600);
  24. }
  25. void loop(){
  26.   alarmDoWork();
  27.   if(isAlarmWork){
  28.     if(millis() - detectionVabrationTime > alarmTimeOut){
  29.       isAlarmWork = false;
  30.       Serial.println("end");
  31.     }
  32.     Serial.println(detectionVabrationCount);
  33.   }
  34. }
  35. void detectionVabration(){
  36.   isAlarmWork = true;
  37.   detectionVabrationCount ++;
  38.   detectionVabrationTime = millis();
  39. }
  40. void alarmDoWork(){
  41.   int ledIndex = 1;
  42.   for(int i=0; i<180; i++){
  43.     if(!isAlarmWork){
  44.       noTone(alarmPin);
  45.       resetAllLed();
  46.       break;
  47.     }
  48.     tone(alarmPin, int(2000 * sin(i * radianValue)));
  49.     if(i % 10 == 0){
  50.       if(ledIndex == 1){
  51.         onlyShowRLed();
  52.         ledIndex = 2;
  53.       }
  54.       else if(ledIndex == 2){
  55.         onlyShowGLed();
  56.         ledIndex = 3;
  57.       }
  58.       else if(ledIndex == 3){
  59.         onlyShowBLed();
  60.         ledIndex = 1;
  61.       }
  62.     }
  63.     delay(10);
  64.   }
  65. }
  66. void onlyShowRLed(){
  67.   digitalWrite(rLedPin, HIGH);
  68.   digitalWrite(gLedPin, LOW);
  69.   digitalWrite(bLedPin, LOW);
  70. }
  71. void onlyShowGLed(){
  72.   digitalWrite(rLedPin, LOW);
  73.   digitalWrite(gLedPin, HIGH);
  74.   digitalWrite(bLedPin, LOW);
  75. }
  76. void onlyShowBLed(){
  77.   digitalWrite(rLedPin, LOW);
  78.   digitalWrite(gLedPin, LOW);
  79.   digitalWrite(bLedPin, HIGH);
  80. }
  81. void resetAllLed(){
  82.   digitalWrite(rLedPin, LOW);
  83.   digitalWrite(gLedPin, LOW);
  84.   digitalWrite(bLedPin, LOW);
  85. }
复制代码

这种问题大家开发过程中遇到过没,有什么好的解决方法吗?

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(4)
無数
無数
我把我的实验经过写在博客里了。
https://blog.csdn.net/GreatSimulation/article/details/108982522
33532997052017
33532997052017
可以正常编译,但是我使用完tone之后,红外遥控就不控制了怎么办?不再接受码值了。。。
Youyou
Youyou
你好,类似于楼上说的,两个库用的是同一个定时器,造成定时器冲突,两个库里面都写了同一个定时器的中断程序,这样就出现了同一个定时器却有两个中断程序,导致编译器不能正常编译。可以修改IR的库,换个定时器使用,在IRremoteint_h里面,找到这些语句:
// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, etc
#else
#define IR_USE_TIMER1 // tx = pin 9
//#define IR_USE_TIMER2 // tx = pin 3
#endif
这里我已经修改了,注释掉了#define IR_USE_TIMER2 ,使用了#define IR_USE_TIMER1,因此不会出现定时器冲突了。
重启IDE后即可正常编译。
wangyi2926
wangyi2926
我有一次用红外遥控接D3的LED,也遇到过类似的事情。经求助得知,红外的库文件里计时器占用了D3和D11针脚,你试着改一下LED的接线,或许有用。
- 没有更多了 -