4

I have just started with Arduino and C++ so I apologise if my question is very basic. I am trying to use tone() with a passive buzzer to create a sound while also using the IRremote library for a remote. However, whenever they are used together, it gives this error:

Tone.cpp.o (symbol from plugin): In function `timer0_pin_port':

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

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

collect2.exe: error: ld returned 1 exit status

exit status 1

Error compiling for board Arduino/Genuino Uno.

This is the code required to recreate this error:

#include "IRremote.h"
#include "pitches.h"
int melody[] = {NOTE_C5, NOTE_E6};

void setup() {}

void loop() {
 for (int thisNote = 0; thisNote < 2; thisNote++) {
 tone(3, melody[thisNote], 300);
}
}

Please could you advise me on how to fix this issue and if necessary, on a different library or way to create a sound or to receive IR signals?

rgiller
  • 43
  • 1
  • 6
  • 1
    I think your problem is related to https://forum.arduino.cc/index.php?topic=165675.0, which is about two libraries using the same timer. – Michel Keijzers Jul 19 '19 at 09:01
  • I'm looking through Tone.cpp and i think it's possible to force it to use another timer by defining USE_TIMER1 i will verify this asap. – Filip Franik Jul 19 '19 at 09:15
  • @FilipFranik, thanks. Where do you find tone.cpp? I downloaded Arduino IDE from the Windows Store. – rgiller Jul 19 '19 at 09:17
  • @MichelKeijzers The forum you linked doesn't seem to explain how to fix the issue I am facing although I do think it is the same problem. – rgiller Jul 19 '19 at 09:35
  • https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/Tone.cpp – Filip Franik Jul 19 '19 at 09:36
  • @rgiller I know, that's why I just post it as a comment instead of an answer. – Michel Keijzers Jul 19 '19 at 09:44
  • @FilipFranik Do you mean I should change `#define USE_TIMER2` to a different number which is different to the IR remote? – rgiller Jul 19 '19 at 09:46

1 Answers1

6

Exactly as @MichelKeijzers wrote the issue is caused by "IRremote" using same timer as "Tone" and the solution to this problem is a little dirty.

Since Tone is included in ArduinoCore (and compiled) we can't easilly modify it so the only thing that worked for me is to modify boarddefs.h file of IRremote library. Since it's code is easilly available after downloading it using "Library Manager" included in ArduinoIDE.

In my tests I used ATmega328P settings.

arduino board setting

Edit C:\Users\youruser\Documents\Arduino\libraries\IRremote\boarddefs.h and look for your processor number.

For ATmega328 the definition is #define IR_USE_TIMER2

boarddefs.h code

I simply changed it to use #define IR_USE_TIMER1

boarddefs.h updated

And compilation was successfull.

Sketch uses 2746 bytes (8%) of program storage space. Maximum is 30720 bytes.
Global variables use 242 bytes (11%) of dynamic memory, leaving 1806 bytes for local variables. Maximum is 2048 bytes.

My code:

#include "IRremote.h"

#define NOTE_C5  523
#define NOTE_E6  1319

int melody[] = {NOTE_C5, NOTE_E6};

void setup() {}

void loop() {
  for (int thisNote = 0; thisNote < 2; thisNote++) {
    tone(3, melody[thisNote], 300);
  }
}
Filip Franik
  • 1,272
  • 1
  • 7
  • 20