Libraries incompatible: IRemote.cpp vs. NewPing.cpp

I am programming a little robot that's supposed to use a srf-04 ultrasonic distance sensor to navigate. Now I also want to control it manually via a IR Remote Control. I tested Ken Shirriffs IR library and the distance sensor library separately and they worked fine. But as soon as I include both Libs in the programm I get the following error message:

IRremote\IRremote.cpp.o: In function __vector_7': C:\Arduino\arduino-1.0.1\libraries\IRremote/IRremote.cpp:311: multiple definition of __vector_7'
Sonar\NewPing.cpp.o:C:\Arduino\arduino-1.0.1\libraries\Sonar/NewPing.cpp:214: first defined here

I think it is possible that they both want to use the same TIMER or something like that, but I can't figure out how to solve the problem, as I'm very new to Arduino and programming... So could anybody help me to make them compatible?

IR Lib from A Multi-Protocol Infrared Remote Library for the Arduino

Ultra Sonic distance Lib: Google Code Archive - Long-term storage for Google Code Project Hosting.

ISR(TIMER2_COMPA_vect) {    // NewPing.cpp:214
#define TIMER2_COMPA_vect _VECTOR(7)   /* Timer/Counter2 Compare Match A */

Yes it definitely looks like the Timer 2 compare "A" side interrupt vector.

This may or may not be easy to fix. It looks like both libraries use timer 2 for some purpose. Maybe one can be changed to use a different timer, maybe not. You would need a spare one. Looks like both libraries use Timer 2 only. Timer 0 is normally used for millis() and micros(). That would appear to leave Timer 1 free.

I've got exactly the same problem with the new ping library and the Tone library.
did you find anytyhing? any solution?

because when I look into the code of the libraries, I can't find something simular in both libraries

I can:

Tone.cpp:

ISR(TIMER1_COMPA_vect)

Tale the code form one of the two libs and make your own lib from it just as if you where writing a lib of your own (example in playground) then change all vector_7 to something else like Vector_7!

Its a good general point that you need to check that libA is compatible with libB you also need to check that a lib is compatible with any shields you are using, for example the Servo code (from the playground) uses the same pinsas the motor shields.

Mark

Is it possible to use and libraries at the same time in 1 arduino? Changing timers doesn't work.

Did anyone ever resolve this? I ran into the same issue last night, thought I'd ask if anyone worked it out before I started digging into the libraries.

Thanks!

Dave

evansd2:
Did anyone ever resolve this? I ran into the same issue last night, thought I'd ask if anyone worked it out before I started digging into the libraries.

The timer 2 stuff is only in NewPing for the timer interrupt method ping_timer(). If you're just using the standard ping(), ping_in(), ping_cm(), or ping_median() methods, NewPing is not using timer 2. However, the compiler is not smart enough to know that you're not using those interrupts. So you'll get this error if any other library is also using timer 2.

There's an easy solution as long as you're not using ping_timer(). Change the NewPing.cpp program and comment out the section that calls timer 2. Find this section at around lines 210 - 216 in version 1.5:

#if defined (__AVR_ATmega32U4__) // Use Timer4 for ATmega32U4 (Teensy/Leonardo).
ISR(TIMER4_OVF_vect) {
#else
ISR(TIMER2_COMPA_vect) {
#endif
 if(intFunc) intFunc(); // If wrapped function is set, call it.
}

Comment it all out like this:

/*
#if defined (__AVR_ATmega32U4__) // Use Timer4 for ATmega32U4 (Teensy/Leonardo).
ISR(TIMER4_OVF_vect) {
#else
ISR(TIMER2_COMPA_vect) {
#endif
 if(intFunc) intFunc(); // If wrapped function is set, call it.
}
*/

That's it! Also, if the conflict is with the tone library, you can use a different tone library that doesn't use timer 2. I've created a couple tone replacement libraries that not only use timer 1 instead of timer 2 to avoid a conflict, they also have many other advantages:

NewTone - About 1,200 bytes smaller code size than the standard tone library. Faster execution time. Exclusive use of port registers for fastest and smallest code. Higher quality sound output than tone library. Plug-in replacement for Tone. Uses timer 1 which may free up conflicts with the tone library.

toneAC - Nearly twice the volume (because it uses two out of phase pins in push/pull fashion). Higher quality (less clicking). Capability of producing higher frequencies (even if running at a lower clock speed). Nearly 1.5k smaller compiled code. Bug fixes (standard tone library can generate some odd and unpredictable results). Can set not only the frequency but also the sound volume. Less stress on the speaker so it will last longer and sound better.

TimerFreeTone - Doesn't use timers which frees up conflicts with other libraries. Compatible with all ATmega, ATtiny and ARM-based microcontrollers. Over 1,500 bytes smaller binary sketch size than the standard tone library. Exclusive use of port registers for AVR-based microcontrollers for fastest and smallest code. Close to a plug-in replacement for the standard Tone library.

In addition, version 1.6 of the NewPing library (currently being released as beta) includes the TIMER_ENABLED switch that allows you to easily turn off the timer 2 stuff if you're not using the ping_timer() method.

First, do you really need to use the ping_timer() method? Many people incorrectly assume that if they're using multiple sensors they must use the ping_timer() method shown in my 15 sensor example. That's simply not the case. It's best to do it that way for "multitasking" reasons. But, there's no reason why you can't just ping() multiple sensors like in the NewPingExample sketch.

Secondly, maybe you can use a different library that's causing the timer 2 conflict. I've shown two other tone libraries above that use timer 1 instead of timer 2. But, if the timer 2 conflict is with a different library, there's still a chance that you have another option. For example, maybe the timer 2 conflict is with an LED dimmer library. Try to find another LED dimmer library that uses timer 1 instead. While this may not be an option for every library, there are many libraries out there so it's worth looking into.

Tim

I commented the section of the NewPing.cpp file that has timer2 and it worked, the error doesn't show anymore... Thanks a lot...

Hope that won't cause a malfunctioning of my Ultrasonic Sensor [Parallax PING)))]?
I really need it to detect Objects for my robot and I also need the IR sensors to serve as beacons to my robot (the IR sensors are to serve for triangulation).

Thanks for this useful information...

I am using IRremote and Newping. Tim provided a solution by commenting out some code in NewPing.cpp . However the code sample provided is now out of date.
so I do not know what to comment out

My error is as follows

libraries/IRremote/IRremote.cpp.o (symbol from plugin): In function MATCH(int, int)': (.text+0x0): multiple definition of __vector_7'
libraries/NewPing/NewPing.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino/Genuino Uno.

This will modify the IRRemote library to use a different timer.

Not sure how good your knowledge of the Dutch language is :wink: IRremote.h library werkt niet meer? gives a description in Dutch how to approach the problem; I'm sure I have also written a post in English, but could not quickly find it.

[edit]
Ah, here is a description of the approach in English.
[/edit]

In short, find the below in IRremoteInt.h; it will be somewhere around line 220

// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, Nano, etc
// ATmega48, ATmega88, ATmega168, ATmega328
#else
	//#define IR_USE_TIMER1   // tx = pin 9
	#define IR_USE_TIMER2     // tx = pin 3

#endif

Change it to

// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, Nano, etc
// ATmega48, ATmega88, ATmega168, ATmega328
#else
	#define IR_USE_TIMER1     // tx = pin 9
	//#define IR_USE_TIMER2   // tx = pin 3

#endif

Add a comment with a motivation as to why you changed it.

Note:
This change will affect any code that you wrote in the past that uses IRremote; if you did not have an interrupt conflict in the past, you might now have one in the old code if it uses timer1 (e.g. in another library). Same for code that you write in the future where you use timer1.

That's why it's important to document the change.

The alternative is to copy the IRremote library to your sketch; either copy it directly to the sketch directory or create a src directory and copy it there and modify it. You will also need to change the 'includes'. The latter should work with newer IDEs (it did not work with 1.6.6 when I tried). The advantage is that the modification is local to your project and does not affect previous code or future code.

Hello guys. i have NewPing_v1.9.0бversion 1.5 no longer download. And also does not work together with the library Remote.h
This error code :

libraries\IRremote\IRremote.cpp.o (symbol from plugin): In function `MATCH(int, int)':

(.text+0x0): multiple definition of `__vector_7'

libraries\NewPing\NewPing.cpp.o (symbol from plugin):(.text+0x0): first defined here

collect2.exe: error: ld returned 1 exit status

exit status 1
Ошибка компиляции для платы Arduino/Genuino Uno.

in this library version there are these lines partially.
I tried to comment these lines but apparently there is an extra code, the result is an error.

Could you help the newbie, what would remove this error in the version 1.9.0....?

#if defined (AVR_ATmega32U4) // Use Timer4 for ATmega32U4 (Teensy/Leonardo).
ISR(TIMER4_OVF_vect) {
#else
ISR(TIMER2_COMPA_vect) {
#endif
if(intFunc) intFunc(); // If wrapped function is set, call it.
}

Thank you very much for attention.
Evgeniy

Please explain the below

Hello guys. i have NewPing_v1.9.0бversion 1.5 no longer download

You have newping version 1.9.0?
Version 1.5 is the version of your IDE or ...?
No longer download means? That you can't upload code to the Arduino or that you can no longer download something from the web (if so, what?)? The error that you get has nothing to do with download; it's a linker error so I'm confused.

Which Arduino are you using?

NewPing version 1.9.0 contains

	#if defined (__AVR_ATmega32U4__) // Use Timer4 for ATmega32U4 (Teensy/Leonardo).
	ISR(TIMER4_OVF_vect) {
		intFunc(); // Call wrapped function.
	}
	#elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__) || defined (__AVR_ATmega8535__) // Alternate timer commands for certain microcontrollers.
	ISR(TIMER2_COMP_vect) {
		intFunc(); // Call wrapped function.
	}
	#elif defined (__arm__)
		// Do nothing...
	#else
	ISR(TIMER2_COMPA_vect) {
		intFunc(); // Call wrapped function.
	}
	#endif

It's the closest that I could find relating to what you posted.

Have you considered following the advice in reply #11? It's probably the simplest solution unless you need timer1 for something else in which case you probably have another problem.

Arduino ID 1.8.5
New Ping Version 1.9.0

I Can`t compile the sketch . This error showing when i try compie the sketch

p.s. What name of file is spoken in answer 11? irremote.cpp or IRremote.h ? there are no these lines in it . If it is not difficult, could you send working libraries to me for mail? nadaskevich@gmail.com

Reply #11 mentioned IRremoteInt.h.

Please pay attention to the note at the end of reply #11.

May be I'm using a wrong library, there are only 110 lines in my library. In irremotelnt file.
In this file, there are no lines 220 and lines that are described in answer 11.
I downloaded my library from different places, specifically from here.

Could you send me a link to the correct library or send to the mail ?

That is basically the correct library. However it seems to have changed (and unfortunately the author seems to have forgotten to update some comments about the version in several files).

I downloaded the latest version using the library manager in the IDE and indeed found that IRremoteInt.h is no longer that big. Reading though the new IRremoteInt.h, it contains below lines (lines 110 and 111)

// All board specific stuff has been moved to its own file, included here.
#include "boarddefs.h"

If yours does as well, you need to look in boarddefs.h

There you will find (lines 194 and 195)

	//#define IR_USE_TIMER1   // tx = pin 9
	#define IR_USE_TIMER2     // tx = pin 3

I haven't tested, leave that up to you.

Thank you very much !!! =) All work normaly

I checked, this library takes pin 10 on my UNO, and I can not use this PWM channel.

I checked boarddefs.h and found next lines:

#elif defined(AVR_ATmega32U4)
//#define IR_USE_TIMER1 // tx = pin 14
//#define IR_USE_TIMER3 // tx = pin 9
#define IR_USE_TIMER4_HS // tx = pin 10

I did this:

#elif defined(AVR_ATmega32U4)
//#define IR_USE_TIMER1 // tx = pin 14
//#define IR_USE_TIMER3 // tx = pin 9
//define IR_USE_TIMER4_HS // tx = pin 10
but it did not help.

if I just remove the library from the sketch, then the pin 10 works normaly.
I checked all the library files and did not find any more "pin 10" of "IR_USE_TIMER4_HS"

Where can I free my 10 pin? Please help =((
p.s. i have no any free PWM pins on my UNO