Compiler error with IRremote library and tone() function

I looked up how to control an arduino with an IR remote and I found this library:

It works, but when I use this library and the tone() function in the same sketch I get an error.
Here is my code:

#include <IRremote.h>

[const int sound = 8;
const int led = 2;
const int sensor = 3;

int RECV_PIN = 10;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup(){
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  pinMode(sensor, INPUT);
  pinMode(sound, OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
}

void loop(){
  while(digitalRead(sensor) == LOW);
  while(true){
    delay(500);
    tone(sound, 262);
    digitalWrite(led, HIGH);
    delay(500);
    //noTone(snd);
    digitalWrite(led, LOW);
  }
}

[

And here is my error:

core.a(Tone.cpp.o): In function `__vector_7':
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Tone.cpp:535: multiple definition of `__vector_7'
IRremote\IRremote.cpp.o:C:\Users\Albert\Documents\Arduino\libraries\IRremote/IRremote.cpp:337: first defined here

When I comment out either the IR library calls or the tone() and noTone() functions, the code compiles fine.

I suspect that the tone() function and the IR libraries are conflicting, but I'm not sure. Any thoughts?

Thanks in advance!

See

http://letsmakerobots.com/content/solved-strange-compilation-error

Its because both libraries use timer 2 (interrupts)

The latest version of the IRLib library has an option which does not use timers. (bitbang mode) *& allows sending from any pin.

In this mode it is blocking while sending, but does free up the various timers.

The Author says it is experimental at this stage.

If I remember correctly, it may also allow choice of timer to use(?)

Thank you everyone! My sketches are now running smoothly! :slight_smile:

It might be agood idea to share what you did to fix it, so that others can benefit/learn!

Here is the modified code in the IRremoteInt.h file:

(Lines 25-68)

// 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

// Teensy 1.0
#elif defined(__AVR_AT90USB162__)
  #define IR_USE_TIMER1     // tx = pin 17

// Teensy 2.0
#elif defined(__AVR_ATmega32U4__)
  //#define IR_USE_TIMER1   // tx = pin 14
  //#define IR_USE_TIMER3   // tx = pin 9
  #define IR_USE_TIMER4_HS  // tx = pin 10

// Teensy++ 1.0 & 2.0
#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
  //#define IR_USE_TIMER1   // tx = pin 25
  #define IR_USE_TIMER2     // tx = pin 1
  //#define IR_USE_TIMER3   // tx = pin 16

// Sanguino
#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
  //#define IR_USE_TIMER1   // tx = pin 13
  #define IR_USE_TIMER2     // tx = pin 14

// Atmega8
#elif defined(__AVR_ATmega8P__) || defined(__AVR_ATmega8__)
  #define IR_USE_TIMER1   // tx = pin 9

// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, etc
#else
  #define IR_USE_TIMER1   // tx = pin 9   uncomment out this line
  //#define IR_USE_TIMER2     // tx = pin 3   comment out this line
#endif

Note that if the servo library is also used in your sketch, the IRlibrary will interfere with it.

See here for more information on arduino timers:

http://letsmakerobots.com/content/arduino-101-timers-and-interrupts