Need assistance reading sequential inputs from arduino serial in processing

EDIT: Got it all sorted out, thanks everyone.

My arduino program and device appears to be working correctly, sending the appropriate info out to serial. From reading through the console prints in processing, it is reading the inputs into strings correctly. I'm just hitting a wall in terms of figuring out how to do different things with the sequential inputs.

The arduino device uses a 4x4 keypad to perform various unit conversions, triggered by the A/B/C/D keys. It sends over the letter of the key pressed, followed by the numbers entered into, and returned by the function. The D key toggles between Imperial to Metric, and Metric to Imperial, and outputs a boolean state status.

All the variables are Strings, since I'm using the info from serial for text displays and they don't have to be used for any calculations within processing.

Copied below is a section of the serial.read code for processing. Inputs from arduino are in the format of:

  • D 1 or D 0 ( letter D followed by the boolean state of conversion direction)
  • A # # (the letter A, B, or C followed by 2 separate numbers, the entered value to convert, and the resulting converted value)

How should I go about changing things to be able to accept those 2 or 3 inputs into separate, useable items, and then acting off of them?

while (myPort.available() > 0)
  {
    // set myPort.read() to a readable variable
    //    using read string until to collect the entirety of the arduino input.
    inPort = myPort.readStringUntil(10);
    // what do i do to get it to operate on following inputs?
    //    a trigger when it hits the newline command?
    
    // printing for troubleshooting
    println(inPort);
    
    // trigger actions based on input from arduino
    if(inPort == "D")
    {
      String dPort = inPort;
      // set variable to display selected conversion mode
      if(dPort == "1")
      {
        conDis = "I->M";
      }
      else if(dPort == "0")
      {
        conDis = "M->I";
      }
    }
    // differences in the A/B/C inputs is really only the trigger of the units
    else if(inPort == "A")
    {
      // really need to figure out how to get these to read properly
      inSum = myPort.readStringUntil(10);
      inVal = myPort.readStringUntil(10);
      // printing to console for debugging
      println("inSum");
      println(inSum);
      println("inVal");
      println(inVal);    
      // set unitString to A units, based on current conversion path
      if(conDis == "I->M")
      {
        unitString = "Meters";
      }
      else if(conDis =="M->I")
      {
        unitString = "Feet";
      }
    }

I've looked at trying Serial Event in processing, but it appears to put me back at square one at best.

Got things to recognize specific inputs through .trim(), now to get it to deal with following input

I’ve looked at trying Serial Event in processing, but it appears to put me back at square one at best.

If you define, using the bufferUntil() method of the Serial instance, when to trigger the serialEvent() function, that function will only be called when a complete packet has arrived (assuming you are sending something as an end of packet marker; a carriage return or line feed will do).

Then, in the serialEvent() function, use readStringUntil() to read parts of the packet (until the space). You will end up with a string for each part (“A”, “14”, and “45”, for instance). Do with them as you see fit.

This really is a Processing issue though, not an Arduino issue.

I am not familiar with Processing.

However ...
Have the Arduino send a Carriage Return after all of the bytes have been sent.

In Processing, read each byte sent by the Arduino into a char array.
Process the char array after the Carriage Return byte is received.

Got it sorted, thanks everyone.

brandon_y:
Got it sorted, thanks everyone.

That's about useless post as any.

If you are not going to share, don't count on people trying to help the next time.

.