Using the IRremote library on Mega on pin 2?

Hello!

I tried the IRremote library (arcfn.com/2009/08/multi-protocol-infrared-remote-library.html) on my Duemilanove and it worked fine.

However, I really need to use it on a Mega on pin 2 (which has PWM too afaik). According to the comments, it works on the Mega on pin 9 (after changing the "3" to "9" in the library, because something about the ports or PWM flags (?) is different), but could you think of any way to use it on pin 2?

Best regards, Cherry

I think if you want to use the IR library to send IR, you are out of luck (unless you basically rewrite the library to use another timer than timer2, if possible, corresponding to another PWM pin). But for receiving IR, it should just be a matter of changing to whatever pin you want? At least I'm pretty sure I just tried it on pin 4 (for receiving), and that worked.

Oh, I forgot to point out that sending is the problem (receiving on port 41 works well).

Well, that's bad. Anyway, thank you.

Best regards, Cherry

Looking into the implementation of the IRremote lib, it only supports to send the IR through the PWM pin.

I have an idea to write another simple lib to meet your requirement. The code below shows how to send the data through IR. The IR_S defined in the code can be any pin. Change the delay in function IR_Send38KHZ can change the IR frequency.

int IR_S = 8;

void IR_Send38KHZ(int x,int bit) //Generate 38KHZ IR pulse
{
for(int i=0;i<x;i++)//15=386US
{
if(bit==1)
{
digitalWrite(IR_S,1);
delayMicroseconds(9);
digitalWrite(IR_S,0);
delayMicroseconds(9);
}
else
{
digitalWrite(IR_S,0);
delayMicroseconds(20);
}
}
}

void IR_Sendcode(uint8_t data) // Send the data
{
for(int i=0;i<8;i++)
{
if((data&0x01)==0x01)
{
IR_Send38KHZ(23,1);
IR_Send38KHZ(64,0);
}
else
{
IR_Send38KHZ(23,1);
IR_Send38KHZ(21,0);
}
data=data>>1;
}
}

Hope it helps.

Best regards,
Cherry