Gyroscope Keyboard Controller Problem??

Hi.

I am new at programming arduino boards.

What i want to do is programming my arduino uno board with mpu6050 to control my keyboard with X Y Z angle changes.

For example:

If X angle is bigger than 10 degrees, press “A” button. If it is less than 10 degrees, release “A” button etc…

I found some arduino code to work my mpu6050 and i see X Y Z results at serial monitor.

So i found some Processing 3 code to work my command.

But procesing ide see X Y Z results as zero all the time…

Can you please help me about my codes, if i am making anything wrong?

Thanks in advance…

MY ARDUINO CODE IS HERE:

#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(115200);
}
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)

int valX=analogRead(AcX); // reads the Analog Input, t.e the value from the X - axis from the accelerometer
Serial.print(AcX); // sends that value into the Serial Port
Serial.print(","); // sends addition character right next to the read value needed later in the Processing IDE for indexing

int valY=analogRead(AcY);
Serial.print(AcY);
Serial.print("/");

int valZ=analogRead(AcZ);
Serial.print(AcZ);
Serial.print(";");

delay(30);

}

MY PROCESSING IDE CODE IS HERE:

/*

*/

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 port; // 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);
port = new Serial(this,“COM4”, 115200); // starts the serial communication
port.bufferUntil(’.’); // reads the data from the serial port 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>320)
{
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<=320){
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<280 )
{
delay(40);
robot.keyPress(KeyEvent.VK_L);
}

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

// turn up

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

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

// turn down

if( iY<280 )
{
delay(40);
robot.keyPress(KeyEvent.VK_K);
}
if(iY>=280){
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);

}

Serial.print(AcX);     // sends that value into the Serial Port
  Serial.print(",");     // sends addition character right next to the read value needed  later in the Processing IDE for indexing
 
  int valY=analogRead(AcY);
  Serial.print(AcY);
  Serial.print("/");

 
  int valZ=analogRead(AcZ);
  Serial.print(AcZ);
  Serial.print(";");

So, you are sending an int, a comma, an int, a slash, an int, and a semicolon.

port.bufferUntil('.'); // reads the data from the serial port up to the character '.'.

And, yet, you expect the serialEvent method to be called when a period arrives. Hmmm...

I made terrible mistakes. And corrected them all. Now the code works without any problems. Thanks. : )

can you please upload the code? im trying to make this a minecraft controller. also I'm on mac and it doesnt seem to take any COM port I select in processing