Loading...
Logo
Processing Forum
doug's Profile
1 Posts
3 Responses
0 Followers

Activity Trend

Last 30 days
Show:
Private Message
    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:
    1. const int xpin = A0; // x-axis of the accelerometer
    2. const int ypin = A3; // y-axis

    3. void setup(){ 
    4.   // initialize the serial communications: 
    5.   Serial.begin(9600);
    6. }

    7. void loop(){ 
    8.   // print the sensor values: 
    9.   Serial.print(analogRead(xpin)); 
    10.   Serial.print(",");  
    11.   Serial.print(analogRead(ypin)); 
    12.   Serial.println(); // print out linefeed 
    13.   // delay before next reading: 
    14.   delay(100);
    15. }
    Here is the code in Processing:
    1. import processing.serial.*;

    2. Serial myPort;  // The serial port
    3. int xVal;
    4. int yVal;
    5. int lf = 10;
    6. String inString;
    7. int[] xList;
    8. float xAng,yAng;

    9. void setup() {
    10.   size(400,400,P3D);
    11.   println(Serial.list());
    12.   myPort = new Serial(this, Serial.list()[0], 9600);
    13.   myPort.bufferUntil(lf);
    14.   stroke(255);
    15. }

    16. void draw() {
    17.   background(0);
    18.   text("received: " + inString, 10,50);
    19.   xList = int(inString.split(","));
    20.   println("String = "+ inString);
    21.   println("Array = "+xList[0]+","+xList[1]);
    22.   xAng = map(xList[0],350,560,0,PI*2);
    23.   directionalLight(51,102,126,+1,0,-1);
    24.   translate(width/2, height/2, 0); 
    25.   fill(200);
    26.   rotateX(xAng);
    27.   box(80);
    28. }

    29. void serialEvent(Serial p) {
    30.   inString = myPort.readString();
    31. }
    Here is the resulting ouput from the console:
    1. String = 442,465
    2. Array = 442,0
    I can't figure out why xList[1] is 0. Ive tried this code as a test and it works fine.
    1. String myString = "100,200";
    2. int[] xList;
    3. xList = int(split(myString,','));
    4. println("X: "+xList[0]);
    5. println("Y: "+xList[1]);