Pages: [1]   Go Down
Author Topic: Arduino Micro, USB Keyboard not 100% like a real keyboard  (Read 1040 times)
0 Members and 1 Guest are viewing this topic.
Berlin
Offline Offline
Full Member
***
Karma: 0
Posts: 112
3D Monkey
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

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

Code:
// 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);   
  }
}


* Final.JPG (47.91 KB, 600x404 - viewed 22 times.)
« Last Edit: March 06, 2013, 03:01:35 pm by calli » Logged

Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Berlin
Offline Offline
Full Member
***
Karma: 0
Posts: 112
3D Monkey
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Berlin
Offline Offline
Full Member
***
Karma: 0
Posts: 112
3D Monkey
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@Erdin: You are my hero ;-)

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....

Thank you very much, you made my evening!
Carsten
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

BTW: where is the source for that functionality?
It's all included with the IDE and also can be found on the Arduino github.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Berlin
Offline Offline
Full Member
***
Karma: 0
Posts: 112
3D Monkey
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Carsten
Logged

Austin, TX
Offline Offline
God Member
*****
Karma: 12
Posts: 524
carpe diem
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Carsten

Here is the lazyman pointer:
https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/HID.cpp
Logged

Berlin
Offline Offline
Full Member
***
Karma: 0
Posts: 112
3D Monkey
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, interesting even though the problem is solved.

Carsten
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Then get your local search fixed.

Code:
James-MBP2b:cores james$ cd /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino
James-MBP2b:arduino james$ grep Keyboard *
HID.cpp:Keyboard_ Keyboard;
HID.cpp: // Keyboard
HID.cpp:    0x09, 0x06,                    // USAGE (Keyboard)
HID.cpp:    0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
HID.cpp: 0x19, 0xe0,                    //   USAGE_MINIMUM (Keyboard LeftControl)
HID.cpp:    0x29, 0xe7,                    //   USAGE_MAXIMUM (Keyboard Right GUI)
HID.cpp:    0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
HID.cpp:    0x29, 0x65,                    //   USAGE_MAXIMUM (Keyboard Application)
HID.cpp:// Keyboard
HID.cpp:Keyboard_::Keyboard_(void)
HID.cpp:void Keyboard_::begin(void)
HID.cpp:void Keyboard_::end(void)
HID.cpp:void Keyboard_::sendReport(KeyReport* keys)
HID.cpp:size_t Keyboard_::press(uint8_t k)
HID.cpp:size_t Keyboard_::release(uint8_t k)
HID.cpp:void Keyboard_::releaseAll(void)
HID.cpp:size_t Keyboard_::write(uint8_t c)
USBAPI.h:// Keyboard
USBAPI.h:class Keyboard_ : public Print
USBAPI.h: Keyboard_(void);
USBAPI.h:extern Keyboard_ Keyboard;
James-MBP2b:arduino james$
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Pages: [1]   Go Up
Jump to: