Hi guys,
I want to use my TV remote to send keyboard commands to my computer. So if I press the fast forward button on my TV remote, I want to send CTRL + F to my computer, which is fast forward for windows media player. So there are two parts that I need to work through. The first part to decode the IR signal being sent by the remote and a second part to send the signal to the computer. I can do both of these parts separately, but I cant get them to work together. I could probably get the project working if I used two arduinos, but ideally I should be able to do it on the one arduino.
So, to read the IR remote code, I used Ken Shirriff's popular and easy to use library
Everytime I press the remote up button for example, I get a short code, such as B54A30CF. Each button has a different code, so I can easily work out which button is pressed based on the code received. Its easy and works well.
Next I needed to send a signal to the computer. Again, instead of reinventing the wheel, I have used some code from the practical arduino book that can be found http://www.practicalarduino.com/projects/virtual-usb-keyboard
I have also built that project, it works well and can be used to send keyboard commands.
So then I figured, hey, I just need to combine the two parts now to make a working solution. BUT its not working out that easily and I think it has something to do with the timer. Which is where Im asking for some expert help...
So here is my code:
#include "UsbKeyboard.h"
#include <IRremote.h>
#define BUTTON_PIN 12
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
// If the timer isr is corrected
// to not take so long change this to 0.
#define BYPASS_TIMER_ISR 1
void setup() {
pinMode(BUTTON_PIN, INPUT);
digitalWrite(BUTTON_PIN, HIGH);
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
#if BYPASS_TIMER_ISR
// disable timer 0 overflow interrupt (used for millis)
TIMSK0&=!(1<<TOIE0); // ++
#endif
}
#if BYPASS_TIMER_ISR
void delayMs(unsigned int ms) {
/*
*/
for (int i = 0; i < ms; i++) {
delayMicroseconds(1000);
}
}
#endif
void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
}
UsbKeyboard.update();
digitalWrite(13, !digitalRead(13));
if (digitalRead(BUTTON_PIN) == 0) {
//UsbKeyboard.sendKeyStroke(KEY_B, MOD_GUI_LEFT);
UsbKeyboard.sendKeyStroke(KEY_H);
UsbKeyboard.sendKeyStroke(KEY_E);
UsbKeyboard.sendKeyStroke(KEY_L);
UsbKeyboard.sendKeyStroke(KEY_L);
UsbKeyboard.sendKeyStroke(KEY_O);
UsbKeyboard.sendKeyStroke(KEY_SPACE);
UsbKeyboard.sendKeyStroke(KEY_W);
UsbKeyboard.sendKeyStroke(KEY_O);
UsbKeyboard.sendKeyStroke(KEY_R);
UsbKeyboard.sendKeyStroke(KEY_L);
UsbKeyboard.sendKeyStroke(KEY_D);
//UsbKeyboard.sendKeyStroke(KEY_B, MOD_GUI_LEFT);
UsbKeyboard.sendKeyStroke(KEY_ENTER);
#if BYPASS_TIMER_ISR // check if timer isr fixed.
delayMs(20);
#else
delay(20);
#endif
}
}
The problem is, as soon as I use the "irrecv.enableIRIn(); // Start the receiver " code, my project stops being recognised as a usb keyboard. If I remove that code, my project is recognised by the computer as a keyboard, but obviously the code is incomplete and it wont work. Any help would be great.