Accelerometer and gyro keyboard

So I’m very new to arduino and I wanted to make a controller for games like minecraft using the accelerometer and gyroscope.

Arduino Code:

// MPU-6050 Short Example Sketch
// By Arduino User JohnChi
// August 17, 2014
// Public Domain
#include<Wire.h>
const int MPU_addr=0x68; // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
void setup(){
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);
Serial.begin(9600);
}
void loop(){
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
AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
Serial.print("AcX = “); Serial.print(AcX);
Serial.print(” | AcY = “); Serial.print(AcY);
Serial.print(” | AcZ = “); Serial.print(AcZ);
Serial.print(” | Tmp = “); Serial.print(Tmp/340.00+36.53); //equation for temperature in degrees C from datasheet
Serial.print(” | GyX = “); Serial.print(GyX);
Serial.print(” | GyY = “); Serial.print(GyY);
Serial.print(” | GyZ = "); Serial.println(GyZ);
delay(333);
if(AcX>300){
Serial.print(F(“Hello World”));
}
}

Processing code:

// Arduino Game Controller Project

// Created by Dejan Nedelkovski,
// www.HowToMechatronics.com

import processing.serial.*;
import java.awt.Robot; // imports library for key press or release simulation
import java.awt.event.KeyEvent; // imports library for reading the data from the serial port
import java.io.IOException;

Serial myPort; // Create object from Serial class
Robot robot;
String X= “”;
String Y= “”;
String Z= “”;
String data= “”;
int index=0;
int index2=0;
int index3=0;
int AcX=0;
int AcY=0;
int AcZ=0;
String val; // Data received from the serial port
void setup()
{
// I know that the first port in the serial list on my mac
// is Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you’re using.
String portName = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
}

{
if ( myPort.available() > 0)
{ // If data is available,
val = myPort.readStringUntil(’\n’); // read it and store it in val
}
try
{
robot = new Robot();
}
catch (Exception e) {
e.printStackTrace();
exit();
}

delay(2000);
size (800, 800);
String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil(’|’); // reads the data from the serial myPort up to the character ‘.’. So actually it reads this: 215,214/141;315:314<316!314?315.
}

{

background(0,0,0);
fill(255, 255, 255);

//Simulating key press or release
{ // If data is available,
val = myPort.readStringUntil(’\n’); // read it and store it in val
}
// turn left

if(AcX>300)
{
delay(40);
robot.keyPress(KeyEvent.VK_J); // Simulates “I” key press if the value from the accelerometer for the X axis is greater than 320
}

if(AcX<=300){
delay(40);
robot.keyRelease(KeyEvent.VK_J); // Simulates “I” key release if the value from the accelerometer for the X axis is less than 320
}

{

data = myPort.readStringUntil(’\n’); // reads the data from the serial port up to the character ‘.’ and it sets that into the String variable “data”. So actually it reads this: 215,214/141;315:314<316!314?315.
data = data.substring(0,data.length()); // it removes the ‘.’ from the previous read. So this will be the String “data” variable: 215,214/141;315:314<316!314?315

// Finding the indexes in the data and setting the variables from the sensors by taking from the String “data” the appropriate values that are between the characters in the “data” String

index = data.indexOf("|"); // finds the index of the character “,” from the String “data” variable
X= data.substring(0, index); // sets into the variable X the string from position 0 of the hole string to where the index was. That would mean that read will be : 215

index2 = data.indexOf("/"); // finds the index of the character “/” from the String “data” variable
Y= data.substring(index+1, index2); // sets into the variable Y data the string from position where the character “,” was +1, to where the index2 was. That would mean that the read will be: 214

// We keep reading this way and that’s how we get only the numbers, the values from the sensors coming from the serial port.

index3 = data.indexOf(";");
Z= data.substring(index2+1, index3);

// Converting the String variables values into Integer values needed for the if statements above

AcX= int(X);
AcY= int(Y);
AcZ= int(Z);

}}

As you can tell its kinda cobbled together from others’ code and the processing doesn’t work. What do I do? I just want to make it so if I tilt the accelerometer/gyro one way or another it gives a keypress.

the processing is a combo of these

import processing.serial.*;

Serial myPort; // Create object from Serial class
String val; // Data received from the serial port
void setup()
{
// I know that the first port in the serial list on my mac
// is Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you’re using.
String portName = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
}
void draw()
{
if ( myPort.available() > 0)
{ // If data is available,
val = myPort.readStringUntil(’\n’); // read it and store it in val
}
println(val); //print it out in the console
}


import processing.serial.*; // imports library for serial communication
import java.awt.Robot; // imports library for key press or release simulation
import java.awt.event.KeyEvent; // imports library for reading the data from the serial port
import java.io.IOException;

Serial myPort; // defines Object Serial
Robot robot; // defines Object Robot

//defining variables

String X= "";
String Y= "";
String Z= "";
String data= "";
int index=0;
int index2=0;
int index3=0;
int iX=0;
int iY=0;
int iZ=0;

// creates new robot object

void setup()
{

try
{
robot = new Robot();
}
catch (Exception e) {
e.printStackTrace();
exit();
}

delay(2000);
size (800, 800);
String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil('.'); // reads the data from the serial myPort up to the character '.'. So actually it reads this: 215,214/141;315:314<316!314?315.
}

void draw()
{

background(0,0,0);
fill(255, 255, 255);

//Simulating key press or release

// turn left

if(iX>3320)
{
delay(40);
robot.keyPress(KeyEvent.VK_J); // Simulates "I" key press if the value from the accelerometer for the X axis is greater than 320
}

if(iX<=3320){
delay(40);
robot.keyRelease(KeyEvent.VK_J); // Simulates "I" key release if the value from the accelerometer for the X axis is less than 320
}

// turn right

if( iX<2280 )
{
delay(40);
robot.keyPress(KeyEvent.VK_L);
}

if(iX>=2280){
delay(40);
robot.keyRelease(KeyEvent.VK_L);
}

// turn up

if(iY>3320)
{
delay(40);
robot.keyPress(KeyEvent.VK_I);
}

if(iY<=3320){
delay(40);
robot.keyRelease(KeyEvent.VK_I);
}

// turn down

if( iY<2280 )
{
delay(40);
robot.keyPress(KeyEvent.VK_K);
}
if(iY>=2280){
delay(40);
robot.keyRelease(KeyEvent.VK_K);
}

}

// Reading data from the Serial Port

void serialEvent (Serial port) // starts reading data from the Serial Port
{

data = port.readStringUntil(';'); // reads the data from the serial port up to the character '.' and it sets that into the String variable "data". So actually it reads this: 215,214/141;315:314<316!314?315.
data = data.substring(0,data.length()-1); // it removes the '.' from the previous read. So this will be the String "data" variable: 215,214/141;315:314<316!314?315

// Finding the indexes in the data and setting the variables from the sensors by taking from the String "data" the appropriate values that are between the characters in the "data" String

index = data.indexOf(","); // finds the index of the character "," from the String "data" variable
X= data.substring(0, index); // sets into the variable X the string from position 0 of the hole string to where the index was. That would mean that read will be : 215

index2 = data.indexOf("/"); // finds the index of the character "/" from the String "data" variable
Y= data.substring(index+1, index2); // sets into the variable Y data the string from position where the character "," was +1, to where the index2 was. That would mean that the read will be: 214

// We keep reading this way and that's how we get only the numbers, the values from the sensors coming from the serial port.

index3 = data.indexOf(";");
Z= data.substring(index2+1, index3);

// Converting the String variables values into Integer values needed for the if statements above

iX= int(X);
iY= int(Y);
iZ= int(Z);

}

It is going to be hard for someone to fix your software without having your hardware. If you haven't done so already I would suggest proceeding like this:

  1. Connect your Arduino to the serial console and make sure you are transmitting the data as you expect
  2. Make a very simple Java program to display what is received on the serial port then connect the Arduino to the serial port and make sure it receives what you displayed in step 1.
  3. Add processing for sensor measurements.

Also, please read the following on how to properly post your code: Read this before posting a programming question ...

I know the arduino code works as I appropriated a processing code which reads the arduino serial ports and I checked and it works but I cant get the keyboard part to work