Arduino-accelrometer-processing HELPPP!!!!

Hi, i am working on a project in processing. I would like to be able to control my mouse using the mesic accelerometer from Radio Shack...

i uploaded this code on my arduino board, so i get the reading out of the accelerometer, now how can i tell processing to import those reading to control the position of the mouse, or control the postion of a shape inside my sketch?

THIS IS THE CODE FOR 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);
}

THIS(below) IS FOR 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);
}

but i always get an error

NumberFormatException: for input string "0 40"

anyone help?

Sounds like you've got a divide by zero problem.
Arduino code looks OK.
Check your averaging.
Check your parsing of the (virtually) motionless string "0 0".

Lots of 25s, 50s and "/2"s in there - what's that all about?

I dont know, i am using a code i did not write, can anyone break it down and make it more simple?

this is my first project using arduino

I don't think the problem is with the arduino code.
You need to examine the processing code more carefully; break it down and possibly simplify it.

Start simple - you look like you've grabbed a whole lot of code from somewhere,and expect it to work first time.

Your arduino seems to be doing its thing, producing pairs of integers separated by a space.

So, ignore the Arduino, look at your processing code, and get it to parse strings containing pairs of integers.

Check what happens when one of those integers is zero.

the arduino code writes a value for X and Y from 0 to +/- 1000

how can i tell processing to import these values and apply them to the x/y position of a rectangle inside my processing sketch?

You must have had some idea of what you wanted to do when you wrote it?

that's the point... i didn't write it... i am just a beginner

With an Arduino, me too.

So, what bits don't you understand? What programming experience have you got?

Where did you get the processing code from, and what was it meant to do?

  1. i really tried to simplify it, but there are bunch of things that I dont know what they are meant for...
    i really just wanted to have processing import the two integers and sppli them to the x/y posyion of an object i draw in processing.

i need processing to import the integers, translate them in order to have them working for the width and height of my sketch, and apply the tow integers to the x y position of my shape(rect(x,y,100,100):wink:

is there a very very simple way to do that?

this is all i need, i really have no idea how to do this!

Start from the beginning.
You need to get pairs of decimal integers, expressed as ASCII strings into integer variables.

Start from there - ignore the drawing stuff, ignore the averaging stuff.

You realise that an accelerometer doesn't work like a mouse?

A mouse provides updates when it is moving even at constant velocity.

An accelerometer moving at constant velocity produces zero output (apart from acceleration due to gravity, straight down).
To produce outputs for constant velocity, you need to integrate over time.

It might benefit you a lot to break up the code into chunks, aka lets not look at the whole thing. Lets see if processing is getting the right values but printing the inputs right back out as outputs. Once that's ok, step it up and check the outputs of whatever your algorithm is to transforming the input data to usable data, and make sure the numbers make sense to you. Once you have that nailed down, put on the pretty interface and hope it works, if it doesn't start debugging.

EDIT:
AWOL beat me too it....

What Matthias said - work on a routine to input a string, then another that can read, say a string of "-13" and produce a integer of minus thirteen in a given variable.
Verify that the routine works for all values, from negative "lots", through and including zero, to positive "lots".
Work up from there; don't expect a whole lot of someone else's stuff to do what you want.

i mean, i understand the code, and i worked around it a lot.. but as you said there are some stuff i dont really need...

now i am trying to get rid of non essential crap, to maybe implement later on

now getting an error in this line

//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="";
}
}
i think i reallu need this, but can be made more simple...

any suggestion?

now getting an error in this line

That's not a line, and the presence of an "else" says that it's not a complete clause either.

Your integer parsing looks over-complicated.
It could probably boil down to something like:

  String [] list = split (input, " ");
...
  int xAccel = int(list [0]);
  int yAccel = int(list [1]);

assuming "input" is a String containing two integers, separated by a space.
However, I don't think that your problem is a hardware one, so you may get some help over @ the software forum.

HTH

ok..i got over the previous problem, I had to use new syntax for some old lines...

Now, i cant get processing to read my values out of arduino, altho I am doing everything right this time..
maybe there are some old lines of code in this sketch too...

/*
*  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);
}