# Using two different readStringUntil "characters"

**URL:** https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769
**Category:** Electronics (Arduino, etc.)
**Tags:** homework
**Created:** [April 29, 2019, 11:10pm UTC](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769 "2019-04-29T23:10:54Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![eezJerome](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@eezJerome](https://discourse.processing.org/u/eezJerome)
#### Post date: [April 29, 2019, 11:10pm UTC](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/1 "2019-04-29T23:10:54Z")

</div>

Hello.

I am not sure what to call the value inside the bufferUntil, but as my title implies I need to use two different characters, rather than just the newline (\n).

Is it possible that, inside the serialEvent() function, I have something like

```auto
...
void serialEvent(Serial port){
   x = float(port.readStringUntil("X"));
   y = float(port.readStringUntil("Y"))
}

//statements
...
```

where the values from serial look something like:

```auto
(note: for the sake of using the values for Processing, I appended the "X" and "Y")
X1.34
Y5.12
X1.25
Y5.23
```

I am really confused and I hope I can be helped. Thanks in advanced

---

<div class="post-metadata">

### Author: ![eezJerome](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@eezJerome](https://discourse.processing.org/u/eezJerome)
#### Post date: [April 29, 2019, 11:33pm UTC](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/2 "2019-04-29T23:33:42Z")

</div>

Alright so let’s say that for some reason I can’t readStringUntil(some\_random\_input), how about this?

```auto
***generic alg; language might be off, but idea is here
  float xval, yval;
  
  input = float(port.readStringUntil('\n');
  xval = input;
  input = float(port.readStringUntil('\n');
  yval = input;
  
  line(pxval, pyval, xval, yval);
  //previous values
  pxval = xval;
  pyval = yval;
```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [April 30, 2019, 10:04am UTC](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/3 "2019-04-30T10:04:46Z")

</div>

- [Forum.Processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item\_1](http://Forum.Processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item_1)

---

<div class="post-metadata">

### Author: ![eezJerome](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@eezJerome](https://discourse.processing.org/u/eezJerome)
#### Post date: [April 30, 2019, 10:26am UTC](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/4 "2019-04-30T10:26:39Z")

</div>

Acknowledged, will give it a read. Thanks for replying!

---

<div class="post-metadata">

### Author: ![eezJerome](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@eezJerome](https://discourse.processing.org/u/eezJerome)
#### Post date: [April 30, 2019, 10:54am UTC](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/5 "2019-04-30T10:54:00Z")

</div>

Well, I was lost after the first 3 replies, however I think I can use an array and index the place of it

Like maybe:

```auto
float val[2];
int index = 0;

//given that we are already reading from serial port called "port"...
...
void func(){ //not sure which real function to put in the real code
  input = port.readStringUntil('\n');
  val[index] = input;
  index = index + 1;
  input = port.readStringUntil('\n');
  val[index] = input;
  index = index + 1;
   if (index > 1){
    index = 0;
  }
  pxval = val[0];
  pyval = val[1];
}

...
//another external function here
void draw(){
  background(1);
  stroke(255);
  line(pxval, pyval, val[0], val[1]);
}
```

Note: being new to Processing and coming from a slight Arduino background, I am still learning.

Another note: I had no time to read the reply you gave, as well as the whole question, due to me having a load of schoolwork. Apologies for any incompetence.

EDIT: I have read a bit of the link within the one you sent me:

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/14988/drawing-of-graphs-from-i2c-imu#Item_7)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.

I think within my arduino code I should output the accelerometer values with a [0] or [1] appended to it, and from there use readStingUntil. I hope this is solved; I will do some testing but for the meantime if there are any errors you can see, please get back to me

Thank you

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [April 30, 2019, 11:29am UTC](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/6 "2019-04-30T11:29:12Z")

</div>

The solution I gave on that link expects the Arduino C code to send the multi-data as if they were a row of **tab-separated values** ( **TSV** ): 🧐

> **[Tab-separated values](https://en.wikipedia.org/wiki/Tab-separated_values)**
>
> A tab-separated values (TSV) file is a simple text format for storing data in a tabular structure, e.g., database table or spreadsheet data, and a way of exchanging information between databases. Each record in the table is one line of the text file. Each field value of a record is separated from the next by a tab character. The TSV format is thus a type of the more general delimiter-separated values format.
> TSV is a simple file format that is widely supported, so it is often used in data exchan...

That is, each value is followed by a `\t` character; except last 1, which is followed by an `\n` instead: 🤓

```auto
Serial.print(temperature);
Serial.write('\t');
Serial.print(masse);
Serial.write('\t');
Serial.print(humidite);
Serial.write('\t');
Serial.print(comptage);
Serial.write('\t');
Serial.println(son); // implies '\n'

```

At the Processing Java side, each data row is received whole via Serial::**readString()**, then split as an array via PApplet::**splitTokens()**, and then converted to `float[]` via PApplet::**float()**: ☕

1. [Processing.org/reference/libraries/serial/Serial\_readString\_.html](http://Processing.org/reference/libraries/serial/Serial_readString_.html)
2. [Processing.org/reference/splitTokens\_.html](http://Processing.org/reference/splitTokens_.html)
3. [Processing.org/reference/floatconvert\_.html](http://Processing.org/reference/floatconvert_.html)

```auto
float[] vals = {};

void serialEvent(final Serial s) {
  vals = float(splitTokens(s.readString()));
  redraw = true;
}

```

Full code from the original post: 🕶

```auto
/**
 * Efficient Serial Multi-Value Reading (v1.1.2)
 * GoToLoop (2015-Feb-18)
 *
 * Forum.Processing.org/two/discussion/14988/
 * drawing-of-graphs-from-i2c-imu#Item_3
 *
 * Forum.Processing.org/two/discussion/16618/
 * processing-with-arduino-void-serialevent#Item_1
 *
 * Discourse.processing.org/t/
 * using-two-different-readstringuntil-characters/10769/6
 */

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) {
  vals = int(splitTokens(s.readString()));
  //vals = float(splitTokens(s.readString()));
  redraw = true;
}

```

---

<div class="post-metadata">

### Author: ![eezJerome](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@eezJerome](https://discourse.processing.org/u/eezJerome)
#### Post date: [April 30, 2019, 11:44am UTC](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/7 "2019-04-30T11:44:37Z")

</div>

allow me to be ecstatic here…

OMG THANKS THIS IS GREAT AND THANK YOU!!!

Anyway…

It’s making more sense on the language part of this as well as the logic part behind it. Additionally the explanation of this made it less daunting to read the whole forum you initially linked hehe.

It turns out I’ve been using TSV in arduino…on my code I use:

```auto
Serial.print(val); Serial.print('\t');
...
```

EDIT: However I am still a bit confused as to how I will go about using values that the serial port in Processing receives. I’m a bit confused about arrays and all I know is once a value is put in an index it can be called like: val[1]

Thank you, but now I have schoolwork! I’m glad I’ve been helped!

Have a good day. Cheers

---

<div class="post-metadata">

### Author: ![eezJerome](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@eezJerome](https://discourse.processing.org/u/eezJerome)
#### Post date: [April 30, 2019, 11:51am UTC](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/8 "2019-04-30T11:51:39Z")

</div>

[https://processing.org/reference/splitTokens\_.html](https://processing.org/reference/splitTokens_.html)

This helps I guess…

I apologize for many reply and edit.

---

<div class="post-metadata">

### Author: ![eezJerome](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@eezJerome](https://discourse.processing.org/u/eezJerome)
#### Post date: [May 1, 2019, 9:42am UTC](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/9 "2019-05-01T09:42:45Z")

</div>

Alright, so far I have been having issues with this

On processing I have:

```auto

import processing.serial.*;
float[] val = {};
Serial port;

int len = 1200; // set length
int wid = 700; // set width
float x, y;
float px = 600;
float py = 350;

void setup() {
  size(1200,700);
  port = new Serial (this, "COM12", 115200);
  port.bufferUntil('\n');
}

void serialEvent(final Serial port) {
  val = float(splitTokens(port.readString()));
  redraw = true;
}
void draw(){
  gyroline();
}

void gyroline() {
  //...
  x = val[0];
  y = val[1];
  line(px, py, x + px, y + py);
  px = x;
  py = y;
  
  print(x); println(y); // for debugging
}
```

However this gives me “ArrayIndexOutOfBoundsException 0”

And for reference’s sake, here’s my Arduino (but only the loop() part):

```auto
//...
void loop() {
  mpu.getMotion6(&rawaccx, &rawaccy, &rawaccz, &rawgyrx, &rawgyry, &rawgyrz);
  getaccel(rawaccx, rawaccy, rawaccz);

  Serial.print(realaccx); Serial.write("\t"); // TSV
  Serial.print(realaccy); Serial.write("\t"); // TSV
  Serial.println('\n'); // NEWLINE
  delay(10);
}
```

Please reply soon ☹ Thank you  
EDIT: I’ve been trying to work on a solution while reading the links you sent and I have this. Now i am getting NullPointerException

```auto

import processing.serial.*;
String index;
float[] val;
Serial port;

int len = 1200; // set length
int wid = 700; // set width
float x, y;
float px = 600;
float py = 350;

void setup() {
  noLoop();
  final String[] port = Serial.list();
  // port = new Serial (this, "COM12", 115200);
  new Serial(this, "COM12", 115200).bufferUntil('\n');
  // port.bufferUntil('\n');
}

void serialEvent(final Serial port) {
  val = float(splitTokens(port.readString()));
  redraw = true;
}
void draw() {
  while (port.available() > 0) {
    String input = port.readString();
    val = float(splitTokens(input));
    gyroline();
  }
}

void gyroline() {
  //...
  x = val[0];
  y = val[1];
  line(px, py, x + px, y + py);
  px = x;
  py = y;

  print(x); 
  println(y);
}
```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 1, 2019, 11:21am UTC](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/10 "2019-05-01T11:21:59Z")

</div>

> [@eezJerome](#):
>
> However this gives me “ArrayIndexOutOfBoundsException 0”

That means the _val[]_ array variable is of [_length_](https://processing.org/reference/Array.html) = 0.

> [@eezJerome](#):
>
> Now i am getting “NullPointerException”

That means the _val[]_ array variable is still [`null`](https://processing.org/reference/null.html). That is, it has not been initialized yet.

So, why are those errors happening? Pay attention to this line of code below inside **serialEvent()**:

> [@eezJerome](#):
>
> `val = float(splitTokens(port.readString()));`

Variable _val[]_ is properly assigned w/ a `float[]` array only when **serialEvent()** is called back.

As you know, **draw()** is called back (much probably many times) much earlier before **serialEvent()** is called back for the 1st time!

However, you’re assuming that _val[]_ is already an array of _length_ = 2 at the lines below:

> [@eezJerome](#):
>
> ```auto
> void draw() {
> gyroline();
> }
> 
> void gyroline() {
> x = val[0];
> y = val[1];
> 
> // ...
> 
> ```

Given you expect to receive exactly 2 values from Serial::**readString()** each time it’s called back, you should pre-initialize _val[]_ w/ an array of length = 2 (or more if you wish), so it doesn’t crash the sketch when **draw()** is called back for the 1st time:

`float[] val = {};` → `float[] val = new float[2];`

P.S.: You don’t need `redraw = true;` when not using **noLoop()**:

> **[noLoop() / Reference](https://processing.org/reference/noLoop_.html)**
>
> Stops Processing from continuously executing the code within draw(). If loop() is called, the code in draw() begin to run continuously again. If using noLoop() in setup(…

---

<div class="post-metadata">

### Author: ![eezJerome](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@eezJerome](https://discourse.processing.org/u/eezJerome)
#### Post date: [May 1, 2019, 10:02pm UTC](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/11 "2019-05-01T22:02:06Z")

</div>

The last line of code gives me an error.

```auto
float[] val = {}; -> float[] val = float[2];
```

"Error on ‘.class’ "

> [@GoToLoop](#):
>
> As you know, **draw()** is called back (much probably many times) much earlier before **serialEvent()** is called back for the 1st time!

How do you propose I read the serial values and store each one separated by tsv onto my array?

Should I put the splitTokens withing draw? It doesn’t necessarily have to be in serialEvent right?

Edit: I tried to do something with this and it’s a null error:

```auto
import processing.serial.*;
String index;
float[] val;

Serial port;

float x, y;
float px = 600;
float py = 350;

void setup() {
  size(200, 200);
  noLoop();
  new Serial(this, "COM12", 115200).bufferUntil('\n');
}

//void serialEvent(final Serial port) {
// val = float(splitTokens(port.readString()));
//}

void draw() {
  while (port.available() > 0) {
    String input = port.readString(); //takes serial input from Arduino
    val = float(splitTokens(input)); //assuming that all values until \n are caputured, split into array
    x = val[0];
    y = val[1];
    line(px, py, x + px, y + py); //draw line
    px = x;
    py = y;
  }
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 2, 2019, 12:57am UTC](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/12 "2019-05-02T00:57:04Z")

</div>

> [@eezJerome](#):
>
> The last line of code gives me an error.

Oops! I knew I was forgetting something, the operator `new`: `float[] val = new float[2];`. 😖

> **[new / Reference](https://processing.org/reference/new.html)**
>
> Creates a "new" object. The keyword new is typically used similarly to the applications in the above example. In this example, a new object "h1" of the datatype "HLine" is created. On the follo…

---

<div class="post-metadata">

### Author: ![eezJerome](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@eezJerome](https://discourse.processing.org/u/eezJerome)
#### Post date: [May 2, 2019, 2:25am UTC](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/13 "2019-05-02T02:25:18Z")

</div>

Alright so nothing is working yet. I’m currently trying it on a simpler scale – using potentiometer and drawing lines. It’s similar anyway, so I thought I’d figure this thing out using potentiometer

I just have a question: Does the value for the array index reset to 0? From as far as I know (which is probably wrong 🤔), the splitTokens() parses a string and puts it into array as

```auto
 val [strIndex0] 
```

where this is the first value from the serial.

Now, as the serial is being read, given that it has 2 values, does the index reset after \n?

On another note…I’m working on this and this is what I have

```auto
import processing.serial.*;

Serial port;
float[] coords = new float [2];

float x, y;
float px, py;

void setup(){
  background(1);
  port = new Serial (this, "COM12", 115200);
}

void serialEvent(final Serial port){
  coords = float(splitTokens(port.readStringUntil('\n')));
}

void draw(){
  if(port.available() > 0){
    index = 0;
    serialEvent(port);
    x = coords[0];
    y = coords[1];
    line(px, py, x, y);
    px = x;
    py = y;
  }
}
```

The Arduino:

```auto

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

void loop() {
  int x = analogRead(A1);
  int y = analogRead(A0);

  int x1 = map(x, 1023, 0, -10, 10);
  int y1 = map(y, 0, 1023, -10, 10);

  Serial.print(x1); Serial.write('\t');
  Serial.print(y1); Serial.write('\t');
  Serial.println('\n');

}

```

Is the arduino side correct? I wont want to be referring to tsv and newline and then it turns out I’m wrong 😞

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 2, 2019, 2:51am UTC](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/14 "2019-05-02T02:51:35Z")

</div>

> [@eezJerome](#):
>
> Is the Arduino side correct?

Dunno much about Arduino, but it seems correct to me. 😳  
Although you can simplify it a lil’ more like this: 🤓

```auto
Serial.print(x1);
Serial.write('\t');
Serial.println(y1);

```

I’m also noticing you’re sending 2 `int` values, not `float` 1s: 🙈

```auto
int x1 = map(x, 1023, 0, -10, 10);
int y1 = map(y, 0, 1023, -10, 10);

```

So it’ll be more efficient to replace all `float` to `int` in the Processing side as well. 💡

Also in your Processing code side, I’ve spotted some bugs which wouldn’t be there if you had followed my own sketch model more closely! 🙄

- [Processing with Arduino - void serialEvent - Processing 2.x and 3.x Forum](http://Forum.Processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item_1)

Where’s Serial::**bufferUntil()** after this statement: 😬  
`port = new Serial(this, "COM12", 115200);`

> **[bufferUntil() / Libraries](https://processing.org/reference/libraries/serial/Serial_bufferUntil_.html)**
>
> Sets a specific byte to buffer until before calling serialEvent().

And do not use Serial::**available()** when using **serialEvent()**! 🙅‍♂️

---

<div class="post-metadata">

### Author: ![eezJerome](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@eezJerome](https://discourse.processing.org/u/eezJerome)
#### Post date: [May 2, 2019, 3:30am UTC](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/15 "2019-05-02T03:30:35Z")

</div>

> [@GoToLoop](#):
>
> Dunno much about Arduino, but it seems correct to me. 😳  
> Although you can simplify it a lil’ more like this: 🤓
> 
> ```auto
> Serial.print(x1);
> Serial.write('\t');
> Serial.println(y1);
> 
> ```

Thanks!

> [@GoToLoop](#):
>
> Where’s Serial:: **bufferUntil()** after this statement: 😬  
> `port = new Serial(this, "COM12", 115200);`

Oops I, too, knew I was missing something 😆

I’ll take the time to _slowly_ read because often times I skim (idk it’s a natural thing I do). Proves to be ineffective with programming hehe

> [@GoToLoop](#):
>
> I’m also noticing you’re sending 2 `int` values, not `float` 1s: 🙈
> 
> ```auto
> int x1 = map(x, 1023, 0, -10, 10);
> int y1 = map(y, 0, 1023, -10, 10);
> 
> ```
> 
> So it’ll be more efficient to replace all `float` to `int` in the Processing side as well. 💡

Will do 👍 but maybe I’ll just replace the Arduino code parts to float. If we’re going for code efficiency, I guess I’ll change it to int

At the time I wrote this reply this is what I have so far (implementing your recent suggestions…)

```auto
import processing.serial.*;

Serial port;
float[] coords = new float [2];

float x, y;
float px = 250;
float py = 250;

void setup(){
  size(500,500);
  final String[] str = Serial.list(); //not sure why i need this but copied it anyway
  printArray(str); //this one too
  port = new Serial (this, "COM12", 115200);
  port.bufferUntil('\n');
}

void draw(){
  background(1);
    //serialEvent(port);
    //coords = float(splitTokens(port.readString()));
    stroke(255);
    x = coords[0];
    y = coords[1];
    line(px, py, x + px, y + py);
    print(x);
    print(y);
    println(" ");
    px = x;
    py = y;
}

void serialEvent(final Serial port){
  coords = float(splitTokens(port.readString()));
  //redraw = true; //added just because it's on your model code
}
```

With a weird output

[code]  
[0] “COM12”  
0.00.0

…

0.00.0  
0.00.0  
ArrayIndexOutOfBoundsException: 0

[/code]:

TL:DR the end of the output has an error on it

It’s also not drawing anything

---

<div class="post-metadata">

### Author: ![eezJerome](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@eezJerome](https://discourse.processing.org/u/eezJerome)
#### Post date: [May 2, 2019, 9:56am UTC](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/16 "2019-05-02T09:56:52Z")

</div>

@GoToLoop I have tried and tried and even copied the code on the links you gave as though it was a template, but I have gotten nothing

I did, however, watch a video about multiple serial values and acting on it. After watching it I got this:

```auto
import processing.serial.*;

Serial port;

int index = 0;
int[] input_arr = new int[2];
int px, py;
int x, y;

void setup() {
  background(0);
  size(500, 500);

  port = new Serial(this, "COM12", 115200);
  stroke(255);
}

void draw() {
}

void serialEvent() {
  while (port.available() > 0) {
    String raw = port.readStringUntil('\n');
    input_arr[index] = int(raw);
    index++;

    if (index > 1) {
      x = px + input_arr[0];
      y = py + input_arr[1];
      port.clear();
      println(input_arr[0]); //debug; this doesnt print anything
      px = x;
      py = y;
    }
  }
  line(x, y, px, py);
  port.bufferUntil('\n');
}
```

I might start a new thread about this specific one…

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 2, 2019, 10:58am UTC](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/17 "2019-05-02T10:58:26Z")

</div>

Well, I don’t have any Arduino hardware and neither even seen 1 in front of me yet! 🥴

So I can only do blind coding in the dark for you. 🕶

Theoretically, an Arduino C code like this 1: 🤔

```auto
void setup() {
  Serial.begin(115200);
}

void loop() {
  const int x = map(analogRead(A1), 0, 1023, -10, 10);
  const int y = map(analogRead(A0), 0, 1023, -10, 10);

  Serial.print(x); // "-5"
  Serial.write('\t'); // '\t'
  Serial.println(y); // "10" + "\r\n"

  delay(2);
}

```

Should expect to send an output similar to this 1 on each of its **loop()**, right: ❔

```auto
String tsv = "-5" + '\t' + "10" + "\r\n";
println(tsv); // -5 10

int[] vals = int(splitTokens(tsv));
println(vals); // [0] -5 [1] 10

exit();

```

So AFAIK, a Processing Java code like this 1 should hopefully work for your case: 🥺

```auto
import processing.serial.Serial;

static final int VALS = 2;
int[] vals = new int[VALS];

void setup() {
  size(800, 600);
  noLoop();

  stroke(-1);
  clear();

  final String[] ports = Serial.list();
  printArray(ports);

  new Serial(this, "COM12", 115200).bufferUntil(ENTER);
}

void draw() {
  println(vals);
}

void serialEvent(final Serial s) {
  vals = int(splitTokens(s.readString()));
  redraw = true;
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 2, 2019, 11:01am UTC](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/18 "2019-05-02T11:01:30Z")

</div>

BtW, here are some Arduino API references: 😉

1. [https://www.Arduino.cc/reference/en/](https://www.Arduino.cc/reference/en/)
2. [https://www.Arduino.cc/reference/en/language/functions/communication/serial/](https://www.Arduino.cc/reference/en/language/functions/communication/serial/)

---

<div class="post-metadata">

### Author: ![eezJerome](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@eezJerome](https://discourse.processing.org/u/eezJerome)
#### Post date: [May 2, 2019, 11:57am UTC](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/19 "2019-05-02T11:57:12Z")

</div>

Yes it definitely works (i feel like I’m cheating here…). Thanks so much

Now, I’m wondering…what in the WORLD did I miss? I’ve been at this for 12 hours and it took you less than an hour

Problem solved, but I just want to know why after missing meals and sitting in front of a screen I still couldn’t get it myself. Reading documentations just became a drag after stressing, so it was pointless to do so 😠

Thanks though, but I hate getting the answer without learning my mistake.

Now I am trying to add the draw line and SO FAR this is what I have…

```auto
import processing.serial.Serial;

static final int VALS = 2;
int[] vals = new int[VALS];

//
int px = 400, py = 300;

void setup() {
  size(800, 600);
  noLoop();

  stroke(-1);
  clear();

  final String[] ports = Serial.list();
  printArray(ports);

  new Serial(this, "COM12", 115200).bufferUntil(ENTER);
}

void draw() {
  println(vals);
  line(px, py, vals[0], vals[1]);
  px = vals[0];
  py = vals[1];
}

void serialEvent(final Serial s) {
  vals = int(splitTokens(s.readString()));
  redraw = true;
}
```

EDIT: I noticed that `println(vals)` works just fine but why not `println(vals[0])`  
I think this has been causing my confusion and I’m glad I found it

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 2, 2019, 1:21pm UTC](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/20 "2019-05-02T13:21:42Z")

</div>

> [@eezJerome](#):
>
> I noticed that `println(vals);` works just fine; but why not `println(vals[0]);`?

Well, `println(vals);` logs the whole content of the array _val[]_.  
While `println(vals[0]);` logs only the value stored at index `0`.

The `[]` is called the array access operator btW:

> **[\[\] (array access) / Reference](https://processing.org/reference/arrayaccess.html)**
>
> The array access operator is used to specify a location within an array. The data at this location can be defined with the syntax array\[element\] = value and read with the syntax value = arra…

You can learn more about Java arrays here:

> **[Arrays (The Java™ Tutorials \>        
            Learning the Java...](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html)**
>
> This beginner Java tutorial describes fundamentals of programming in the Java programming language

> [@eezJerome](#):
>
> Now, I’m wondering…what in the WORLD did I miss?

The sketch I’ve posted in this thread is about 90% the same from my posted link:

- [Processing with Arduino - void serialEvent - Processing 2.x and 3.x Forum](http://Forum.Processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item_1)

Basically, I’ve simply replaced:

```auto
static final int PORT_INDEX = 0, BAUDS = 9600;
int[] vals = {};

new Serial(this, ports[PORT_INDEX], BAUDS).bufferUntil(ENTER);

```

w/

```auto
static final int VALS = 2;
int[] vals = new int[VALS];

new Serial(this, "COM12", 115200).bufferUntil(ENTER);

```

BtW, the constant PORT\_INDEX has to match an index value from the array returned by Serial.**list()** which corresponds to the port that’s gonna receive bytes from the Arduino:

> **[list() / Libraries](https://processing.org/reference/libraries/serial/Serial_list_.html)**
>
> Gets a list of all available serial ports. Use println() to write the information to the text window.

In your computer, that seems “COM12” matches the 1 connected to your Arduino.

However, if you simply copy & paste it on some other computer, chances are “COM12” might fail!

Also, even if in the same computer, but w/ a diff. OS, like some Linux distro, “COM12” won’t work at all; b/c Linux uses a completely diff. string for ports!

Also, if you happen to send a TSV row w/ more data than just 2, `int[] vals = new int[2];` isn’t enough, obviously!

Therefore, when dealing w/ any Arduino code, we should consider such code a template.

B/c each computer and OS will have its own port number!

Thus we have to slightly customize such code to the values used by own particular computer configuration rather than directly use an unmodified copy & paste serial program!

[Next page](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769.md?page=2)
