after trying a bit with a normal Arduino and usbkeyboard.h I found it too wacky and bought a Arduino Micro. It was quite straight forward to make my application, a footswitch controller for a music game (Rocksmith).
The Keyboard from the Arduino micro works well in Windows and even the menu control in the game works with it. HOWEVER and thats my problem, as soon as the real game (outside menus, playing a "level") starts no keypresses are send (or received?). A second (real) USB Keyboard connected works as expected, so I think the Arduino Micro does not act exact like a USB Keyboard...
Any suggestions how to solve this? Who is the developer for this part of the Arduino project? BTW: where is the source for that functionality?
Any suggestions welcome to save my project from going to the "not working projects" box...
Cheers,
Carsten
EDIT: The sketch
// Keybox
#define SHIFTLED 10
#define POWERLED 11
#define DEBOUNCE 10 // button debouncer
// here is where we define the buttons that we'll use. button "1" is the first, button "6" is the 6th, etc
byte buttons[] = {
2, 3, 4, 5,6,7}; // the analog 0-5 pins are also known as 14-19
// This handy macro lets us determine how big the array up above is, by checking the size
#define NUMBUTTONS sizeof(buttons)
// Track Array
byte pressed[NUMBUTTONS];
void setup() {
byte i;
// set up serial port
// Serial.begin(9600);
// pin13 LED
pinMode(POWERLED,OUTPUT);
pinMode(SHIFTLED,OUTPUT);
// Make input & enable pull-up resistors on switch pins
for (i=0; i< NUMBUTTONS; i++) {
pinMode(buttons[i], INPUT);
// digitalWrite(buttons[i], HIGH); // we use external Pulls
}
delay(1000);
Keyboard.begin();
}
void check_switches()
{
byte index;
for (index = 0; index < NUMBUTTONS; index++)
{
if (digitalRead(buttons[index]) and (pressed[index]<DEBOUNCE))
{
pressed[index]++;
}
else if (!digitalRead(buttons[index]))
{
pressed[index]=0;
}
}
}
void keySend(char key)
{
digitalWrite(POWERLED, LOW);
Keyboard.write(key);
delay(100);
}
void loop()
{
digitalWrite(POWERLED, HIGH);
delay(10);
check_switches();
if (pressed[0] >= DEBOUNCE)
{
if (!digitalRead(SHIFTLED))
{
keySend(KEY_F1); // F1
}
else
{
keySend(KEY_LEFT_ARROW); // Links
}
}
else if (pressed[1] >= DEBOUNCE)
{
if (!digitalRead(SHIFTLED))
{
keySend(KEY_F2);
}
else
{
keySend(' ');
}
}
else if (pressed[2] >= DEBOUNCE)
{
if (!digitalRead(SHIFTLED))
{
keySend(KEY_F3);
}
else
{
keySend(KEY_RIGHT_ARROW);
} }
else if (pressed[3] >= DEBOUNCE)
{
keySend(KEY_ESC); // ESC
}
else if (pressed[4] >= DEBOUNCE)
{
keySend(KEY_RETURN); // ENTER
}
if (digitalRead(buttons[NUMBUTTONS-1]))
{
digitalWrite(SHIFTLED, HIGH);
}
else
{
digitalWrite(SHIFTLED, LOW);
}
}
I think it should work. I'm looking at the sketch and see a few strange things.
You use Keyboard.write(). That 'sends' a character, as if someone is typing a key very quickly. After that, you wait 100ms, that is a long time.
Could you make small test sketch for one button.
Use the Keyboard.press() if the button is pressed, and Keyboard.release() if the button is released. Try if that is accepted by the game.
I will try. Maybe the game itself uses a different keyboard polling because of the realtime needs of that specific game and so it misses that keypresses.
The delay(100) I use for the pause between keyboard key repeat.
will come back later with results.
Thanks,
Carsten
Works now. I was unsure from beginning what function to use, and then decided that they are equal and that there must be some kind of buffer so that no keypress gets lost, but as it seems that function is much lower level, near hardware....
Yea, sure, but I could not find it searching for "Leonardo", "Keyboard" or "USB". Was hoping for a lazyman pointer. Often the dvelopers comments are quite helpfull.
calli:
BTW: where is the source for that functionality?
It's all included with the IDE and also can be found on the Arduino github.
Yea, sure, but I could not find it searching for "Leonardo", "Keyboard" or "USB". Was hoping for a lazyman pointer. Often the dvelopers comments are quite helpfull.
calli:
Yea, sure, but I could not find it searching for "Leonardo", "Keyboard" or "USB". Was hoping for a lazyman pointer. Often the dvelopers comments are quite helpfull.