ShiftPWM and Tone Problem

Hey, i got this ShiftPWM Library: https://github.com/elcojacobs/ShiftPWM/downloads
At the beginning of the code i got this statement: #define SHIFTPWM_USE_TIMER2
If i just uncomment this i get this error message: "The avr you are using does not have a timer3"
The problem is that i also want to play a tone on my piezo buzzer. Therefore i got this code:

 for (int thisNote = 0; thisNote < 8; thisNote++) {
    int noteDuration = 1000/noteDurations[thisNote];
    tone(12, melody[thisNote],noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(12);
    }

Which is working fine in a single project, but combined with the shiftpwm library and its define statment i get the following error message:

core.a(Tone.cpp.o): In function `__vector_7':
/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Tone.cpp:523: multiple definition of `__vector_7'
ShiftPWM_RGB_Example.cpp.o:/Users/david/Documents/Arduino/libraries/ShiftPWM/ShiftPWM.h:190: first defined here

So i went to the Tone.cpp file and i saw that there is also an timer2 statement which (in my opinion) causes this error.

I am new to this arduino stuff and i can make some minor bug fixes or something like that, but this error is too complicated for me at this time.
Do you have an idea how i can solve the puzzle ?

Btw if it helps you these are line 190 following from ShiftPW:

#elif defined(SHIFTPWM_USE_TIMER2)
	//Install the Interrupt Service Routine (ISR) for Timer1 compare and match A.
	ISR(TIMER2_COMPA_vect) {
		ShiftPWM_handleInterrupt();
	}

and line 523 following from Tone.cpp:

ISR(TIMER2_COMPA_vect)
{

  if (timer2_toggle_count != 0)
  {
    // toggle the pin
    *timer2_pin_port ^= timer2_pin_mask;

    if (timer2_toggle_count > 0)
      timer2_toggle_count--;
  }
  else
  {
    // need to call noTone() so that the tone_pins[] entry is reset, so the
    // timer gets initialized next time we call tone().
    // XXX: this assumes timer 2 is always the first one used.
    noTone(tone_pins[0]);
//    disableTimer(2);
//    *timer2_pin_port &= ~(timer2_pin_mask);  // keep pin low after stop
  }
}

The short answer is that both libraries are trying to use the same interrupt (Timer 2, compare match "A").

You can't always just combine libraries and have them work, if they both are trying to use the same piece of the hardware.

I don't really know the ShiftPWM library, but maybe it can be configured to use Timer 1, or something. It can be solved, but not necessarily by changing one or two lines of code.

Hm this is getting complicated... do you know a library which plays tone with timer1 ? ShiftPWM Library says it would use timer1 by default and just because i uncomment the define statement it would use timer 2. But if i uncomment the define statement again he tries to use timer3 and not timer1 by default... so maybe i should contact the developer of this library.

// ShiftPWM uses timer1 by default. To use a different timer, before '#include <ShiftPWM.h>', add #define SHIFTPWM_USE_TIMER2 // for Arduino Uno and earlier (Atmega328)

dehlen:
Hey, i got this ShiftPWM Library: https://github.com/elcojacobs/ShiftPWM/downloads
At the beginning of the code i got this statement: #define SHIFTPWM_USE_TIMER2

Where did you see that, exactly? I just downloaded the library and can't find that line.

Also what are you trying to do? This library seems very complex. Are you trying to fade LEDs and control a servo motor? Or what?

Hi,

Author of the library here. This error is my fault.

The library uses timer 1 by default, but I made an error in a recent update, which I corrected the next day:

You downloaded the library at just the wrong moment.

This error made the compiler complain about not having a timer3, even when it was not used.

So just update to the newest version and you will be fine.

Ah i see... thanks for the quick and easy help :slight_smile: !