Need help on Arudino accelerometer + 3D processing code.

I already finish out the accelerometer code on angle calculation with calibration on it. And i also Google out the code to run the 3D cube on processing, but the problem is how do i interface arduino with processing?

Arduino Code:

#include <math.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>

#define O0 11
#define O1 10
#define O2 9
#define O3 6
#define O4 5
#define O5 3
#define I0 A0
#define I1 A1
#define I2 A2
#define I3 A3
#define I4 A4
#define I5 A5
// These constants won't change. They're used to give names
// to the pins used:

const int analogInPin1 = I0; // Analog input pin that the Accelerometer's first pin is attached to
const int analogInPin2 = I1; // Analog input pin that the Accelerometer's second pin is attached to
const int analogInPin3 = I2; // z-axis

// value read from the Accelerometer's first pin
float ValueX,ValueY,ValueZ; 
float gX,gY,gZ;
float aX, aY, aZ;
float vdd=4.91;     //the measured 5V supply. Meter reading is more accurate
//sensitivity from datasheet is 0.5V/g. unit of sens is g/value. 1024=vdd
float sens=(vdd/1024)/0.5000; 
float sensX=sens;  //without calibration, default value is from the datasheet
float sensY=sens;
float sensZ=sens;
float mid=1023/2;
float midX=mid;
float midY=mid;
float midZ=mid;

struct calData_t
{ int   written; //99 if written
  float mx,my,mz,sx,sy,sz;
} ;

calData_t calData;

int noAvg=10; //averaging of 10 reading
int keyResponse;

float readAcc(float &ValueX,float &ValueY, float &ValueZ, char One);
void calOne(char One, float &mid, float &sens);

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  Serial.println("Hit 'y' and RETURN, if you want to calibrate...");
  Serial.println("Hit 'x' and RETURN, if you want to clear the cal data in EEPROM...");
  Serial.println("If not, 'n' and RETURN");
  keyResponse=readkey();
  if (keyResponse=='y'){
      calibration();
  }
  else if (keyResponse=='x'){
    while (!eeprom_is_ready()); // Wait for EEPROM to be ready
    cli();
    calData.written=0;
    eeprom_write_block((const void*)&calData, (void*)0, sizeof(calData));
    Serial.println("Cal data is cleared...");
    sei();    
  }
  else {
     while (!eeprom_is_ready()); // Wait for EEPROM to be ready
     cli();
     eeprom_read_block((void*)&calData, (void*)0, sizeof(calData));
     sei();
     if (calData.written==99){
       Serial.println("Using previously stored cal data from EEPROM");
       midX=calData.mx; midY=calData.my; midZ=calData.mz;
       sensX=calData.sx;sensY=calData.sy;sensZ=calData.sz;
     }
     else {
        Serial.println("no previous cal data, using default");
     }
     dispCalData();
  }
}

int readkey() {
  // read 1 char:
  int c;
  while(!Serial.available());   
    c= Serial.read(); //read 1 byte ?only read 1 byte 
    delay(100);
    while(Serial.available()>0){   
      Serial.read();  //keeping reading. Clear the buffer until Serial.available()==0
    }
  return c;
}

void calibration(){
  calOne('z',midZ,sensZ);
  calOne('x',midX,sensX);
  calOne('y',midY,sensY);
  dispCalData();
  storeCalData();
}

void storeCalData(){
  calData=(calData_t){99 , midX,midY,midZ,sensX,sensY,sensZ};
  while (!eeprom_is_ready()); // Wait for EEPROM to be ready
  cli();
  eeprom_write_block((const void*)&calData, (void*)0, sizeof(calData));
  Serial.println("Cal data written to EEPROM...");
  sei();
}

void dispCalData(){
  Serial.println("The current middle value and sensitivity are: ");
  Serial.print("sensX:");
  Serial.print(sensX,5);
  Serial.print("\tsensY:");
  Serial.print(sensY,5);
  Serial.print("\tsensZ");
  Serial.print(sensZ,5);
  Serial.print("\tsens without cal:");
  Serial.println(sens,5);
  Serial.print("midX:");
  Serial.print(midX,5);
  Serial.print("\tmidY:");
  Serial.print(midY,5);
  Serial.print("\tmidZ");
  Serial.print(midZ,5);
  Serial.print("\tmid without cal:");
  Serial.println(mid,5);  
  delay(5000);
}

