Here is a simple sketch that I wrote to get the key code and address of the buttons on an IR remote. Also outputs the protocol. This uses the new version of the IRremote library. Note that the receive pin is set to pin 4 so change it if you need. And the baud rate is 115200. This code has been tested and proven to work on my Uno with a TSOP1838 type decoder.
#include <IRremote.hpp>
const byte IR_RECEIVE_PIN = 4;
unsigned long keycommand = 0;
unsigned long keyaddress = 0;
decode_type_t remoteprotocol;
bool newIR = false;
const char *thisProtocol[] = {"UNKNOWN", "PULSE_DISTANCE","PULSE_WIDTH","DENON","DISH","JVC","LG","LG2","NEC","PANASONIC",
"KASEIKYO","KASEIKYO_JVC","KASEIKYO_DENON","KASEIKYO_SHARP","KASEIKYO_MITSUBISHI","RC5","RC6",
"SAMSUNG","SHARP","SONY","ONKYO","APPLE","BOSEWAVE","LEGO_PF","MAGIQUEST","WHYNTER"};
void setup()
{
Serial.begin(115200);
Serial.println("IR receive test");
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
}
void loop()
{
checkIR();
if (newIR)
{
printIR();
newIR = false;
}
}
void checkIR()
{
if (IrReceiver.decode())
{
keycommand = IrReceiver.decodedIRData.command;
keyaddress = IrReceiver.decodedIRData.address;
remoteprotocol = IrReceiver.decodedIRData.protocol;
if ((IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT))
{
IrReceiver.resume();
return;
}
IrReceiver.resume();
newIR = true;
}
}
void printIR()
{
Serial.print("decoded command = ");
Serial.print(keycommand, HEX);
Serial.print(" decoded address = ");
Serial.print(keyaddress, HEX);
Serial.print(" decoded protocol = ");
Serial.println(thisProtocol[remoteprotocol]);
Serial.println();
}
Output for the up and down arrows for 2 different remotes:
IR receive test
decoded command = 39 decoded address = 1C5A decoded protocol = SONY
decoded command = 3A decoded address = 1C5A decoded protocol = SONY
decoded command = 40 decoded address = 4 decoded protocol = NEC
decoded command = 41 decoded address = 4 decoded protocol = NEC