IR remote keyboard - short press = 1 key press - long press = another key press

I’m quite new to arduino and programming all together. I managed to figure out how to modify a sketch to allow me to turn an IR remote into a keyboard so I can control a raspberry pi running a car computer software program. This works just fine and I’m happy with the results. However, my remote is limited in the buttons it has and thus, I’d like to figure out how to measure a long press from the IR remote and allow a short press to do one key stroke and a long press to do another key stroke.

#include <IRremote.h>
#include <Keyboard.h>

int RECV_PIN = 21;  // IR Receiver connected to pin 21

// Change these to fit your IR remote codes
#define IR_CODE_HAT_UP 0x815700FF //Navigate up
#define IR_CODE_HAT_DOWN 0x8157C03F //Navigate down
#define IR_CODE_HAT_RT 0x815740BF //Navigate right
#define IR_CODE_HAT_LT 0x8157807F //Navigate left
#define IR_CODE_ZOOM_DOWN 0x8157D02F //scroll left
#define IR_CODE_ZOOM_UP 0x815750AF //Scroll right
#define IR_CODE_ENTER 0x815710EF //Select
#define IR_CODE_RET 0x8157906F //Back
#define IR_CODE_MENU 0x815730CF //Home
#define IR_CODE_VOICE 0x8157708F //Voice command in Android Auto
#define IR_CODE_POS 0x8157B04F //Bring OpenAuto Pro to front


IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  Serial.begin (9600); //Serial.begin(9600)
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  Keyboard.begin();  //begin keyboard
  if (irrecv.decode(&results)) {
    //Serial.println(results.value, HEX);
    switch(results.value) {
     case IR_CODE_HAT_UP:
       Keyboard.write(KEY_UP_ARROW);
       break;
     case IR_CODE_HAT_DOWN:
       Keyboard.write(KEY_DOWN_ARROW);
       break;
     case IR_CODE_HAT_RT:
       Keyboard.write(KEY_RIGHT_ARROW);
       break;
     case IR_CODE_HAT_LT:
       Keyboard.write(KEY_LEFT_ARROW);
       break;
     case IR_CODE_ZOOM_DOWN:
       Keyboard.write('1');
       break;
     case IR_CODE_ZOOM_UP:
       Keyboard.write('2');
       break;
     case IR_CODE_ENTER:
       Keyboard.write(KEY_RETURN);
       break;
     case IR_CODE_RET:
       Keyboard.write(KEY_ESC);
       break;
     case IR_CODE_MENU:
       Keyboard.write('H');
       break;
     case IR_CODE_VOICE:
       Keyboard.write('M');
       break;
     case IR_CODE_POS:
       Keyboard.write(KEY_F12);
       break;
     }
    delay(100); //Debouncing the IR receive signal
    irrecv.resume(); // Receive the next value
  }
  Keyboard.end();  //stops keybaord
}

I’ve found a number of things in the button category that look similar and have tried the following test code below to see if I can at least get the arduino to register the long and short press by looking at the serial monitor

#include <IRremote.h> //IR library

#define IR_CODE_ENTER 0x815710EF //Select

//We need to track how long the momentary pushbutton is held in order to execute different commands
//This value will be recorded in seconds
float pressLength_milliSeconds = 0;
 
// Define the *minimum* length of time, in milli-seconds, that the button must be pressed for a particular option to occur
int optionOne_milliSeconds = 100;
int optionTwo_milliSeconds = 2000;        
 
//The Pin your IR Reciever is attached to
int RECV_PIN = 2;
 
//Pin your LEDs are attached to
int ledPin_Option_1 = 13;
int ledPin_Option_2 = 12;

IRrecv irrecv(RECV_PIN);
decode_results results;

 
void setup(){
 
  //set the LEDs pins as outputs
  pinMode(ledPin_Option_1, OUTPUT); 
  pinMode(ledPin_Option_2, OUTPUT);

   
 
  //Start serial communication - for debugging purposes only
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver                                     
 
} // close setup
 
 
void loop() {
if(irrecv.decode(&results)){
  //Record *roughly* the tenths of seconds the button in being held down
  while (irrecv.decode(&results) == IR_CODE_ENTER ){ 
 
    delay(100);  //if you want more resolution, lower this number 
    pressLength_milliSeconds = pressLength_milliSeconds + 100;   
 
    //display how long button is has been held
    Serial.print("ms = ");
    Serial.println(pressLength_milliSeconds);
 
  }//close while
 
 
  //Different if-else conditions are triggered based on the length of the button press
  //Start with the longest time option first
 
  //Option 2 - Execute the second option if the button is held for the correct amount of time
  if (pressLength_milliSeconds >= optionTwo_milliSeconds){
 
    digitalWrite(ledPin_Option_2, HIGH);     
 
  } 
 
  //option 1 - Execute the first option if the button is held for the correct amount of time
  else if(pressLength_milliSeconds >= optionOne_milliSeconds){
 
    digitalWrite(ledPin_Option_1, HIGH);  
 
  }//close if options
 
 
  //every time through the loop, we need to reset the pressLength_Seconds counter
  pressLength_milliSeconds = 0;
 
}
}// close void loop

the above code just does not seem to register anything on the serial monitor. Does anyone have some places to start here to incorporate long press remote commands for multi function button presses?

Then you must have confirmed the IR remote will continuously send the codes for the button being pressed. Rather unusual, but not impossible.

Paul

I have confirmed that my remote will send the same code infinitum when a button is pressed (it's not the FFF version).

As I start to understand this forum better, I think it might be better if this was moved to the "Programming Questions" section. Can it be moved, or should I start a thread over there?