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?