Go Down

Topic: Arduino Micro, USB Keyboard not 100% like a real keyboard (Read 1 time) previous topic - next topic

calli

Mar 06, 2013, 08:59 pm Last Edit: Mar 06, 2013, 09:01 pm by calli Reason: 1
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: [Select]

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

Erdin

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.

calli

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

calli

@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

James C4S


BTW: where is the source for that functionality?

It's all included with the IDE and also can be found on the Arduino github.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

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.

Carsten

Louis Davis




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

calli

Thanks, interesting even though the problem is solved.

Carsten

James C4S


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: [Select]

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$
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Go Up