Wiki

Clone wiki

Arduino New Ping / Multiple Definition of "__vector_7" Error

Introduction

A common problem with using NewPing is getting a `_ _vector_7' error during compile. What this error means is that you're using two libraries that are both trying to use timer 2. 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. Solutions follow...


If not using the ping_timer() method

There's an easy solution as long as you're not using ping_timer() and running v1.6 or later of NewPing. In the NewPing.h file, TIMER_ENABLED is defined as "true" by default. Setting TIMER_ENABLED to "false" will disable all the timer functions from NewPing and the `_ _vector_7' error will be no more.


Conflicts with the tone library

Also, if the conflict is with the tone library, you can use a different tone library that doesn't use timer 2 and still use the ping_timer() method in NewPing. I've created a few 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.2k 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). Ability to set the sound volume of the tone. Less stress on the speaker so it will last longer and sound better.

TimerFreeTone - Doesn't use timers at all, which frees up conflicts with other libraries. Compatible with all ATmega, ATtiny and ARM-based microcontrollers. Over 1.5k 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.


Other solutions

If, however you are using the ping_timer() method and none of my above tone library replacements are a solution, you still may have options.

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 without using timers. Here's an example 3 sensors 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. The ATmega328 only has a couple timers. Because of this, timer conflicts are very common.

Updated