IR receiver not giving output on Serial monitor

Hello, this is my first post in the forum and I am just starting out with arduino. I am following the Arduino Mega 2560 Tutorials pdf that was delivered with the Box.

Right now I want to do get the IR receiver to work but its not showing any output on the Serial monitor.
I am using an Arduino Mega 2560 R3 and the attached receiver and remote.

The basic sketch has the following code:

#include "IRremote.h"
#include "IR.h"

IRrecv irrecv(RECEIVER); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'

void setup() {
Serial.begin(9600);
Serial.println("IR Receiver Button Decode");
irrecv.enableIRIn();
}

void loop()
{
int tmpValue;
if (irrecv.decode(&results)) // have we received an IR signal?
{
Serial.println(results.value);
}
irrecv.resume(); // receive the next value
}

The headers are loaded and working. I triple checked the wiring, but as there are only 3 pins there is not that much that can go wrong.
What is happening now is that when I point the remote towards the receiver and press any button the LED lights up as expected. If I keep the button pressed the LED keeps lighting up in a high frequency. But nothing happens on the serial monitor. If I add the following else statement behind the if statement above:

else{
Serial.print("Hello")
}
it keeps spamming hello, so I conclude that even though the LED lights whenever I press a button the the receiver still says its not getting a signal.

Can anyone help me here?

receiver.JPG

remote.JPG

receiver.JPG

remote.JPG

Your code will not compile so I fail to see how you got any results.

I fixed the code and tested it on my Mega, IR detector decoder and remote. Fixed code works fine. See the comments. Don't call irrecv.resume() unless there has been a code received.

#include "IRremote.h"
//#include "IR.h"  //  causes an error "not found" but you do not need this so bye bye. ********

const byte RECEIVER = 2;  // RECEIVE not declared so added this *************************

IRrecv irrecv(RECEIVER);     // create instance of 'irrecv'
decode_results results;      // create instance of 'decode_results'


void setup() {
 Serial.begin(9600);
 Serial.println("IR Receiver Button Decode");
 irrecv.enableIRIn();
}


void loop()
{
 //int tmpValue;  // not used
 if (irrecv.decode(&results)) // have we received an IR signal?
 {
   Serial.println(results.value);
   irrecv.resume(); // moved ****************************************
 }
   //irrecv.resume(); // receive the next value ** don't  call in loop, call after key has been decoded. **
 }

I guess I failed to mention that there is an IR.h header, with that it compiles fine. But I also tried out your code and I still have the same issue, the receiver blinks when I press a button but nothing appears on the serial monitor

I tried it again to do, after resetting my arduino and it worked! I only tried it with your code though but that suffices. Thank you for taking the time to read my problem and solve it :slight_smile:

Edit:

I played around with it a little more and I think it was due to the thing you commented in the code :

Serial.println(results.value);
irrecv.resume(); // moved ****************************************
}
//irrecv.resume(); // receive the next value ** don't call in loop, call after key has been decoded.

because as soon as I put the resume outside the if statement it doesnt work anymore. Good to know!