Error on my Processing GUI code for Arduino , I need some help

Hello,

Take a look at this example:

Arduino code:

  • sends comma delimited string of data with Serial.print(',');
  • the end of the data string is terminated with a '\r' and a '\n' from Serial.println();
Arduino Code
void setup() 
  {
  Serial.begin(9600);
  }

void loop() 
  {  
  for(int x = 0; x < 15; x++) 
    {  
    Serial.print((float) x + random(0, 1000)/1000.0 , 3);   
    Serial.print(',');
    }
  Serial.println();  
  delay(1000);
  }

Processing code:

  • receives characters up to and including '\n'
  • uses trim() to trim the data of whitespace including '\r' and a '\n'
  • splits the data separated by a ',' into an array
Processing Code
import processing.serial.*;

String myString = null;
Serial myPort;

void setup()
  {
  size(700, 700);
  printArray(Serial.list());
  String portName = Serial.list()[2];
  myPort = new Serial(this, portName, 9600);
  myPort.clear();
  myString = myPort.readStringUntil('\n');
  myString = null;
  }

void draw()
  {
  while (myPort.available() > 0)
    { // If data is available,
    String myString = myPort.readStringUntil('\n');
    //println(myString);
    if (myString != null) 
      {
      //println(myString);
      myString = myString.trim();
      //println(myString);

      //String[] q = split(myString, ','); 
      String[] q = splitTokens(myString, ",");
      printArray(q);
      
      if(q.length == 16)
        {
        printArray(q);
        }
      }
    }
  }

In my Arduino example…

The Arduino Serial.println(); sends a '\r' and a '\n':
https://www.arduino.cc/reference/en/language/functions/communication/serial/println/

The Processing trim() removes the '\r' and '\n'.

You can terminate the data sent with the Arduino with any character you want as long as it is not a character that is in your data.
Remember to check for this character in Processing in readStringUntil().

Take a good look at a working example and then scrutinize your code.

:)