How do I receive multiple values through Processing from the Arduino Serial Monitor

edited September 2017 in Arduino

Hi experts, newbie here,

I am having trouble reading data from the serial monitor sent by my Arduino in processing. Im not sure how to read multiple values.

Here is my Arduino data being sent to the serial monitor (9600 baud):

Serial.print(LeftSensor); Serial.print(','); Serial.print(FrontSensor); Serial.print(','); Serial.print(RightSensor);

Here is my processing serial reading function:

import processing.serial.*;
Serial port;
float center = 0;
float left = 0;
float right = 0;


void setup(){
size(480, 300); // AqT arduino quad test
port = new Serial(this, "/dev/cu.usbmodem1421", 9600);
port.bufferUntil('\n');
}

void serialEvent()
{ 
  int newLine = 13; // new line character in ASCII
  String message;
  do {
    message = port.readStringUntil(newLine); // read from port until new line
    if (message != null) {
      String[] list = split(trim(message), " ");
      if (list.length >= 4 && list[0].equals("Stuff: ")) {
        //yaw = float(list[1]); // convert to float yaw
        //pitch = float(list[2]); // convert to float pitch
        left = float(list[1]);
        center = float(list[2]);
        right = float(list[3]);
        roll = float(list[4]);
      }
      else{
      rect(20, 20, 20, 20);
      }
    }
  } while (message != null);
}

void draw(){
  serialEvent();  
}

When I try the code out, Processing reads nothing but the arduino works fine in terms of output.

Please help,

Thanks

Answers

  • Processing does not receive the input from arduino (arduino code works fine)

  • Hi ash8, What if somehow your processing program is out of step with the Arduino program? Your incoming message will only have some of the values. (Is the Arduino sending repeatedly?) or only occasionally? Try this idea:

    In Processing, make a global String called message. Each time a character arrives append it on the end of message (2 advantages already, you don't hang up your program waiting for something that's not coming, and you can print out what you have so far). Then check the message. If it has an end of line, and the stuff in front of the eol has 'stuff' and 4 values then use it. After using it (or not) delete it from the front of message.

Sign In or Register to comment.