IR remote error

Hi, I modified IR remote tutorial to make it turn on LEDs and make sounds with buzzer. But it can’t compile my code… I don’t see where is the problem:

 #include <IRremote.h>
const int irReceiverPin = 7;
int led1=2;
int led2=3;
int led3=4;
int led4=5;
int buzzer=6;
IRrecv irrecv(irReceiverPin);
decode_results results;

void setup(){
Serial.begin(9600);
irrecv.enableIRIn();
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(buzzer, OUTPUT);
}

void loop(){
if (irrecv.decode(&results))
{
  switch(results.value){
  case 16753245:{
    digitalWrite(led1,HIGH);
    tone(buzzer, 200, 1000);
    digitalWrite(led1,LOW);
    break;
  }
  case 16736925:{
   digitalWrite(led2,HIGH);
    tone(buzzer, 300, 1000);
    digitalWrite(led2,LOW);
    break;
  }
  case 16769565:{
    digitalWrite(led3,HIGH);
    tone(buzzer, 400, 1000);
    digitalWrite(led3,LOW);
    break;
  }
  case 16720605:{
    digitalWrite(led4,HIGH);
    tone(buzzer, 500, 1000);
    digitalWrite(led4,LOW);
    break;
  }
  default:
        Serial.println("Waiting");
  }
Serial.print("IR_Code: ");
Serial.print(results.value, DEC);
Serial.print(", Bits: ");
Serial.println(results.bits);
irrecv.resume();
}
delay(600);
}

Error message: exit status 1
Error compiling for board Arduino/Genuino Uno.

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn’t show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you’re using the Arduino Web Editor you will not have access to this useful tool but it’s still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into logical sections is fine but large spaces for no reason or random blank lines just make for more scrolling when we’re trying to read your code. Do not post double spaced code.

When you encounter an error you’ll see a button on the right side of the orange bar “Copy error messages”. Click that button. Paste the error in a message here USING CODE TAGS (</> button on the toolbar).

Just look up how a switch case statement is really supposed to be. Compare with yours.

You did not include enough of the error messages to help us figure this out. Did you install the IRremote library using library manager? If not, the compile will fail because IRremote.h does not exist. And there is a missing curly brace at the end.

aarg:
Just look up how a switch case statement is really supposed to be. Compare with yours.

There is nothing wrong with the switch statement, or the cases. There are unnecessary curly braces in the cases, but there are times when one wants to declare a variable only in a case, and the compiler doesn’t like that. Adding curly braces defines a block, and local variables are allowed in a block, even one in a case statement.

What IS wrong with the code is that there is a missing } at the end.

OP: You start fixing errors at the top, NOT at the bottom. There were errors listed before the one you posted. Scroll to the top in the output window, and start fixing errors from the top.

Sorry, the last bracket was there, i just didn't select it when i was pasting. It does compile when i comment out switch portion, and it reads remote's values. The library is included.

But it doesn't want to compile with the switch case. Funny though, if I type "switch(results.value, DEC)" it compiles, but then when i press the buttons it reads the value, but no LED's light up and no sound is made. So idk...

the full error message goes like this:

 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.

Ok, I narrowed it down. When I comment out anything related to buzzer it works - it reads the buttons and lights up LED's. Something to do with pin 7?

antonkuzubov: Ok, I narrowed it down. When I comment out anything related to buzzer it works - it reads the buttons and lights up LED's. Something to do with pin 7?

Do the IR library examples sketches work when configured to use that pin?

Couldn't find no _vector 7 in IRremote.cpp like the error message stated... Couldn't find no tone library to compare to as well, i think it's built into the Arduino IDE or something

Try using a different pin.

Tried. Same stuff. Looks like those 2 libraries (IRremote and Tone) conflict each other somehow. But as I said, I couldn't find __vector_7 in IRremote, and idk where that Tone library is

