Im writing a processing program to display data from an accelerometer using P3D. The data is sent over serial from a Launchpad in the format "xAngle,yAngle". Im trying to split the string to an int[] but the result of the the array always leaves the second element of the array blank.
Here is the code from the Launchpad:
const int xpin = A0; // x-axis of the accelerometer
const int ypin = A3; // y-axis
void setup(){
// initialize the serial communications:
Serial.begin(9600);
}
void loop(){
// print the sensor values:
Serial.print(analogRead(xpin));
Serial.print(",");
Serial.print(analogRead(ypin));
Serial.println(); // print out linefeed
// delay before next reading:
delay(100);
}
Here is the code in Processing:
import processing.serial.*;
Serial myPort; // The serial port
int xVal;
int yVal;
int lf = 10;
String inString;
int[] xList;
float xAng,yAng;
void setup() {
size(400,400,P3D);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil(lf);
stroke(255);
}
void draw() {
background(0);
text("received: " + inString, 10,50);
xList = int(inString.split(","));
println("String = "+ inString);
println("Array = "+xList[0]+","+xList[1]);
xAng = map(xList[0],350,560,0,PI*2);
directionalLight(51,102,126,+1,0,-1);
translate(width/2, height/2, 0);
fill(200);
rotateX(xAng);
box(80);
}
void serialEvent(Serial p) {
inString = myPort.readString();
}
Here is the resulting ouput from the console:
String = 442,465
Array = 442,0
I can't figure out why xList[1] is 0. Ive tried this code as a test and it works fine.