void calOne(char One, float &mid, float &sens){
  //when you're cal z=+1g, you could cal for x=y=0g
  //this only need to perfm 6 cal instead of 9 cal
  //however, it is more messy in the code and hard to trace if there is cal error.
  float Pos1g=0, Neg1g=0;
  String String1=String(One)+"=0g cal: put "+String(One)+                "=0g and hit 'y' & RETURN";
  String String2=String(One)+"=+1g cal: put +"+String(One)+ " facing downwards and hit 'y' & RETURN";
  String String3=String(One)+"=-1g cal: put +"+String(One)+ " facing   upwards and hit 'y' & RETURN";
  Serial.println(String1);
  keyResponse=readkey();
  if (keyResponse=='y'){
    delay(1000);
    mid=readAcc(ValueX,ValueY,ValueZ,One);
  }
  Serial.println(String2);
  keyResponse=readkey();
  if (keyResponse=='y'){
    delay(1000);
    Neg1g=readAcc(ValueX,ValueY,ValueZ,One); 
  }
  Serial.println(String3);
  keyResponse=readkey();
  if (keyResponse=='y'){
    delay(1000);
    Pos1g=readAcc(ValueX,ValueY,ValueZ,One); 
  }  
  if (Pos1g==0 || Neg1g==0){}
  else {sens=2/(Pos1g-Neg1g);}  
}
float readAcc(float &ValueX,float &ValueY, float &ValueZ, char One){
  float X=0; float Y=0; float Z=0;
  noAvg=100;
  for (int i=0; i < noAvg; i++){  //int is 16bit
    X = X+analogRead(analogInPin1);
    Y = Y+analogRead(analogInPin2);
    Z = Z+analogRead(analogInPin3);
    delay(10); 
  } 
  ValueX=X/noAvg;
  ValueY=Y/noAvg;
  ValueZ=Z/noAvg;  
  if (One == 'x')     { return ValueX;}
  else if (One == 'y'){ return ValueY;}
  else                { return ValueZ;}
}
float mapAcc(float x, float mid, float sens){
  return sens*(x-mid);
}

void loop() {
  // read the both analog in values:
  readAcc(ValueX,ValueY,ValueZ,'x');
  gX = mapAcc(ValueX, midX, sensX);
  gY = mapAcc(ValueY, midY, sensY);
  gZ = mapAcc(ValueZ, midZ, sensZ);
  aX=asin(constrain(gX, -1,1))*180/M_PI; //convert from rad to degree
  aY=asin(constrain(gY, -1,1))*180/M_PI;
  aZ=asin(constrain(gZ, -1,1))*180/M_PI;
  // print the results to the serial monitor:
  Serial.print("X=" );
  Serial.print(ValueX,5);
  Serial.print(" Y=" );
  Serial.print(ValueY,5);
  Serial.print(" Z=" );
  Serial.print(ValueZ,5);
  Serial.print("\tgX=" );
  Serial.print(gX,5);
  Serial.print(" gY=" );
  Serial.print(gY,5);
  Serial.print(" gZ=" );
  Serial.print(gZ,5);
  Serial.print("\taX=" );
  Serial.print(aX,5);
  Serial.print(" aY=" );
  Serial.print(aY,5);
  Serial.print(" aZ=" );
  Serial.print(aZ,5);
  Serial.print("\t " );
  Serial.print(vdd);
  Serial.println(' ');
  //datasheet suggest 10ms delay for ADC to settle
  delay(900);
}

Processing Code:

import processing.serial.*;
 
Serial port;  // Create object from Serial class
int val_pitch;      // Data received from the serial port
int val_roll;
//int val_yaw;

int[] values_pitch;
int[] values_roll;
//int[] values_yaw;
int cubesize = 50; 
int angleX=0;
int angleY=0;
int angleZ=0;
void setup() 
{
size(500, 500, P3D); 
noStroke(); 
 port = new Serial(this, "COM19", 9600);
  values_pitch = new int[width];
  values_roll = new int[width];

  smooth(); 
}
 