Tone is part of the Arduino core library. Here is the Arduino AVR Boards version used by your Uno: https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/Tone.cpp

Thanks! I still don't see how I can resolve this issue(

You have an interrupt conflict. More than likely timer interrupt (no datasheet at hand). Search the two libraries for the keyword ISR and take it from there.

Determine which timer is used in IRremote. See if you can use a pin for the tone that does not use the same timer.

Alternatively modify one of the libraries to use a different timer. Be aware that that will affect all your old code and future code.

Thanks! I figured something of that matter. But i have no clue to do with that ISR, and have no idea what pin it uses lol. Still to complicated for me. Here’s the code from IRremote.cpp, where ISR is mentioned:

ISR(TIMER_INTR_NAME)
{
  TIMER_RESET;

  uint8_t irdata = (uint8_t)digitalRead(irparams.recvpin);

  irparams.timer++; // One more 50us tick
  if (irparams.rawlen >= RAWBUF) {
    // Buffer overflow
    irparams.rcvstate = STATE_STOP;
  }
  switch(irparams.rcvstate) {
  case STATE_IDLE: // In the middle of a gap
    if (irdata == MARK) {
      if (irparams.timer < GAP_TICKS) {
        // Not big enough to be a gap.
        irparams.timer = 0;
      } 
      else {
        // gap just ended, record duration and start recording transmission
        irparams.rawlen = 0;
        irparams.rawbuf[irparams.rawlen++] = irparams.timer;
        irparams.timer = 0;
        irparams.rcvstate = STATE_MARK;
      }
    }
    break;
  case STATE_MARK: // timing MARK
    if (irdata == SPACE) {   // MARK ended, record time
      irparams.rawbuf[irparams.rawlen++] = irparams.timer;
      irparams.timer = 0;
      irparams.rcvstate = STATE_SPACE;
    }
    break;
  case STATE_SPACE: // timing SPACE
    if (irdata == MARK) { // SPACE just ended, record it
      irparams.rawbuf[irparams.rawlen++] = irparams.timer;
      irparams.timer = 0;
      irparams.rcvstate = STATE_MARK;
    } 
    else { // SPACE
      if (irparams.timer > GAP_TICKS) {
        // big SPACE, indicates gap between codes
        // Mark current code as ready for processing
        // Switch to STOP
        // Don't reset timer; keep counting space width
        irparams.rcvstate = STATE_STOP;
      } 
    }
    break;
  case STATE_STOP: // waiting, measuring gap
    if (irdata == MARK) { // reset gap timer
      irparams.timer = 0;
    }
    break;
  }

  if (irparams.blinkflag) {
    if (irdata == MARK) {
      BLINKLED_ON();  // turn pin 13 LED on
    } 
    else {
      BLINKLED_OFF();  // turn pin 13 LED off
    }
  }
}

How do I know what timer is used for what pin?

short version long version

Yes, it worked! Thank you very much! I’m still not sure what exactly did it do, and why, but it works now)

antonkuzubov: Yes, it worked! Thank you very much! I'm still not sure what exactly did it do, and why, but it works now)

An 328 based Arduino has 3 timers each having a few functionalities. In this case the Tone and IRremote libraries use the same functionality (COMP_A) of the same timer (timer2). The modification changes the timer that is used by the IRremote library to timer1.

Now it is very well possible that in future you want to use another library (e.g. NewPing; I did not look into it) in combination with IRremote.h and Tone.h and this other library uses timer1 or timer2 as well. That library might offer the option to use the same functionality but from timer0 (not sure if it's possible). If not, you can not solve the problem because Tone uses timer2 and IRremote uses timer1 (after the modification) and no other timers are available for the other library.

After all, there is a limit to what can be done with three timers. The solution [u]can[/u] be to use a different micro with more timers; you will have to analyse all involved libraries to see which micro is suitable. Alternative [u]can[/u] be to use multiple micros, split the functionalities and let them communicate with each other.