How to split incoming data from arduino

edited December 2017 in Arduino

I have a code that serial prints Arduino Esplora data and then in read in processing. It then displays the data in its print() function. I know I saw it somewhere, but I cant find out how to split the data into separate data points. Here's hos the string comes out in processing:

1,1,1,1,702,1010,20,1,1023,3,-10,12,7,135

That's the whole string. It then repeats with the next string. I need to spit that into each data point.

EDIT: This is the code for processing that is receiving the data:

import processing.serial.*;

Serial myPort;  // Create object from Serial class
String val;     // Data received from the serial port
void setup()
{

  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
}
void draw()
{
  if ( myPort.available() > 0) 
  {  // If data is available,
  val = myPort.readStringUntil('\n');         // read it and store it in val
  } 
println(val); //print it out in the console
}

Answers

  • edited December 2017

    So, I tried the split() function and it runs for a moment, then it gives me the NullPointerExeption error. Here's the code:

        import processing.serial.*;
    
        Serial myPort;  // Create object from Serial class
        String val;     // Data received from the serial port
        void setup()
        {
          String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
          myPort = new Serial(this, portName, 9600);
        }
        void draw()
        {
          if ( myPort.available() > 0) 
          {  // If data is available,
          val = myPort.readStringUntil('\n');         // read it and store it in val
          String[] data = split(val, ",");
          int button1 = Integer.parseInt(data[0]);
          int button2 = Integer.parseInt(data[1]);
          int button3 = Integer.parseInt(data[2]);
          int button4 = Integer.parseInt(data[3]);
        } 
        println(val); //print it out in the console
        }
    

    I get the NPE error on the line String[] data = split(val, ",");. How do I fix this. I looked at some pages on the error and I cant figure it out.

  • edited December 2017 Answer ✓

    Yes, you need to make sure you don't have a null object, like this:

    val = myPort.readStringUntil('\n');
    if(val!=null){
       //...your code here
    }
    

    Also make sure you have 4 tokens after parsing....

    Kf

  • edited December 2017

    Thx @kfrajer, works great. EDIT: It works great sometimes. I added the rest of the information:

    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port
    int button1 = 1;
    int button2 = 1;
    int button3 = 1;
    int button4 = 1;
    int linPot = 0;
    int liteSensor,tempSensor,mic;
    int jX,jY;
    int aX,aY,aZ;
    int jButton = 1;
    void setup()
    {
      String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
      myPort = new Serial(this, portName, 9600);
    }
    void draw()
    {
      if ( myPort.available() > 0) 
      {  // If data is available,
      val = myPort.readStringUntil('\n');
      if(val!=null){
        String[] data = splitTokens(val, ",");
          button1 = Integer.parseInt(data[0]);
          button2 = Integer.parseInt(data[1]);
          button3 = Integer.parseInt(data[2]);
          button4 = Integer.parseInt(data[3]);
          linPot = Integer.parseInt(data[4]);
          liteSensor = Integer.parseInt(data[5]);
          tempSensor = Integer.parseInt(data[6]);
          mic = Integer.parseInt(data[7]);
          jX = Integer.parseInt(data[8]);
          jY = Integer.parseInt(data[9]);
          aX = Integer.parseInt(data[10]);
          aY = Integer.parseInt(data[11]);
          aZ = Integer.parseInt(data[12]);
          jButton = Integer.parseInt(data[13]);
    
      }
    } 
    println(button1,",",button2,",",button3,",",button4,",",linPot,",",liteSensor,",",
    tempSensor,",",mic,",",jX,",",jY,",",aX,",",aY,",",aZ,",",jButton);
    }
    

    But I get a new error that iv never seen before, and, like the NPE I looked it up and didn't understand how to fix it. The error is NumberFormatException on a random line that has Integer.parseInt(data[]) in it. Please help me fix this.

    EDIT AGIAN: this is the error that it gives me: NumberFormatException: For input string: "135" The number 135 is actually anywhere between 134 to 136.

  • you can use trim() to cut (invisible) characters (e.g. space sign or line breaks) off

    also iirc you can check if it's not a number with if(a==int(a)) or so, which only works with correct numbers

  • String[] data = splitTokens(val, ",");
    button1 = Integer.parseInt(data[0]);
    

    you need to check data[] before using it. what if the first line fails, returns null or an empty array?

    kf said exactly this much earlier.

  • @chrisir the trim() in not needed because the arduino is not sending anyspaces. @koogs im not sure what you mean by "check data[]" please clarify

  • what if the first line fails, returns null or an empty array?

    if (data != null && data.length > 0) {
      ...
    }
    
  • (or data.length == 14, i guess, for that last bit.)

  • edited December 2017

    Im not sure if I put it in the right spot in my code because when I println() all the new values they are either 1's or 0's and nothing changes. Here's the code with the new lines added:

    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port
    int button1 = 1;
    int button2 = 1;
    int button3 = 1;
    int button4 = 1;
    int linPot = 0;
    int liteSensor,tempSensor,mic;
    int jX,jY;
    int aX,aY,aZ;
    int jButton = 1;
    void setup()
    {
      String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
      myPort = new Serial(this, portName, 9600);
    }
    void draw()
    {
      if ( myPort.available() > 0) 
      {  // If data is available,
      val = myPort.readStringUntil('\n');
      if(val!=null){
        String[] data = splitTokens(val, ",");
        if(data!=null&&data.length>14){             //ADDED IT HERE
          button1 = Integer.parseInt(data[0]);
          button2 = Integer.parseInt(data[1]);
          button3 = Integer.parseInt(data[2]);
          button4 = Integer.parseInt(data[3]);
          linPot = Integer.parseInt(data[4]);
          liteSensor = Integer.parseInt(data[5]);
          tempSensor = Integer.parseInt(data[6]);
          mic = Integer.parseInt(data[7]);
          jX = Integer.parseInt(data[8]);
          jY = Integer.parseInt(data[9]);
          aX = Integer.parseInt(data[10]);
          aY = Integer.parseInt(data[11]);
          aZ = Integer.parseInt(data[12]);
          jButton = Integer.parseInt(data[13]);
        }
      }
    } 
    println(button1,",",button2,",",button3,",",button4,",",linPot,",",liteSensor,",",tempSensor,",",mic,",",jX,",",jY,",",aX,",",aY,",",aZ,",",jButton);
    }
    

    EDIT: It sometimes will give the right data but in the wrong order and still nothing changes.

  • Swap lines 25 and 26

    What is the difference between split and splitTokens?

  • So i switched lines 25 and 26 and then it gave me an error on the new line 25 which is the line with if(data!=null&&data.length>14){ It says "data" doesn't exist so i initialized String[] data; at the beginning and no longer got that error. Then i run the sketch. i didn't get any errors but all the data still comes out as 1's and 0's and doesn't change at all.

    The updated code with lines 25 and 26 switched:

    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port
    int button1 = 1;
    int button2 = 1;
    int button3 = 1;
    int button4 = 1;
    int linPot = 0;
    int liteSensor,tempSensor,mic;
    int jX,jY;
    int aX,aY,aZ;
    int jButton = 1;
    String[] data;
    void setup()
    {
      String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
      myPort = new Serial(this, portName, 9600);
    }
    void draw()
    {
      if ( myPort.available() > 0) 
      {  // If data is available,
      val = myPort.readStringUntil('\n');
      if(val!=null){
    
        if(data!=null&&data.length>14){
          data = split(val, ",");
          button1 = Integer.parseInt(data[0]);
          button2 = Integer.parseInt(data[1]);
          button3 = Integer.parseInt(data[2]);
          button4 = Integer.parseInt(data[3]);
          linPot = Integer.parseInt(data[4]);
          liteSensor = Integer.parseInt(data[5]);
          tempSensor = Integer.parseInt(data[6]);
          mic = Integer.parseInt(data[7]);
          jX = Integer.parseInt(data[8]);
          jY = Integer.parseInt(data[9]);
          aX = Integer.parseInt(data[10]);
          aY = Integer.parseInt(data[11]);
          aZ = Integer.parseInt(data[12]);
          jButton = Integer.parseInt(data[13]);
        }
      }
    } 
    println(button1,",",button2,",",button3,",",button4,",",linPot,",",liteSensor,",",tempSensor,",",mic,",",jX,",",jY,",",aX,",",aY,",",aZ,",",jButton);
    }
    
  • So i switched lines 25 and 26 and then it gave me an error on the new line 25 which is the line with if(data!=null&&data.length>14){ It says "data" doesn't exist so i initialized String[] data; at the beginning and no longer got that error. Then i run the sketch. i didn't get any errors but all the data still comes out as 1's and 0's and doesn't change at all.

    The updated code with lines 25 and 26 switched:

    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port
    int button1 = 1;
    int button2 = 1;
    int button3 = 1;
    int button4 = 1;
    int linPot = 0;
    int liteSensor,tempSensor,mic;
    int jX,jY;
    int aX,aY,aZ;
    int jButton = 1;
    String[] data;
    void setup()
    {
      String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
      myPort = new Serial(this, portName, 9600);
    }
    void draw()
    {
      if ( myPort.available() > 0) 
      {  // If data is available,
      val = myPort.readStringUntil('\n');
      if(val!=null){        
        if(data!=null&&data.length>14){
          data = split(val, ",");
          button1 = Integer.parseInt(data[0]);
          button2 = Integer.parseInt(data[1]);
          button3 = Integer.parseInt(data[2]);
          button4 = Integer.parseInt(data[3]);
          linPot = Integer.parseInt(data[4]);
          liteSensor = Integer.parseInt(data[5]);
          tempSensor = Integer.parseInt(data[6]);
          mic = Integer.parseInt(data[7]);
          jX = Integer.parseInt(data[8]);
          jY = Integer.parseInt(data[9]);
          aX = Integer.parseInt(data[10]);
          aY = Integer.parseInt(data[11]);
          aZ = Integer.parseInt(data[12]);
          jButton = Integer.parseInt(data[13]);
        }
      }
    } 
    println(button1,",",button2,",",button3,",",button4,",",linPot,",",liteSensor,",",tempSensor,",",mic,",",jX,",",jY,",",aX,",",aY,",",aZ,",",jButton);
    }
    
  • Length won’t be > 14, would it?

  • yeah, since there are 14 values i'm taking from the string.

  • edited December 2017

    OR do you want

    ==14

    ?

  • I have no way to test your code, but you should test it on your side. Try doing something like

      if(data!=null&&data.length>14){
          data = split(val, ",");
           //NEW lines next--------------
           println("REPORT: length="+data.length)
           println("Incoming data content:");
           println(data); //or println(val)
           //REST of your code.....
           //...
           //...
           //...
    

    I am taking a wide guess here, but I think if you are getting lots of data, then your stream input might possibly have more than 14 tokens. If that is the case, I will process the first 14 tokens and then, if there is anything left (in case of more than 14), you wait until there are 14 or more to process it again. Another option is to reduce the rate of packages being sent from your Arduino side.

    Kf

  • edited December 2017
    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port
    int button1 = 1;
    int button2 = 1;
    int button3 = 1;
    int button4 = 1;
    int linPot = 0;
    int liteSensor,tempSensor,mic;
    int jX,jY;
    int aX,aY,aZ;
    int jButton = 1;
    String[] data;
    void setup()
    {
      String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
      myPort = new Serial(this, portName, 9600);
    }
    void draw()
    {
      if ( myPort.available() > 0) 
      {  // If data is available,
      val = myPort.readStringUntil('\n');
      if(val!=null){
    
        if(data!=null&&data.length==14){
          data = split(val, ",");
          println("REPORT: length="+data.length);
          println("Incoming data content:");
          println(data);
          button1 = Integer.parseInt(data[0]);
          button2 = Integer.parseInt(data[1]);
          button3 = Integer.parseInt(data[2]);
          button4 = Integer.parseInt(data[3]);
          linPot = Integer.parseInt(data[4]);
          liteSensor = Integer.parseInt(data[5]);
          tempSensor = Integer.parseInt(data[6]);
          mic = Integer.parseInt(data[7]);
          jX = Integer.parseInt(data[8]);
          jY = Integer.parseInt(data[9]);
          aX = Integer.parseInt(data[10]);
          aY = Integer.parseInt(data[11]);
          aZ = Integer.parseInt(data[12]);
          jButton = Integer.parseInt(data[13]);
        }
      }
     // println(val);
    } 
    
    //println(button1,",",button2,",",button3,",",button4,",",linPot,",",liteSensor,",",tempSensor,",",mic,",",jX,",",jY,",",aX,",",aY,",",aZ,",",jButton);
    }
    

    So i put in the code that you gave me and changed the >14 to ==14 and noted out my print() and nothing happens. The only thing that happens is a yellow line under println(data); with the warning of "Type String[] of the last argument to method println(Object...) doesn't exactly match the vararg parameter type. cast to Object[] to conform the non-varargs invocation, or pass individual arguments of type Object for a varargs invocation," Help

    EDIT: When i change println(data); to println(val); the warning goes away.

  • data is an array, and println() should be able to operate on this (I believe). It should print each element of the data array, one per line. I want you to print data so for you to see how the parsing is done. However, printing val should work as well. Move these lines before the preceding conditional:

      data = split(val, ",");
      println("REPORT: length="+data.length);
      println("Incoming data content:");
      println(val);
    

    I wish I could test your code to address the question directly, but I do not have a setup and not much time...

    noted out my print() and nothing happens.

    Nothing happens is not good... now you should be able to see something, I hope.

    Kf

  • println(data);still gives me issues. When I put println(val)in two different places before your code, it only works in one spot:

    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port
    int button1 = 1;
    int button2 = 1;
    int button3 = 1;
    int button4 = 1;
    int linPot = 0;
    int liteSensor,tempSensor,mic;
    int jX,jY;
    int aX,aY,aZ;
    int jButton = 1;
    String[] data;
    void setup()
    {
      String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
      myPort = new Serial(this, portName, 9600);
    }
    void draw()
    {
      if ( myPort.available() > 0) 
      {  // If data is available,
      val = myPort.readStringUntil('\n');
      if(val!=null){
        println(val);       //HERE IT PRINTS FINE
        if(data!=null&&data.length==14){
          println(val);         //HERE NOTHING PRINTS AT ALL
          data = split(val, ",");
          println("REPORT: length="+data.length);
          println("Incoming data content:");
          println(val);
          button1 = Integer.parseInt(data[0]);
          button2 = Integer.parseInt(data[1]);
          button3 = Integer.parseInt(data[2]);
          button4 = Integer.parseInt(data[3]);
          linPot = Integer.parseInt(data[4]);
          liteSensor = Integer.parseInt(data[5]);
          tempSensor = Integer.parseInt(data[6]);
          mic = Integer.parseInt(data[7]);
          jX = Integer.parseInt(data[8]);
          jY = Integer.parseInt(data[9]);
          aX = Integer.parseInt(data[10]);
          aY = Integer.parseInt(data[11]);
          aZ = Integer.parseInt(data[12]);
          jButton = Integer.parseInt(data[13]);
        }
      }
    } 
    
    //println(button1,",",button2,",",button3,",",button4,",",linPot,",",liteSensor,",",tempSensor,",",mic,",",jX,",",jY,",",aX,",",aY,",",aZ,",",jButton);
    }
    

    I have been looking all over and I cant find anything that works.

  • edited December 2017

    that advice to switch lines 25 and 26 (now lines 27 and 29) was bad advice 8)

    if(data!=null&&data.length==14){
      println(val);         //HERE NOTHING PRINTS AT ALL
      data = split(val, ",");
    

    why would you check that the data is null before setting the data? the check needs to be after the split.

  • edited December 2017

    (also, that needs spaces

    if(data!=null&&data.length==14){
    

    vs

    if (data != null && data.length == 14) {
    

    so much clearer. youwouldn'twriteasentencewithoutspaces.)

  • That still doesn't help me though. When i println() the data values, they all come out as 1's and 0's and then they don't even change. Ill give you the Arduino code also. Arduino Code:

    #include <Esplora.h>
    
    void setup() {
      Serial.begin(9600);
    while (!Serial);
    }
    
    void loop() {
      if(Serial.available()){
       parseCommand();
      }
      dumpInputs();
     }
    
    void parseCommand(){
      char cmd = Serial.read();
      switch (cmd) {
        case 'D':
          //dumpInputs();
          break;
        case 'R':
          setRed();
          break;
        case 'G':
          setGreen();
          break;
        case 'B':
          setBlue();
          break;
        case 'T':
          setTone();
          break;
      }
    }
    void dumpInputs() {
      Serial.print(Esplora.readButton(SWITCH_1));
      Serial.print(',');
      Serial.print(Esplora.readButton(SWITCH_2));
      Serial.print(',');
      Serial.print(Esplora.readButton(SWITCH_3));
      Serial.print(',');
      Serial.print(Esplora.readButton(SWITCH_4));
      Serial.print(',');
      Serial.print(Esplora.readSlider());
      Serial.print(',');
      Serial.print(Esplora.readLightSensor());
      Serial.print(',');
      Serial.print(Esplora.readTemperature(DEGREES_C));
      Serial.print(',');
      Serial.print(Esplora.readMicrophone());
      Serial.print(',');
      Serial.print(Esplora.readJoystickSwitch());
      Serial.print(',');
      Serial.print(Esplora.readJoystickX());
      Serial.print(',');
      Serial.print(Esplora.readJoystickY());
      Serial.print(',');
      Serial.print(Esplora.readAccelerometer(X_AXIS));
      Serial.print(',');
      Serial.print(Esplora.readAccelerometer(Y_AXIS));
      Serial.print(',');
      Serial.print(Esplora.readAccelerometer(Z_AXIS));
      Serial.println();
      delay(30);
    }
    void setRed() {
      Esplora.writeRed(Serial.parseInt());
    }
    
    void setGreen() {
      Esplora.writeGreen(Serial.parseInt());
    }
    
    void setBlue() {
      Esplora.writeBlue(Serial.parseInt());
    }
    
    void setTone() {
      Esplora.tone(Serial.parseInt());
    }
    

    Processing Code:

    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port
    int button1 = 1;
    int button2 = 1;
    int button3 = 1;
    int button4 = 1;
    int linPot = 0;
    int liteSensor,tempSensor,mic;
    int jX,jY;
    int aX,aY,aZ;
    int jButton = 1;
    void setup()
    {
      String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
      myPort = new Serial(this, portName, 9600);
    }
    void draw()
    {
      if ( myPort.available() > 0) 
      {  // If data is available,
      val = myPort.readStringUntil('\n');
      if(val!=null){
        String[] data = split(val, ",");
        //if(data != null && data.length == 14){ 
          if(data.length > 14){
          println("REPORT: length="+data.length);
          println("Incoming data content:");
          println(val);
          button1 = Integer.parseInt(data[0]);
          button2 = Integer.parseInt(data[1]);
          button3 = Integer.parseInt(data[2]);
          button4 = Integer.parseInt(data[3]);
          linPot = Integer.parseInt(data[4]);
          liteSensor = Integer.parseInt(data[5]);
          tempSensor = Integer.parseInt(data[6]);
          mic = Integer.parseInt(data[7]);
          jX = Integer.parseInt(data[8]);
          jY = Integer.parseInt(data[9]);
          aX = Integer.parseInt(data[10]);
          aY = Integer.parseInt(data[11]);
          aZ = Integer.parseInt(data[12]);
          jButton = Integer.parseInt(data[13]);
        }
      }
    } 
    
    //println(button1,",",button2,",",button3,",",button4,",",linPot,",",liteSensor,",",tempSensor,",",mic,",",jX,",",jY,",",aX,",",aY,",",aZ,",",jButton);
    }
    
  • edited December 2017

    this is how i would do this: do one thing at a time.

    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port
    int button1 = 1;
    int button2 = 1;
    int button3 = 1;
    int button4 = 1;
    int linPot = 0;
    int liteSensor, tempSensor, mic;
    int jX, jY;
    int aX, aY, aZ;
    int jButton = 1;
    
    boolean test = true; // new
    
    void setup() {
      if (test) {
        frameRate(2); // slow everything down
      } else {
        String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
        myPort = new Serial(this, portName, 9600);
      }
    }
    
    void draw() {
      String val = getData();
      if (val != null) {
        parseData(val);
      }
    }
    
    void parseData(String val) {
      String[] data = split(val, ",");
      if (data == null || data.length < 14) {
        println("error: not enough data");
      } else {
        println("REPORT: length="+data.length);
        println("Incoming data content:");
        println(val);
        button1 = Integer.parseInt(data[0]);
        button2 = Integer.parseInt(data[1]);
        button3 = Integer.parseInt(data[2]);
        button4 = Integer.parseInt(data[3]);
        linPot = Integer.parseInt(data[4]);
        liteSensor = Integer.parseInt(data[5]);
        tempSensor = Integer.parseInt(data[6]);
        mic = Integer.parseInt(data[7]);
        jX = Integer.parseInt(data[8]);
        jY = Integer.parseInt(data[9]);
        aX = Integer.parseInt(data[10]);
        aY = Integer.parseInt(data[11]);
        aZ = Integer.parseInt(data[12]);
        jButton = Integer.parseInt(data[13]);
        println("buttons:", button1, button2, button3, button4);
        println("others:", linPot, liteSensor, tempSensor, mic);
        println("j:", jX, jY);
        println("a:", aX, aY, aZ);
        println("jbutton:", jButton);
      }
    }
    
    // gets the data (test or from serial)
    String getData() {
      if (test) {
        // test, return test String
        println("Testing");
        return "1,1,1,1,702,1010,20,1,1023,3,-10,12,7,135";
      } else if ( myPort.available() > 0) {
        // return data from port
        val = myPort.readStringUntil('\n');
        println("Val: [" + val + "]");
        return val;
      } else {
        return null;
      }
    }
    

    i've added test variable. when this is true then it'll parse a known string. use this to debug the parsing of the incoming string without worrying about the arduino.

    WHEN THIS WORKS then you can set test to false to get the real data from the arduino. by this time you know the parsing works (assuming your test data is correct) and can concentrate on the incoming data.

    and don't skimp on the debug. just printing out a bunch of values without labels helps nobody.

  • So I tried your test and the values come out right, the when I change test to false so I can get my incoming data, I starts giving me the error NumberFormatExeption on a random Integer.parseInt()line. Am I dong something wrong with testing?

  • What you need to test, is to check how many tokens you get after you apply the split() function on your data. Because it is less than 14 doesn't mean you have all the tokens. In your case, if you want to process 14 tokens, your condition should read data.length==14. If you do data.length<14 then it is not guaranteed you have 14 tokens available. So for instance, if split returns 6 tokens, then you won't succeed in tempSensor = Integer.parseInt(data[6]); What I want to convince you (and convince us as we can't test your code) is that you are receiving consistently 14 tokens. If you are not, then at least we can proceed to think of the proper approach. I will strongly suggest you check previous arduino code here in the forum as they manage single data arrival (yes, you need to change your ino code for this). When you get a simple code working, then you can test some other sensor in a one to one approach.

    Problems that I can see:

    • You are sending an float value as integer
    • Your sensors might not be doing what you think they should be doing
    • The data rate you are sending is too high

    Stuff we don't know...

    Another approach is this:

    import processing.serial.Serial;
    
    static final int PORT_INDEX = 0, BAUDS = 9600;
    
    int[] vals;
    //float[] vals;
    
    void setup() {
      noLoop();
      final String[] ports = Serial.list();
      printArray(ports);
      new Serial(this, ports[PORT_INDEX], BAUDS).bufferUntil(ENTER);
    }
    
    void draw() {
      println(vals);
    }
    
    void serialEvent(final Serial s) {
      val = myPort.readStringUntil('\n');
      println(val);
    
      if(val!=null){
        String[] data = split(val, ",");
        println("REPORT: length="+data.length);
      }
    
      redraw = true;
    }
    

    What do you get from this code. Can you post few lines of this output? As you see, here I remove the overhead of processing the data as this part is ok. You need to focus in your raw data.

    Kf

  • edited December 2017

    All right guys, thanks for all the help. I finally figured it out...with a little help from my dad. Here's the final code:

    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port
    int button1 = 1;
    int button2 = 1;
    int button3 = 1;
    int button4 = 1;
    int linPot = 0;
    int liteSensor, tempSensor, mic;
    int jX, jY;
    int aX, aY, aZ;
    int jButton = 1;
    
    boolean test = false; // new
    
    void setup() {
      if (test) {
        frameRate(2); // slow everything down
      } else {
        String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
        myPort = new Serial(this, portName, 300);
      }
    }
    
    void draw() {
      String val = getData();
      if (val != null) {
        parseData(val);
      }
    }
    
    void parseData(String val) {
      String[] data = split(val, ",");
      if (data == null || data.length != 14) {
        println("error: not enough data");
      } else {
        println("REPORT: length="+data.length);
        println("Incoming data content:");
        println("|"+val+"|");
        button1 = int(data[0]);
        button2 = int(data[1]);
        button3 = int(data[2]);
        button4 = int(data[3]);
        linPot = int(data[4]);
        liteSensor = int(data[5]);
        tempSensor = int(data[6]);
        mic = int(data[7]);
        jButton = int(data[8]);
        jX = int(data[9]);
        jY = int(data[10]);
        aX = int(data[11]);
        aY = int(data[12]);
        aZ = int(data[13]);
        println("buttons:", button1, button2, button3, button4);
        println("others:", linPot, liteSensor, tempSensor, mic);
        println("j:", jX, jY);
        println("a:", aX, aY, aZ);
        println("jbutton:", jButton);
      }
    }
    
    // gets the data (test or from serial)
    String getData() {
      if (test) {
        // test, return test String
        println("Testing");
        return "1,1,1,1,702,1010,20,1,1023,3,-10,12,7,135";
      } else if ( myPort.available() > 0) {
        // return data from port
        val = myPort.readStringUntil('\n');
        println("Val: [" + val + "]");
        return val;
      } else {
        return null;
      }
    }
    

    As you can probably see, the biggest difference is that I should have been using int()to change data points into an integer rather than Integer.parseInt(). So for anyone else trying to split a string of data into a bunch of data points, this is how you do it.

  • int() is based on Integer.parseInt(): https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L9503

    Dunno what is the problem in your case. Great to hear it is working now.

    Kf

Sign In or Register to comment.