Go Down

Topic: Arduino Leonardo. Keyboard library stopped working? (Read 820 times) previous topic - next topic

The_Rid

Hello.

I've been using this Leonardo as an arcade controller for 9 months now, and i've had no problems untill a week ago when the board stopped sending Keyboard signals.

This is the code i've been using, this is not my code by the way, I just added Serial.println() bit of code yesterday to see if I could fix the issue myself.

Code: [Select]
#include <Keyboard.h>
#include <Mouse.h>

#define DELAY            5  // Delay per loop in ms
 enum PinAssignments
 {
  encoderPinA = 0,
  encoderPinB = 1,
  encoderPinC = 3,
  encoderPinD = 2,
 
 };
//This is up to your pin wiring
 
 int encoderPos[] = {0,0};
 static boolean rotating[] = {false,false};
 
 boolean A_set = false;             
 boolean B_set = false;
 boolean C_set = false;             
 boolean D_set = false;
 
void setup()
{
  pinMode(4, INPUT_PULLUP);
  pinMode(A0, INPUT_PULLUP);
  pinMode(A1, INPUT_PULLUP);
  pinMode(A2, INPUT_PULLUP);
  pinMode(A3, INPUT_PULLUP);
  pinMode(A4, INPUT_PULLUP);
  pinMode(A5, INPUT_PULLUP);
  Keyboard.begin();
 
  pinMode(encoderPinA, INPUT_PULLUP);
  pinMode(encoderPinB, INPUT_PULLUP);
  pinMode(encoderPinC, INPUT_PULLUP);
  pinMode(encoderPinD, INPUT_PULLUP);
 
  attachInterrupt(0, doEncoderC, CHANGE);
  attachInterrupt(1, doEncoderD, CHANGE);
 
  attachInterrupt(2, doEncoderA, CHANGE);
  attachInterrupt(3, doEncoderB, CHANGE);
 
  Serial.begin(9600);
}
 
void loop() {
 
  if(digitalRead(4)==LOW){
    Serial.println("Pressed: a");
    Keyboard.press('a');
  }
  if(digitalRead(4)==HIGH){
    Serial.println("Released: a");
    Keyboard.release('a');
  }
  if(digitalRead(A0)==LOW){
    Serial.println("Pressed: b");
    Keyboard.press('b');
  }
  if(digitalRead(A0)==HIGH){
    Serial.println("Released: b");
    Keyboard.release('b');
  }
  if(digitalRead(A1)==LOW){
    Serial.println("Pressed: c");
    Keyboard.press('c');
  }
  if(digitalRead(A1)==HIGH){
    Serial.println("Released: c");
    Keyboard.release('c');
  }
  if(digitalRead(A2)==LOW){
    Serial.println("Pressed: d");
    Keyboard.press('d');
  }
  if(digitalRead(A2)==HIGH){
    Serial.println("Released: d");
    Keyboard.release('d');
  }
  if(digitalRead(A3)==LOW){
    Serial.println("Pressed: i");
    Keyboard.press('i');
  }
  if(digitalRead(A3)==HIGH){
    Serial.println("Released: i");
    Keyboard.release('i');
  }
  if(digitalRead(A4)==LOW){
    Serial.println("Pressed: f");
    Keyboard.press('f');
  }
  if(digitalRead(A4)==HIGH){
    Serial.println("Released: f");
    Keyboard.release('f');
  }
  if(digitalRead(A5)==LOW){
    Serial.println("Pressed: g");
    Keyboard.press('g');
  }
  if(digitalRead(A5)==HIGH){
    Serial.println("Released: g");
    Keyboard.release('g');
  }
  //Encoder Reset
  for(int i=0;i<=1;i++)
  {
    rotating[i] = true;
    if (encoderPos[i] != 0)
    {
      if(i==0)  Mouse.move(encoderPos[i],0,0);
      if(i==1)  Mouse.move(0,encoderPos[i],0);
 
      encoderPos[i] = 0;
    }
  }
 
  delay(DELAY);
}
 
void doEncoderA()
{
 
  if( digitalRead(encoderPinA) != A_set )
  { 
    A_set = !A_set;
 
    if ( A_set && !B_set )
      encoderPos[0] += 1;
 
    rotating[0] = false; 
  }
}
 
void doEncoderB()
{
 
  if( digitalRead(encoderPinB) != B_set ) {
    B_set = !B_set;
   
    if( B_set && !A_set )
      encoderPos[0] -= 1;
 
    rotating[0] = false;
  }
}
 
void doEncoderC()
{
  if( digitalRead(encoderPinC) != C_set )
  { 
    C_set = !C_set;
 
    if ( C_set && !D_set )
      encoderPos[1] += 1;
 
    rotating[1] = false;
  }
}
 
void doEncoderD()
{
  if( digitalRead(encoderPinD) != D_set ) {
    D_set = !D_set;
   
    if( D_set && !C_set )
      encoderPos[1] -= 1;
 
    rotating[1] = false;
  }
}


It emulates a Sound Voltex arcade controller:



By acting as an USB keyboard for the buttons, and mouse X/Y movement for the knobs. I have a video of me playing on my controller if anybody is curious.

The keyboard signals are controlled by the pushbuttons and the mouse movement is controlled by a pair of rotary encoders.

Here's the thing, it has been working fine for 9 months until a week ago. I built the controller in march of this year, and i've been using it at least 3 times a week since then.

A week ago I plugged it in as usual, but to my surprise the controller stopped sending keyboard signals to my computer. I tested this by opening up notepad and started pressing buttons, none of them wrote anything on the notepad.

The encoders work perfectly. I rotate them and i see the cursor on screen moving as it's supposed to.

So now I added that Serial.println() bit of code to see if maybe the ground daisy chain for the pushbuttons got loose, but this is not the case.

In this image none of the pushbuttons are being pressed:



And on this one, all of the buttons are pressed:



I also tested by pressing the buttons individualy and they do show up on the serial monitor as well. But again, theres no keyboard signal being recieved by my computer.

I also tried adding Mouse.begin() under the Keyboard.begin() bit of code, Mouse.press() and Mouse.release() under pin 4 where Keyboard.press() and Keyboard.release() are.
And sure thing it works, I press and release the button assigned to pin 4 and it results in a mouse click, I hold the button and it holds the left click, I release the button and it releases, as it's supposed to.

So this is why I think the keyboard library stopped working.
Today I updated the Arduino IDE to 1.6.13 to see if that may have solved the issue, but still the same thing, the buttons don't work, but the cursor moves with the encoders as I described previously.

I tried to plug it in another PC and the same thing happens, the encoders work, but the buttons don't.
Both machines are running Windows 7 with the lastest updates so, maybe it's a windows update issue?

I'm honestly completely lost, other than turning the arduino into a joystic instead of emulating a keyboard and mouse, because again i think the keyboard library stopped working somehow, which doesn't make much sense.

Any help would be very much appreciated. Thanks

pert

Can you try it on a different computer to check if the problem is specific to that computer?

If the thing was working and then stopped working without you uploading the sketch to the Arduino between the working and non-working state then it's not caused by a new bug in the library because you were still running the same code since the last upload.

oqibidipo

Code: [Select]

 if(digitalRead(4)==LOW){
    Serial.println("Pressed: a");
    Keyboard.press('a');
  }
  if(digitalRead(4)==HIGH){
    Serial.println("Released: a");
    Keyboard.release('a');
  }


You are flooding the computer with keypress/release events, 200*7 events per second.
You should send an event only when the state of a pin has changed.

Go Up