leonardo as keyboard

Hi,

I use a leonardo configured as a keyboard, to read 5 input pins and send characters to a pc. This works fine when uploading it from the IDE. However, when I plug the leonardo to another pc it doesn’t work. the leonardo is recognised as a keyboard, but there are no keystrokes registered. Any idea?

code:

/*
flipbook controller
 greatbustards@gmail.com
 for leonardoboard!
*/

int inpPins[] = {
  2, 3, 4, 5, 6 };       // an array of input pin numbers 

char charOut[] = {
  'a','f','k','p','l'};  // chars to send to computer

/*
a: zoom out
 f: zoom in
 k: up
 p: down
 l: start
 */

int pinCount = 5;        // the number of pins (i.e. the length of the array)

int buttonState[] ={
  0,0,0,0,0};       // variable for reading the pushbuttons status

void setup() {

  // set keyb control. 
  pinMode(7, INPUT_PULLUP);

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only 
  }

  // initialize the pushbutton pin as an input:
  for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
    pinMode(inpPins[thisPin], INPUT_PULLUP);      
  }   

  if (digitalRead(7) == LOW) {
    Keyboard.begin();
  }  
}

void loop(){
  // read input pins
  for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
    buttonState[thisPin]=digitalRead(inpPins[thisPin]);
  }

  for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
    if (buttonState[thisPin]==LOW) {
      //Serial.print(charOut[thisPin]);
      // send keyboard command
      Keyboard.write(charOut[thisPin]);
    };        
  } 
  delay(100);
}
  for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
    buttonState[thisPin]=digitalRead(inpPins[thisPin]);
  }

  for (int thisPin = 0; thisPin < pinCount; thisPin++)  {

Why can’t you use one for loop? Why does the value read from the pin need to be stored in an array? Why does the array need to be global?

but there are no keystrokes registered.

Registered by what?

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the # button above the posting area.

I found in one case that I had to install the Leonardo driver on a Windows PC before it would be recognized as a keyboard. You don’t have to install the entire IDE, just take the “drivers” folder and install that with the Leonardo plugged in.

I found in one case that I had to install the Leonardo driver on a Windows PC before it would be recognized as a keyboard.

Second that!

Of course, you can resort to USBkeyboard.h and setup your sketch as USB 1.1 HID.
V-USB HID 1.1

Also
http://eleccelerator.com/usnoobie/

Are you testing your sketch in one piece of software, like a text editor, and then trying to use it with a video game? I did a similar project and found that my sketches would all work fine in some software but fail in the games. The games seemed to only like the keyboard.press() and keyboard.release() methods. I was using my prototype sequential shifter the other day. :slight_smile:

Assuming you've now got all the drivers in order.

“Why can’t you use one for loop? Why does the value read from the pin need to be stored in an array? Why does the array need to be global?”

I need the buttonstate at some other point in the program to keep track of buttonchanges… that’s why.

However, I found out that first installing e leonardo driver on the second machine (win 7) solved it. Don’t know why because it was recognised as a keyboard at the fist place.