Compilation Error

UKHeliBob:
It looks like the NewPing and IRremote libraries are using the same interrupt. I don't know what to suggest to fix the problem.

Analyse the libraries :smiley:

Boothy920Q:
I get an error message which I don't understand and have gone through many hours trying to fix

libraries\IR\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

exit status 1
Error compiling for board Arduino/Genuino Uno.

As UKHeliBob indicated, you have an interrupt conflict (and it's easy to solve if you know how).

The below is for a 328P based micro (Uno, Nano, ...). First get the datasheet of the processor to find out which interrupt relates to __vector_7. There is a table in the datasheet

1 0x0000(1) RESET External Pin, Power-on Reset, Brown-out Reset and Watchdog System Reset
2 0x0002 INT0 External Interrupt Request 0
3 0x0004 INT1 External Interrupt Request 1
4 0x0006 PCINT0 Pin Change Interrupt Request 0
5 0x0008 PCINT1 Pin Change Interrupt Request 1
6 0x000A PCINT2 Pin Change Interrupt Request 2
7 0x000C WDT Watchdog Time-out Interrupt
[color=red]8 0x000E TIMER2 COMPA Timer/Counter2 Compare Match A[/color]
9 0x0010 TIMER2 COMPB Timer/Counter2 Compare Match B

The vectors start counting from 0, the table starts at 1 (so you have to subtract 1 from the number in the first column).

Ah, so this is timer related and the problem is with timer 2; more speific TIMER2_COMPA. Now you know that, you can start digging through the two conflicting libraries and find where. I started with the NewPing library but I did not see a quick way to change the timer that is used (compared to the IRremote library that I'm slightly more familiar with) so continued with the IRremote library).

The next thing to do is to look for TIMER2_COMPA (all capital) in all library files of the IRremote library. You will find it in IRremoteInt.h (line 242 in my version of the library).

//---------------------------------------------------------
// Timer2 (8 bits)
//
#if defined(IR_USE_TIMER2)

#define TIMER_RESET
#define TIMER_ENABLE_PWM    (TCCR2A |= _BV(COM2B1))
#define TIMER_DISABLE_PWM   (TCCR2A &= ~(_BV(COM2B1)))
#define TIMER_ENABLE_INTR   (TIMSK2 = _BV(OCIE2A))
#define TIMER_DISABLE_INTR  (TIMSK2 = 0)
#define TIMER_INTR_NAME     TIMER2_COMPA_vect

If you dig through IRremote.cpp, you will find that TIMER_INTR_NAME that is used for the ISR so you now know that your on the right track.

Now note the line #if defined(IR_USE_TIMER2). Somewhere IR_USE_TIMER2 must be defined so the next step is to dig through the library files to find it. You will find it the first time in IRremoteInt.h on line 158 (for the version of the library that I use).

//------------------------------------------------------------------------------
// Define which timer to use
//
// Uncomment the timer you wish to use on your board.
// If you are using another library which uses timer2, you have options to
//   switch IRremote to use a different timer.
//

// Arduino Mega
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
	//#define IR_USE_TIMER1   // tx = pin 11
	#define IR_USE_TIMER2     // tx = pin 9          <<----------- line 158
	//#define IR_USE_TIMER3   // tx = pin 5
	//#define IR_USE_TIMER4   // tx = pin 6
	//#define IR_USE_TIMER5   // tx = pin 46

The complete section that contains this line starts at line 147. Note the comment on the first few lines; this is where you can define which timer to use. Now you must follow that block to find what applies to your microprocessor.

Mega, no; Teensy 1.0, no etc etc. For the 328P processor, you will end at this (line 221)

// 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 the two lines after the else to use timer 1 instead of timer 2

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

and save the file.

I advise that you add a comment to that section so you know what changed and why.

Next compile your code and the error should be gone.

Notes:
1a)
No guarantee that your code will work after that; I think it will. Please test and provide feedback.
1b)
I'm not sure of the comment * // tx = pin 9*. I think it's only used if you use the library for an IR transmitter. I leave it up to you to test.
2)
If you have existing code in other projects that use the IRremote library and you recompile that code, it will also use timer 1; this might conflict with other things in that project.
3)

Sorry for the long post; it might look a little complicated but I considered it important that you understand the procedure that can be followed to solve the problem.