More than 1 value

Hello,

My name is Jessy.
I’m trying to send more than 1 values from Arduino to Processing over Serial.
But what is the best method of sending them from the Arduino, and receiving it on Processing?

I know how to send 1 value, but when there is more coming over the Serial line, then i don’t know how to let Procesing pick up the second value.

This is what i have now, just for testing.

ARDUINO:

int potPin = A0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  int val = map(analogRead(potPin), 0, 1023, 0, 255);
  Serial.println(val);
  delay(500);
  
}

PROCESSING:

import processing.serial.*;
import controlP5.*; 

Serial port;
float hoogte = 0;

void setup() {

  size(500, 500);
  background(120);
  port = new Serial(this, "COM3", 9600);
  port.bufferUntil('\n');
}

void draw() {
  background(120);
  rect(125, 400, 50, -hoogte);
 
}

void serialEvent (Serial port) {

  hoogte = float(port.readStringUntil('\n'));
}

Now i want to add a LDR to my Arduino, but i don’t know how to let Processing know, which value that is.
So that i can make a second rect with the second value from the serial line.

Could anyone help me? :slight_smile:

Thanks in advance.

When sending multiple values there are a number of ways to distinguish between them

  • Always send the values in the same sequence with some means to determine one sequence from the next. This can be a time gap of some minimum length or a unique value.
  • Have a identifier preamble each value. That way you are not bound to the sequence but decoding is more complicated
  • Use the msb to distinguish between control and data bytes. This limits the transfer to 7 bits/byte instead of 8

Since you are sending the data as text,

Serial.print(firstValue);
Serial.print(",");
Serial.println(secondValue);

In Processing, use the port.readStringUntil('\n') method, to read the data into a String. Then, use the split() method to split the string into an array of ints (not floats, since you are NOT sending floats).