Arduino + accelerometro + processing

qualcuno potrebbe aiutarmi?

sto cercando di usare un accellerometro (memsic) per cpntrollare un oggeto(o anche la posizione del mouse) in processing…

ho usato sto codice per arduino

int xpin = 2;
int ypin = 3;

void setup()
{
Serial.begin(9600);
pinMode(xpin, INPUT);
pinMode(ypin, INPUT);
}

void loop()
{
int pulseX, pulseY;
int accX, accY;

// read pulse from x- and y-axes
pulseX = pulseIn(xpin,HIGH);
pulseY = pulseIn(ypin,HIGH);

// convert the pulse width into acceleration
// accX and accY are in milli-g’s: earth’s gravity is 1000.
accX = ((pulseX / 10) - 500) * 8;
accY = ((pulseY / 10) - 500) * 8;

// print the acceleration
Serial.print(accX);
Serial.print(" ");
Serial.print(accY);
Serial.println();

delay(100);
}

e questo per processing

import processing.serial.*;

Serial port;
String portname = “/dev/tty.usbserial-A6008cwp”; // find the name of your serial port in your system setup!
int baudrate = 9600; // set baudrate here
int value; // variables used to store value from serial port
String buf=""; // String buffer to store serial values
//lastValue, averageX and averageY is used for smoothening the fluctuation of the sensor
int lastValue = 0;
int averageX = 0;
int averageY = 0;
//xSign and ySign contains + or -
String xSign = " ";
String ySign = " ";
boolean isXpositive = false;
boolean isYpositive = false;
//the coordinates of the upper left corner of the square/rectangle
int coordinateX = 0;
int coordinateY = 0;
//Corrections that keep the rectangle centered, these values may differ between different sensors
int correctionX = 85;
int correctionY =85;

boolean actBuff;

String buffA="";
String buffB="";
int myValue_1 = 0;
int myValue_2 = 0;

void setup(){

size(400,400);

port = new Serial(this, portname, baudrate);
println(port);
}

void drawRect(){
if(isXpositive){
coordinateX = ((width/2)+25)+(averageX/2)-correctionX;
//keep the square inside of the frame
if(coordinateX > (width-50)){ coordinateX = (width-50); }
}
else{
coordinateX = ((width/2)-25)-(averageX/2)-correctionX;
//keep the square inside of the frame
if(coordinateX < 0){ coordinateX = 0; }
}
if(isYpositive){
coordinateY = ((height/2)+25)+(averageY/2)-correctionY;
//keep the square inside of the frame
if(coordinateY > (height-50)){ coordinateY = (height-50); }
}
else{
coordinateY = ((height/2)-25)-(averageY/2)-correctionY;
//keep the square inside of the frame
if(coordinateY < 0){ coordinateY = 0; }
}
//draw the square
rect(coordinateX, coordinateY, 50, 50);
}

//method used for smoothening the changes of the values
int getAverage(int newValue){
lastValue += newValue;
lastValue /=2;

return lastValue;
}

// the serial event function takes the value of the event and store it in the corresponding variable
void serialEvent(int serial){
if(serial!=’ ') {
if (serial==‘X’) actBuff = true;
if (serial==‘Y’) actBuff = false;
if (actBuff){
if (serial!=‘X’) buffA += char(serial);
}else{
if (serial!=‘Y’) buffB+= char(serial);
}

}
else {

if (actBuff){
//Extract the + or - from the buffer and toggle the isXpostive true/false
xSign = buffA.substring(0,1);
if(xSign.equals("+")){
isXpositive = true;
}
else{
isXpositive = false;
}
//Extract the value from the buffA string and parse to integer
buffA = buffA.substring(1,buffA.length());
myValue_1 = Integer.parseInt(buffA);
buffA="";
averageX = getAverage(myValue_1);
}
else {
//Extract the + or - from the buffer and toggle the isYpostive true/false
ySign = buffB.substring(0,1);
if(ySign.equals( “+”)){
isYpositive = true;
}
else{
isYpositive = false;
}
//Extract the value from the buffB string and parse to integer
buffB = buffB.substring(1,buffB.length());
myValue_2 = Integer.parseInt(buffB);
averageY = getAverage(myValue_2);
buffB="";
}
}
}

void draw(){

while(port.available() > 0){
value = port.read();
serialEvent(value);
}

noStroke();
fill(0, 0, 255);

drawRect();

//Generates output

delay(100);
}

ma mia da sempre un errore su questa linea

//Extract the value from the buffB string and parse to integer
buffB = buffB.substring(1,buffB.length());
myValue_2 = Integer.parseInt(buffB);
averageY = getAverage(myValue_2);
buffB="";
}
}
dicendo NumberFormatException: for input strin “0 40”

Bene, sono riuscito a far funzionare il codice di arduino, cambiando la siantassi che era un po vecchia…

ora, non riesco a far leggere a processing i miei input…
per paicere, date un occhiata e vedete se ce qualcosa di sbagliato, magari devo aqnche qui usare una nuova sintassi per alcune linee…

ho vermanete bisogno di aiuto

