Keyboard.press types wrong keystrokes

Hello Im obviulsy fairly new to keyboard library but ive used it before. Im trying to make a simple gesture control movement of WASD for videogames using the MPU6050 gyro. The code that graphs everything works fine but when I try to add the keyboard funtionality the devices sends wrong keystrokes. instead of "WASD" I get "X, Q, and }" I dont know in what order due to the gyro giving too many readings to fast.

My code: (using a standard sample of michael schoeffler)

#include "Wire.h" 
#include "Keyboard.h"
const int MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69.

int16_t accelerometer_x, accelerometer_y, accelerometer_z;
int16_t gyro_x, gyro_y, gyro_z; 
int16_t temperature; 

char tmp_str[7]; 

char* convert_int16_to_str(int16_t i) { 
  sprintf(tmp_str, "%6d", i);
  return tmp_str;
}

void setup() {
  Serial.begin(9600);
  Wire.begin();
  Wire.beginTransmission(MPU_ADDR);
  Wire.write(0x6B); 
  Wire.write(0);
  Wire.endTransmission(true);
}
void loop() {
  Wire.beginTransmission(MPU_ADDR);
  Wire.write(0x3B);
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_ADDR, 7*2, true);
  

  accelerometer_x = Wire.read()<<8 | Wire.read();
  accelerometer_y = Wire.read()<<8 | Wire.read(); 
  accelerometer_z = Wire.read()<<8 | Wire.read();
  temperature = Wire.read()<<8 | Wire.read(); 
  gyro_x = Wire.read()<<8 | Wire.read(); 
  gyro_y = Wire.read()<<8 | Wire.read(); 
  gyro_z = Wire.read()<<8 | Wire.read();
  // print out data
  Serial.print("aX = "); Serial.print(convert_int16_to_str(accelerometer_x));
  Serial.print(" | aY = "); Serial.print(convert_int16_to_str(accelerometer_y));
  Serial.print(" | aZ = "); Serial.print(convert_int16_to_str(accelerometer_z));
  // the following equation was taken from the documentation [MPU-6000/MPU-6050 Register Map and Description, p.30]
  Serial.print(" | tmp = "); Serial.print(temperature/340.00+36.53);
  Serial.print(" | gX = "); Serial.print(convert_int16_to_str(gyro_x));
  Serial.print(" | gY = "); Serial.print(convert_int16_to_str(gyro_y));
  Serial.print(" | gZ = "); Serial.print(convert_int16_to_str(gyro_z));
  Serial.println();
  if(gyro_y>1100)
  Keyboard.press("w");
  else Keyboard.release("w");
if(gyro_y<-1150)
  Keyboard.press("s");
  else Keyboard.release("s");
if(gyro_x>1150)
  Keyboard.press("d");
  else Keyboard.release("d");
if(gyro_x<-1150)
  Keyboard.press("d");
  else Keyboard.release("d");
  // delay
  delay(60);
}
if(gyro_x>1150)
  Keyboard.press("d");
  else Keyboard.release("d");
if(gyro_x<-1150)
  Keyboard.press("d");
  else Keyboard.release("d");

No answer here, just things to ask yourself: Were two "d"s and no "a" intentional?
You noted that too many characters were being produced... can you Serial.print() them all and see if non-WASD characters are being used? Can you limit the characters produced to WASD?

Is your PC keyboard layout set to US-English? The Keyboard library ends codes for positions on the keyboard. It's your operating system that maps those key positions to characters.

If your keyboard layout is not US-English, send the character on that key of the US-English keyboard.

Well I checked and apparently I had my keyboard on another version of english. Once I changed it I stopped getting "}" and instead gettint "T" at least the W key finally shows up so that narrows the problem a bit thanks.

I wouldnt know how to send the characters as you commented on your second paragraph sorry.

thanks but it still gives the wrong keystrokes.

Well to tell you the truth I do not know how to limit the characters to WASD (wich do not appear from a start), so that is my question precisely. ive seen all the characters printed and as I commented on my post only non WASD characters show up.

I would start by simplifying your system as much as possible. You have a lot going on in your current sketch and it is not clear which parts of the code can safely be ignored for the purposes of troubleshooting. That makes things more difficult for you, and more difficult for the helpers here as well (e.g., I can easily plug an Arduino Micro in to my computer, but I don't own an MPU6050 and it would take some effort to dig one out and wire it up even if I did).

So start with a basic test of the keyboard emulation functionality. If that isn't working, then there is no point in us looking at the MPU6050 code at this stage in the process.

I like to use this simple sketch as my Blink or "hello world" equivalent for keyboard emulation:

// Connect pin 2 to ground to enable keyboard emulation.

#include <Keyboard.h>

const byte enablePin = 2;
const byte LEDPin = LED_BUILTIN;

void setup() {
  pinMode(enablePin, INPUT_PULLUP);
  pinMode(LEDPin, OUTPUT);
  Keyboard.begin();
}

void loop() {
  if (digitalRead(enablePin) == LOW) {
    Keyboard.print("hello");
    digitalWrite(LEDPin, HIGH);
    delay(500);
    digitalWrite(LEDPin, LOW);
    delay(500);
  }

After you upload that to your Arduino board and use a jumper wire to connect pin 2 to the ground pin, does the board start typing "hello" at 1 Hz?

Thanks a lot ! it does print hello when I redirect it to ground!
So the problem migh be related to the way the MPU6050 is coded that narrows it down.
:star_struck: