Leonardo with Gyroscope - Gamepad

Hello everyone,

I am trying my best to finish project of gamepad with gyroscope (MPU-9050), on my Arduino Leonardo.
I have no problem to read and use potentiometers and buttons, but I can not find out how to convert signals from SDA and SCL.

When I use example code below I can see datas in serial monitor:

#include <MPU6050_tockn.h>
#include <Wire.h>

MPU6050 mpu6050(Wire);

void setup() {
  Serial.begin(9600);
  Wire.begin();
  mpu6050.begin();
  mpu6050.calcGyroOffsets(true);
}

void loop() {
  mpu6050.update();
  Serial.print("angleX : ");
  Serial.print(mpu6050.getAngleX());
  Serial.print("\tangleY : ");
  Serial.print(mpu6050.getAngleY());
  Serial.print("\tangleZ : ");
  Serial.println(mpu6050.getAngleZ());
}

But what is the correct way to convert them and use as xAxis in my code?
Example of my code below, how to exchange "analogRead(A2)" for data from gyrocope from example above?


#include <Joystick.h>
#include <arduino.h>

Joystick_ Joystick;

int xAxis_ = 0;

const bool initAutoSendState = true;

void loop() {

  //LEFT - RIGHT
 xAxis_ = analogRead(A2);
 xAxis_ = map(xAxis_,0,1023,0,1023);
 Joystick.setXAxis(xAxis_);
  
  delay (50);
}

I will be glad for every help.
Thank you in advance.

As I'm not familiar with the MPU6050, what range of values do you get for (mpu6050.getAngleX()?

The function returns a float; in its roughest form, you can assign it to an int.

xAxis_ = mpu6050.getAngleX();

Next you can adjust the mapping.

Thank you for advice. Anyway I was not succesful. No errors, but when I check Gamepad device I do not see anything move.
Range of data on axis I collect is -90,90

My code now looks like this:

#include <Joystick.h>
#include <MPU6050_tockn.h>
#include <Wire.h>

Joystick_ Joystick;
MPU6050 mpu6050(Wire);

void setup() {
  Serial.begin(9600);
  Wire.begin();
  mpu6050.begin();
  mpu6050.calcGyroOffsets(true);
}

int xAxis_ = 0;

const bool initAutoSendState = true;

void loop() {

  //LEFT - RIGHT
xAxis_ = mpu6050.getAngleX ();
xAxis_ = map(xAxis_,-80,80,0,1023);
 Joystick.setXAxis(xAxis_);
  
  delay (50);
}

By assig to int, you mean what? Sorry, I am not really experienced in programing, I am more mechanical guy, but I need to learn this to finish project.
I was trying to look everywhere for example code, but can not find anythig suitable for me.

Thanks

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