#include <Keyboard.h>
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
MPU6050 mpu;
int16_t ax, ay, az;
int16_t gx, gy, gz;
struct MyData {
byte X;
byte Y;
};
MyData data;
void setup()
{
Serial.begin(9600);
Wire.begin();
mpu.initialize();
//pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
data.X = map(ax, -17000, 17000, 0, 255 ); // X axis data
data.Y = map(ay, -17000, 17000, 0, 255); // Y axis data
//delay(500);
Serial.print("Axis X = ");
Serial.print(data.X);
Serial.print(" ");
Serial.print("Axis Y = ");
Serial.println(data.Y);
if (data.X > 190 && data.X < 197 && data.Y > 50 && data.Y < 95) { //gesture : down
Keyboard.release('w');
Keyboard.press('s');
Serial.println("gesture 1");
}
if (data.X > 197 && data.X < 204 && data.Y > 95 && data.Y < 140) { //gesture : up
Keyboard.release('s');
Keyboard.press('w');
Serial.println("gesture 2");
}
if (data.X > 207) {//gesture : left
//Keyboard.release('d');
//Keyboard.press('a');
Serial.println("gesture 3");
}
if (data.X < 190) {//gesture : right
//Keyboard.release('a'); // If true, press the "a" key
//delay(1000); // Length of time the button is held down
//Keyboard.press('d');
//delay(5); // Delay if needed to avoid spamming the button
Serial.println("gesture 4");
}
if (data.Y > 91 && data.Y < 110) { //gesture : neutral
Keyboard.release('s');
Keyboard.release('w');
Serial.println("gesture 5");
}
}
//
Someone told me: "It looks like it's an i2c device. You can connect multiple, but you will need to make sure each one has a different address. This is done by wiring a specific pin on the device." - Literally have no idea what the wiring would look like..
Also: "Google mpu6050 datasheet. It should have some detail on i2c addressing. When you use any i2c device the arduino talks to a specific address assigned to each component. You need to make sure each sensor has a unique address. Typically the address is set with a pin on the sensor. Then in the code you will need to call each one separately." - Not really understanding how I would find out what the address is or how a pin would set the address. Not enough elaboration. Through the serial monitor and some specific code?
I'm assuming the 2nd mpu6050 would be vcc > 5v, gnd > gnd, scl > 5, sca > 6, ad0 > 9.
Am I even remotely close?
New to all of this btw.
Also for context on the code, I want one of the modules to be on my index finger (used for up and down), and the other on my thumb (left and right.)
Each MPU6050 has an address selection pin, which allows to assign different I2C addresses to each module. The default address is 0x68, the alternate address 0x69. Check your module description for setting the address. Check your library for a constructor or initializer that allows to give each instance a distinct address.
I still don't understand how to wire the 2nd mpu6050? I just hooked the ad0 to the 5v pin and the gnd to gnd pin. Still not getting any activity from the 2nd mpu6050 through the serial monitor. It does light up green though. What do I do with the SCL and SCA wires? Attach them to any of the digital pins? Please explain.
Also, I have some code I downloaded online which allowed me to test multiple mpu6050's. As stated above, not getting any activity because I don't understand the wiring. No one explains this or it's all over the place and different/inconsistent.
Attach the Arduino SCL and SDA pins to each module.
Run the i2c_scanner sample from the Wire library. It will show you the registered I2C addresses. Set the A0 jumper to Vcc or Gnd until 2 modules are found.
Declare 2 MPU objects, like
MPU6050 mpu1;
MPU6050 mpu2;
and use both in your code. Also declare related global variables for each MPU.
I'm pretty horrible at coding so I don't know how long it would take me to understand how to separate the 2 modules and apply my own separate code for each. Don't really understand how to call the 2nd mpu6050 within the code or where to.
I do want to learn code but I also want to know if this is even worth it for my application. The irony is when there's someone new usually they google this stuff up and someone already has example code online. I guess no one else has tried two of these things on a leonardo before. Sorry, just a little frustrated is all. I did want to thank you for explaining how to wire it up and how to detect the addresses. Other people I have talked too just gave me a vague description of what I needed to do. I guess that's what I'll just do then- learn.
It's not a Leonardo matter. Check your library for setting the I2C address of an instance, in a constructor or the begin() method. If none is found try other libraries.
DrDiettrich:
Attach the Arduino SCL and SDA pins to each module.
Run the i2c_scanner sample from the Wire library. It will show you the registered I2C addresses. Set the A0 jumper to Vcc or Gnd until 2 modules are found.
Declare 2 MPU objects, like
MPU6050 mpu1;
MPU6050 mpu2;
and use both in your code. Also declare related global variables for each MPU.
For the library you are using, that would be:
MPU6050 mpu1(MPU6050_ADDRESS_AD0_LOW); // AD0 pin wired to GND
MPU6050 mpu2(MPU6050_ADDRESS_AD0_HIGH); // AD0 pin wired to +5V