Error on IRremote project

Hi everyone! I'm a beginner to Arduino world and I love it but sometimes (quite often) i get a blocked with certain projects, like the one Im trying to do now.

I have an Arduino Mega, and for my IR remote project I'm using the IR library by Ken Shirriff.

So far so good, the problem comes when I try to add an extra function (melody for example).

#include <IRremote.h>

int RECV_PIN = 11;
int buzz = 2;
IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  //melody(); // WHEN I ADD AN EXTRA FUNCTION LIKE THIS ONE I GET AN ERROR

}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
  }
}


/*void melody(){
  tone(buzz, 2500, 150);
  delay(150);
  tone(buzz, 2500, 150);
  delay(150);
  tone(buzz, 2500, 150);
  delay(150);
  tone(buzz, 4186, 500);
  }*/
  
  //ERRROR WHEN I TRY TO ADD AN EXTRA FUNCTION LIKE THIS ONE

And that's when i get this error:

Arduino:1.6.6 (Windows 7), Placa:"Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Users\Admin\AppData\Local\Temp\buildaf6b3c979e963c25610b5838ddac79d0.tmp/core\core.a(Tone.cpp.o): In function `__vector_13':

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Tone.cpp:539: multiple definition of `__vector_13'

libraries\IRremote\IRremote.cpp.o:C:\Program Files (x86)\Arduino\libraries\IRremote/IRremote.cpp:124: first defined here

c:/program files (x86)/arduino/hardware/tools/avr/bin/../lib/gcc/avr/4.8.1/../../../../avr/bin/ld.exe: Disabling relaxation: it will not work with multiple definitions

collect2.exe: error: ld returned 1 exit status

exit status 1
Error de compilación

  Este informe podría tener más información con
  "Mostrar salida detallada durante la compilación"
  activala desde Archivo > Preferencias

Any ideas why this is happening?

Thanks in advance guys!
I would appreciate any help/comments.

Looks like there is incompatibility between IRremote.h and the tone function.

What remote are you using?

You'll find the fix in this thread:-
IRRemote library conflicts with tone() function

In a nutshell, in "IRremoteInt.h", you can define which timer you want 'IRremote' to use.

Thank you YoungSteve!

Here is another option if the OP has NEC remote.
http://forum.arduino.cc/index.php?topic=317625.0

.

LarryD:
Thank you YoungSteve!

Haha, I haven't heard that for a long time. :smiley:

Here is another option if the OP has NEC remote.
New Library for IR Remotes - Exhibition / Gallery - Arduino Forum

Looks like a good library for the NEC remotes. Especially useful for people using Arduinos based on the '328P or similar, since they don't have the extra timers to implement my suggestion. Nice work Larry.

It is written as none blocking code and as you said no timers.
Does use an interrupt.
.

Thank you Steve and Larry for your help.

Unfortunately still not working I've change the timer in that file and no sucess.

This is what I changed in the IRremoteInt.h file. See below:

// 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
//#define IR_USE_TIMER3 // tx = pin 5
//#define IR_USE_TIMER4 // tx = pin 6
#define IR_USE_TIMER5 tx = pin 46

What error are you getting now?

It can't be the same one if IRremote isn't using timer2 any more.
The __vector_13 define relates purely to timer2.

Changing timers seems to work for everyone else that has this error.
Have you closed and re-opened the IDE since editing "IRremoteInt.h"?

As Larry asked, what sort of remote control format? If NEC, you could give his library a spin.

You should change

 #define IR_USE_TIMER5    tx = pin 46

to

   #define IR_USE_TIMER5    //tx = pin 46

It is probably not the issue.

Presumably you are using either an Arduino Mega1280 or Arduino Mega2560??
...vs UNO or Leonardo etc