Arduino Pro Micro: Keyboard.press() is not working

I have been trying to build a gaming wheel using Arduino pro micro and an MPU 6050 by following a tutorial found on youtube. I constructed the circuit as shown in the image below.

The sketch uploaded:

#include<Keyboard.h>
#include<Wire.h>
#include<MPU6050.h>


// Threshold value for steering
int THRESHOLD = 4000;

// Controller buttons
int brake = 5;
int nitro = 6;
int pedal = 4;

// Indicator LEDs
int left_led = 8;
int right_led = 9;

int x;
int y;
int z;
 
const int MPU_addr=0x68;  // I2C address of the MPU-6050

void indicator_blink()
{
  // blinks the leds - 5 times
  int i = 0;
  
  for(i=0; i<5; i++){
    digitalWrite(left_led, HIGH);
    digitalWrite(right_led, HIGH);
    delay(100);
    digitalWrite(left_led, LOW);
    digitalWrite(right_led, LOW);
    delay(100);
  }
}

void setup(){
  // set pin mode and initialize things
  pinMode(brake, INPUT);
  pinMode(nitro, INPUT);
  pinMode(pedal, INPUT);

  pinMode(left_led, OUTPUT);
  pinMode(right_led, OUTPUT);
  
  Serial.begin(9600); 
 
  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);

  indicator_blink();
}

void loop()
{ // Main loop that runs forever
  
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr, 14, true);  // request a total of 14 registers
  
  x = Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)    
  y = Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  z = Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)


  if (digitalRead(brake)){
    Keyboard.release(KEY_UP_ARROW);
    Keyboard.press(KEY_DOWN_ARROW);
    //Keyboard.press(217);
  }
  
  if (digitalRead(nitro)){
    delay(10);
    Keyboard.press((char) 32);
    Keyboard.release((char) 32);
  }
    
  else if (digitalRead(pedal)){
    Keyboard.release(KEY_DOWN_ARROW);
    Keyboard.press(KEY_UP_ARROW);
    //Keyboard.press(218);
  
    if(y < -THRESHOLD){     
          Serial.print("    Left    ");
          Keyboard.release(KEY_RIGHT_ARROW);
          Keyboard.press(KEY_LEFT_ARROW);
          digitalWrite(left_led, HIGH);
      }
  
    else if(y > THRESHOLD){     
          Serial.print("    Right    ");
          Keyboard.release(KEY_LEFT_ARROW);
          Keyboard.press(KEY_RIGHT_ARROW);
          digitalWrite(right_led, HIGH);
      //Keyboard.press(37);
      }
     else{
          Keyboard.releaseAll();
     }
   }
   
  //Serial.println(y);
  delay(10);
    digitalWrite(left_led, LOW);
    digitalWrite(right_led, LOW);
}

The lights blink in the Arduino every time I push a button. When I push the pedal button, the MPU6050 reacts too (2 external LED blinks). The serial print works as expected (Serial Monitor).

The problem is that no inputs are being taken on my pc. The Keyboard.press() is not doing anything.

p.s. Arduino does show up as a keyboard on my pc after sketch upload.

Can anyone tell me what I am doing wrong here?

Where is Keyboard.begin()?

I am an idiot. It worked after a simple restart :confused:
Didn't need the keyboard.begin();

Facing a new issue.. Keyboard.press() is giving multiple output at a time..

Thank you for taking your time to reply. :slight_smile: :smiley:

Multiple items are connected to the controller.
The best way in such a project is splitting it into parts and verifying that each single part works. Did You do like that?

Putting all components in a sack, shaking it and starting fault finding is the most difficult way to advance in a project.

Hey man, thanks for the reply. Yes, I have done exactly as you suggested.
The problem is solved though. Just restarted the pc.

Facing a new issue. One Keyboard.press() is sending multiple strokes at a time. any idea how to solve that?

Thanks again!

Sounds a lot like, you didn't debounce the switch.

Regarding begin(), it doesn't seem wise to randomly experiment with omitting API code while you are still experiencing difficulties...

I am a newbie :slight_smile: thanks for your patience with me.
Can you tell me how to do it?

Here, you test to see if the key is down. But you run this test continuously. See the problem?

You need to test when it becomes pressed, not when it is pressed.

1 Like

Yes. Read and try out example sketches and tutorials. Bookmark the original documentation for Arduino on the reference page. Don't start any new sketch until you fully understand the last one.

1 Like

Thanks, big time!

You need two more things, there are IDE examples for both of them.

  1. switch debounce https://docs.arduino.cc/built-in-examples/digital/Debounce
  2. state change detection https://docs.arduino.cc/built-in-examples/digital/StateChangeDetection
1 Like

I take my hat off!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.