0

I'm working on a project where I can use an IR remote to hook up to my PC and control it because I have a projector connected to it that I want a remote for. I've figured out all of the IR remote stuff and have also been able to configure my Arduino UNO as an HID keyboard (I know there are better solutions for HID devices than Arduino UNO but the way I want this set up makes it so I have to use an UNO). However when I try to send key inputs to my PC it acts like its pressing the Control button (for example if I press A on my actual keyboard after pressing a button on the remote it selects everything on the page I am currently on). I've tried a bunch of different keycodes and looked at other people's code and couldn't find where I am going wrong here is the code I have for my project so far:

#include <IRremote.hpp>
#include <HID.h>     
#include <IRremote.h> 

int RECV_PIN = 7;
uint8_t buf[8] = { 0 };    

void setup() {     
  Serial.begin(9600);     
  IrReceiver.begin(RECV_PIN, ENABLE_LED_FEEDBACK);  

  delay(200); 
}

void loop(){ 
  if (IrReceiver.decode()) {
    int i;
    int command = IrReceiver.decodedIRData.command;
    IrReceiver.stop();  
    switch (command) {
      case 70:
        Serial.println("Volume +");
        buf[2] = 0x80;
        Serial.write(buf, 8);
        break;
      /* 
         There are a bunch more case statements with different IR commands that 
         correlate to functions I want the remote to do
      */

    }
    releaseKey();
    delay(500);
    IrReceiver.start(8000);
    IrReceiver.resume();
  }
}

void releaseKey() {
  buf[0] = 0;
  buf[2] = 0;
  Serial.write(buf, 8);
}
Jonah.Checketts
  • 54
  • 2
  • 10
  • 1
    Are you sure you're using an Arduino Uno? The user-programmable ATmega328p on there does not have a user interface so I'm not sure how you got it to act as an HID. (The Arduino Leonardo or any other ATmega32U4 would work well.) Does it show up as an HID in the device manager? Did you have to modify the firmware or install a special driver? Why are you setting buf[2] to 0x80? – David Grayson Nov 23 '22 at 05:08
  • I installed different firmware on the device to make it show up as an HID device and it does show up as and HID keyboard in device manager. and 0x80 is the keycode for the increase volume key. I should probably put a define statement for that. – Jonah.Checketts Nov 23 '22 at 05:29
  • 1
    Thanks for clarifying. Also, I meant "USB interface" in the comment above, not "user interface". Anyway, you'll need to provide the source code or at least the documentation of your mysterious firmware that converts serial commands into HID or else there isn't much hope of anyone being able to help you. – David Grayson Nov 23 '22 at 06:45
  • 1
    It would also help if you provide a [mcve]. The first line I would remove while minimizing this code is the thing that prints "Volume +" because I doubt that is a valid command to the HID firmware. – David Grayson Nov 23 '22 at 06:56
  • I got all my firmware from this tutorial on youtube: https://www.youtube.com/watch?v=tvqA-JcTQNg&ab_channel=TechToTinker – Jonah.Checketts Nov 23 '22 at 07:05
  • 1
    I can't find the source code or documentation in there so your question is still very difficult. There is some example code though, and the example code never prints "Volume +" on the serial port. Why do you think it's valid to do that? Have you tried removing that line? – David Grayson Nov 23 '22 at 07:32
  • 1
    **P.S. Beware of downloading random USB firmware on the Internet, especially if you didn't compile it and it acts as USB keyboard. It can send arbitrary keystrokes to your computer, giving a hacker full access to it.** – David Grayson Nov 23 '22 at 07:33
  • I added the Volume + line to verify that the IR remote commands were connected properly to their respective buttons. Removing that line seemed to fix the problem so thanks for your help! And I was worried to download the file for that reason but decided to trust it because I couldn't find another solution and there were a lot of comments under the youtube video and none of the ones I read mentioned anything bad happening. Thank you again for the help! – Jonah.Checketts Nov 23 '22 at 08:51

1 Answers1

0

Removing the line that prints "Volume +" was the solution. That was apparently not a valid command for the HID firmware. Remember to always simplify your code to the simplest possible thing that should work but doesn't, so you can solve problems like that yourself.

David Grayson
  • 84,103
  • 24
  • 152
  • 189