Hi
5 years ago, a user here called jmknapp, created a Library called "NECIRrcv",
that enables Receiving and Sending IR Codes.
Despite the fact that 5 years have passed, this Library still works well.
(when running it, I just needed to replace "include WProgram.h" with "include Arduino.h", and all worked well).
Specifically, this code works:
#include "NECIRrcv.h"
#define IRPIN 4 // pin that IR detector is connected to
NECIRrcv ir(IRPIN);
void setup()
{
Serial.begin(9600);
Serial.println("NEC IR code reception");
ir.begin();
}
unsigned long ircode;
void loop()
{
if(ir.available())
{
ircode =ir.read();
Serial.println(ircode,HEX);
}
}
The library can be downloaded here:
http://forum.arduino.cc/index.php/topic,8494.0.html
Now I have a small problem in C++, which I cannot understand what's causing it.
I tried to do a very simply thing:
To create a simple class, that will be called "IR_Receiver",
which will contain an object of NECIRrcv.
The reason I want to create this class, is in order to add some more methods of mine.
But now when I run it, the program compiles and runs, but when I press the IR Remote, nothing appears on the screen..
As if the program is not receiving any codes.
Why is that?
Here is my simple class:
class IR_Receiver
{
public:
NECIRrcv* m_ir;
IR_Receiver(int SetLeg)
{
NECIRrcv ir2(SetLeg);
m_ir =&ir2;
m_ir->begin();
}
//Methods:
unsigned long ircode;
unsigned long ReceiveIfAvailable()
{
if(m_ir->available())
{
ircode =m_ir->read();
return ircode;
}
else
{
return 0; //No Code Available
}
}
};
void setup()
{
Serial.begin(9600);
Serial.println("Up");
}
IR_Receiver O_IR_Receiver(4);
unsigned long IR_Code;
void loop()
{
IR_Code O_IR_Receiver.ReceiveIfAvailable();
if(IR_Code!=0)
{
Serial.println(IR_Code,HEX);
}
}
I hope someone can see what's the problem..
Thank you