IR Remote Button Hold - Used as momentary switch

Thanks in advance for help and advice!

For a few days I have tried to crack the code…I have done research here and other places to figure it out.

I need for the PIN to stay HIGH when I press and hold the remote button. When the button isnt pressed, the PIN goes LOW. The remote I am using does produce a “repeat” code, after the initial code is given, if the button is held. I have been successfully able to code so that the initial code is always being read. The trouble I am having is being able to use the remote buttons as a momentary switch. If the button is held, PIN is high. If button isn’t pressed or held, button is LOW. I am only able to get it act like a latching button. Any help would be greatly appreciated!


#include <IRremote.h> // Library from "" 
int RECV_PIN = 11; //Pin of the out leg of the 38kHz demodulating Infrared Receiver Module 
int output1 = 5; // LED connected to pin
int output2 = 6; // LED connected to pin
int output3 = 9; // LED connected to pin
const int code1 = 0x10EFF807; //Remote code for button "A" on remote
const int code2 = 0x10EF7887; //Remote code for button "B" on remote
const int code3 = 0x10EF58A7; //Remote code for button "C" on remote

IRrecv irrecv(RECV_PIN);
decode_results results;
int lastCode = 0; // This keeps track of the last code RX'd

void setup()
  Serial.begin(9600);   //
  irrecv.enableIRIn();  //
  pinMode(output1, OUTPUT);
  pinMode(output2, OUTPUT);
  pinMode(output3, OUTPUT);

void loop() {

  if (irrecv.decode(&results))
    uint16_t resultCode = (results.value & 0xFFFF);
    if (resultCode == 0xFFFF)
      resultCode = lastCode;
      lastCode = resultCode;

    if (resultCode == code1) {
      digitalWrite(output1, HIGH);
    } else {
      digitalWrite(output1, LOW);
    irrecv.resume(); // Receive the next value

    if (resultCode == 0xFFFF)
      resultCode = lastCode;
      lastCode = resultCode;

Here is your problem.

When there is no new code (so resultCode == 0xFFFF), the previous code is kept. So if you once press the button, lastCode is set to that code, then you don't press the button, no new code is received, and resultCode is set to lastCode again and again holding your pin HIGH.

Pressing another button on the remote, and sending another code, will toggle the PIN to LOW. Or you have to simply remove those lines.