/*
*  processing meets arduino
*  
*  pa_Read_accelerometer
*  This is an example showing how to use an accelerometer with processing.
*  use the accelerometer to control the direction of a square on the screen.
*  The accelerometer is mounted to an Arduino-board. The Arduino-software
*  used is the ap_ReadAccelerometer.pde created by Markos Yarza, K3 Malm[ch65533] university
*
*  created by Anders Gran and Jacob Holst, december 2005
*
*/

import processing.serial.*;

  DisplayItems di;  // backgroundcolor, grid, etc. are controlled in the DisplayItems object
  // width and height should be set here
  int xWidth = 400;
  int yHeight = 400;

  // set framerate
  int fr = 24;
  
  // set up the display items you want by choosing true
  boolean bck = true;
  boolean grid = true;
  boolean g_vert = true;
  boolean g_horiz = true;
  boolean g_values = false;
  boolean output = true;
  
  // these variables are for the serial port connection object
  Serial port;
  String portname = "/dev/tty.usbserial-A6008cwp";  // find the name of your serial port in your system setup!
  int baudrate = 9600;  //  set baudrate here
  int value;  // variables used to store value from serial port
  String buf=""; // String buffer to store serial values
  //lastValue, averageX and averageY is used for smoothening the fluctuation of the sensor
  int lastValue = 0;
  int averageX = 0;
  int averageY = 0;
  //xSign and ySign contains + or - 
  String xSign = " ";
  String ySign = " ";
  boolean isXpositive = false;
  boolean isYpositive = false;
  //the coordinates of the upper left corner of the square/rectangle
  int coordinateX = 0;
  int coordinateY = 0;
  //Corrections that keep the rectangle centered, these values may differ between different sensors
  int correctionX = 85;
  int correctionY =85;
  
  boolean actBuff;
  
  String buffA="";
  String buffB="";
  int myValue_1 = 0;
  int myValue_2 = 0;
 
// pressing these keys you can toogle on/off display items
void keyPressed(){
      if (key == 'b' || key == 'B') bck=!bck; 
      if (key == 'g' || key == 'G') grid=!grid; 
      if (key == 'v' || key == 'V') g_values=!g_values; 
      if (key == 'o' || key == 'O') output=!output; 
}
void setup(){
  
      size(xWidth, yHeight);
      frameRate(fr);

      di = new DisplayItems();

      port = new Serial(this, portname, baudrate);
      println(port);
}
//Method creating the rectangle, calculating positions
//Checking which direction to move and 
//Adding constraints to keep within the framesize
  
void drawRect(){
 if(isXpositive){
   coordinateX = ((width/2)+25)+(averageX/2)-correctionX;
   //keep the square inside of the frame
   if(coordinateX > (width-50)){ coordinateX = (width-50); }
 }
 else{
   coordinateX = ((width/2)-25)-(averageX/2)-correctionX;
   //keep the square inside of the frame
   if(coordinateX < 0){ coordinateX = 0; }
 }  
 if(isYpositive){ 
   coordinateY = ((height/2)+25)+(averageY/2)-correctionY;
    //keep the square inside of the frame
    if(coordinateY > (height-50)){ coordinateY = (height-50); }
 } 
 else{  
      coordinateY = ((height/2)-25)-(averageY/2)-correctionY; 
      //keep the square inside of the frame
      if(coordinateY < 0){ coordinateY = 0; }
 }
    //draw the square
   rect(coordinateX, coordinateY, 50, 50);
}

//method used for smoothening the changes of the values
int getAverage(int newValue){
    lastValue += newValue;
    lastValue /=2;
  
    return lastValue; 
}



// the serial event function takes the value of the event and store it in the corresponding variable
void serialEvent(int serial){
  if(serial!=' ') {
       if (serial=='X') actBuff = true;
       if (serial=='Y') actBuff = false;
       if (actBuff){
          if (serial!='X') buffA += char(serial);
          }else{
          if (serial!='Y') buffB+= char(serial);
          }
          
       }
       else {
        
        if (actBuff){
          //Extract the + or - from the buffer and toggle the isXpostive true/false
          xSign = buffA.substring(0,1);
          if(xSign.equals("+")){
            isXpositive = true;
          }
          else{
            isXpositive = false;
          }
          //Extract the value from the buffA string and parse to integer
          buffA = buffA.substring(1,buffA.length());
          myValue_1 = Integer.parseInt(buffA);
          buffA="";
          averageX = getAverage(myValue_1); 
          } 
          else {
            //Extract the + or - from the buffer and toggle the isYpostive true/false
           ySign = buffB.substring(0,1);
           if(ySign.equals( "+")){
              isYpositive = true;
            }
            else{
              isYpositive = false;
            }
          //Extract the value from the buffB string and parse to integer
          buffB = buffB.substring(1,buffB.length());
          myValue_2 = Integer.parseInt(buffB);
          averageY = getAverage(myValue_2); 
          buffB="";
          }
       }
}


// setup initializes displayItems and serial port objects

// draw listens to serial port, draw 
void draw(){
  
  while(port.available() > 0){
        value = port.read();
        serialEvent(value);
    }
    
    //myValue_1=port.read();

    di.drawBack();

    noStroke();
    fill(0, 0, 255);
    
    drawRect();
    
    di.drawItems();  
    
    //Generates output
    if(output) println("X= "+xSign+myValue_1 + "-- Y= "+ ySign + myValue_2);
    delay(100);
}