void draw() 
{
  background(100); 
  translate(250,250,0);   
  rotateZ(((float)angleZ)*180/PI);
  rotateX(((float)angleX)*180/PI);
  rotateY(((float)angleY)*180/PI);
pushMatrix(); 

//Front
beginShape();
fill(255,0,0);
vertex(-cubesize, -cubesize, cubesize, 0, 0); 
vertex(cubesize, -cubesize, cubesize, 0, 1); 
vertex(cubesize, cubesize, cubesize, 1, 1);
vertex(-cubesize, cubesize, cubesize, 1, 0);
endShape();
popMatrix(); 

//Back
pushMatrix();
beginShape();
fill(0,255,0);
vertex(cubesize, -cubesize, -cubesize, 0, 0);
vertex(-cubesize, -cubesize, -cubesize, 0, 1);
vertex(-cubesize, cubesize, -cubesize, 1, 1);
vertex(cubesize, cubesize, -cubesize, 1, 0);
endShape();
popMatrix();

//Top
pushMatrix();
beginShape();
fill(0,0,255);
vertex(-cubesize, cubesize, cubesize, 0, 0);
vertex(cubesize, cubesize, cubesize, 0, 1);
vertex(cubesize, cubesize, -cubesize, 1, 1);
vertex(-cubesize, cubesize, -cubesize, 1, 0);
endShape();
popMatrix();

//Bottom
pushMatrix();
beginShape();
fill(255,0,255);
vertex(cubesize, -cubesize, cubesize, 0, 0);
vertex(-cubesize, -cubesize, cubesize, 0, 1);
vertex(-cubesize, -cubesize, -cubesize, 1, 1);
vertex(cubesize, -cubesize, -cubesize, 1, 0);
endShape();
popMatrix();

//Right
pushMatrix();
beginShape();
fill(255,255,0);
vertex(cubesize, -cubesize, cubesize, 0, 0);
vertex(cubesize, -cubesize, -cubesize, 0, 1);
vertex(cubesize, cubesize, -cubesize, 1, 1);
vertex(cubesize, cubesize, cubesize, 1, 0);
endShape();
popMatrix();    

//Left
pushMatrix();
beginShape();
fill(0,255,255);
vertex(-cubesize, -cubesize, -cubesize, 0, 0);
vertex(-cubesize, -cubesize, cubesize, 0, 1);
vertex(-cubesize, cubesize, cubesize, 1, 1);
vertex(-cubesize, cubesize, -cubesize, 1, 0);
endShape();
popMatrix();
 
 
while (port.available() >= 3){
    int val= port.read();
    println(val);
    if (val == 0xff) {    
     val_pitch = (port.read() << 8) | (port.read()); 
    }
    else if (val == 0xfe) {
     val_roll = (port.read() << 8) | (port.read());
  }
}
angleX = val_pitch/4;
angleY = val_roll/4; 
}

Anyone? =(

Fufu:
how do i interface arduino with processing?

You seem to have code to do this already - what's the problem?

It seem the cube won't move when i run the processing. But if i run arduino, it will ask whether want to calibrate all 6 orientation or not.

You keep hinting at completely unrelated problems and I suspect this is because you have not got a clear picture of which parts of your solution are working.

Is the Arduino generating the data correctly and writing it to the serial port correctly? Until that is working, you are wasting your time trying to have Processing do anything with it. Once it's working you can forget about the Arduino and you just need to sort out the Processing app to handle the data correctly.

PeterH:
You keep hinting at completely unrelated problems and I suspect this is because you have not got a clear picture of which parts of your solution are working.

Is the Arduino generating the data correctly and writing it to the serial port correctly? Until that is working, you are wasting your time trying to have Processing do anything with it. Once it's working you can forget about the Arduino and you just need to sort out the Processing app to handle the data correctly.

My arduino is already generating the data correctly, now i'm trying to sort out the processing app.

Is the Arduino writing the data values to the serial port in your chosen format?

Is the processing app receiving the same byte stream from the serial port that the Arduino is sending?

Can the processing app make sense of the byte stream and extract the original data values from it?

I did a small testing using the code below, the processing app able to read the data value from the serial port and the cube is able to rotate but is not very accurate. I believe is the line that i have just added in: serial.write. Allow arduino communicate with processing app.

But i try to add it on my arduino coding , it give me the error: invalid operands of types 'float' and 'int' to binary 'operator&' .

#define X_AXIS 0
#define Y_AXIS 1
#define Z_AXIS 2
 
void setup() {
  Serial.begin(9600); 
}
 
void loop() {
  int x = analogRead(X_AXIS);
  int y = analogRead(Y_AXIS);
  int z = analogRead(Z_AXIS);
  
  Serial.write( 0xff);
  Serial.write( (x >> 8) & 0xff);
  Serial.write( x & 0xff);
  Serial.write( 0xff);
  Serial.write( (y >> 8) & 0xff);
  Serial.write( y & 0xff);
  Serial.write( 0xfe);
  Serial.write( (z >> 8) & 0xfe);
  Serial.write( z & 0xfe);
  delay(100);
}

Fufu:
But i try to add it on my arduino coding

I thought you said the Arduino part was working correctly. So now we're back to having no idea which bits of your project are working correctly.

Decide what format you're going to use to send the data over the serial interface, write your Arduino sketch so that it gets the correct sensor values and sends them over the serial port in the correct format, and get that working correctly before you try to do anything on the Processing side. Once the Arduino side is working correctly, leave it alone.

I've been trying to use the serial write method on my arduino code but it seem to be error. How can i solve this? =(

Fufu:
it seem to be error

Then you probably want to fix the error, or tell somebody about it so they can advise you how to fix it.

Yes i want to fix the error. My arduino coding is working but it just processing app can't receive the data. Did i miss out something? Can someone take a look at the coding?

Fufu:
Can someone take a look at the coding?

I don't see how, since you haven't posted the code with the problem, or any details about what the problem is.