Well, I got the MPU working! And here is the code:
/* Tilt Control - King Dub Dub, 11/29/19
A tilt-controller script for USB-HID enabled Arduino boards with the MPU-6050 sensor.
Tilt to control the WASD controls and spacebar (JK I lied about the W, it presses [space]).
There is an additional fail-safe that keeps any keyboard data from being printed if pin 9
isn't grounded. Ground pin 9 with a switch/button when you want to send keyboard inputs.
MPU6050 - Arduino
VCC to 5V
GND to GND
SCL to A5 or SCL
SDA to A4 or SDA
ADO to GND
INT to digital pin 2
Pin 9 to button/switch to ground
*/
#include<Wire.h>
#include<Keyboard.h>
const int MPU_addr=0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ; //sets up the accelerometer XYZ's, temperature, and gyro XYZ's
int minVal=265;
int maxVal=402;
double x,y,z;
int MPUFail = 0;
void setup(){
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(9600);
pinMode(9, INPUT_PULLUP); //pin 9 is a fail-safe switch
Keyboard.begin();
}
void loop(){
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
int xAng = map(AcX,minVal,maxVal,-90,90);
int yAng = map(AcY,minVal,maxVal,-90,90);
int zAng = map(AcZ,minVal,maxVal,-90,90);
x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);
if ((AcX == -1) and (AcY == -1) and (AcZ == -1)){
MPUFail = 1; //if the Arduino can't connect to the MPU, all raw values come out as -1,
} else { // so this is another fail-safe that stops it from pressing keys.
MPUFail = 0;
}
Serial.print("X= "); Serial.print("N/A"); //the X value doesn't work on our axis
Serial.print(" Y= "); Serial.print(y); //print the Y value
Serial.print(" Z= "); Serial.print(z); //print the Z value
Serial.print(" Safety: ");
if(digitalRead(9) == 0){ //prints if safety button is on
Serial.print("Off");
} else {
Serial.print("On");
}
Serial.print("\n"); //prints a new line
if ((y >=130) and (digitalRead(9) == 0) and (MPUFail == 0)){ //if tilted left:
Keyboard.press('a'); //"press" the [a] key
} else {
Keyboard.release('a'); //else, stop "pressing" the [a] key
}
if ((z <=335) and (z >= 180) and (digitalRead(9) == 0) and (MPUFail == 0)){ //if tilted down:
Keyboard.press('s');
delay(10);
} else {
Keyboard.release('s');
}
if ((y<=60) and (digitalRead(9) == 0) and (MPUFail == 0)){ //if tilted right:
Keyboard.press('d');
delay(10);
} else {
Keyboard.release('d');
}
if ((z >= 35) and (z<=179) and (digitalRead(9) == 0) and (MPUFail == 0)){ //if tilted up:
Keyboard.press(' '); //sends [spacebar] key
delay(10);
} else {
Keyboard.release(' ');
}
delay(10);//keeps a regulated speed
}
Here's an image of the glove I constructed rather then spend Thanksgiving with my family:
It is a little finicky, but I am a fast learner, so I just need to get the hang of using it. I hope to get the fingers working so I can run, shoot fireballs, climb vines, and maybe code it for other games as well. I might make a new showcase post if this works out!