# Trying to wrangle serial data from an Arduino

**URL:** https://discourse.processing.org/t/trying-to-wrangle-serial-data-from-an-arduino/32720
**Category:** Electronics (Arduino, etc.)
**Created:** [October 10, 2021, 3:28am UTC](https://discourse.processing.org/t/trying-to-wrangle-serial-data-from-an-arduino/32720 "2021-10-10T03:28:41Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [October 10, 2021, 9:54am UTC](https://discourse.processing.org/t/trying-to-wrangle-serial-data-from-an-arduino/32720/2 "2021-10-10T09:54:15Z")

</div>

Hello,

I spent a few moments on this to get you started…

> **Processing Code 1**
>
> ```auto
> import processing.serial.*;
> 
> Serial arduino; // Create object from Serial class
> String serialData; // Data received from the serial port
> int aziRange = 10;
> int elevRange = 3;
> int[][] LiDARdata = new int[aziRange][elevRange]; //Serial data formatted into an array
> 
> void setup() {
> 
> printArray(Serial.list()); //glv
> String portName = Serial.list()[2]; // 2 = COM3 = Arduino
> arduino = new Serial(this, portName, 9600);
> 
> size(320, 240);
> }
> 
> void draw() {
> if (arduino.available() > 0) {
> serialData = arduino.readStringUntil('\r');
> //println(serialData); //glv
> }
> //if(serialData != "End" && serialData != null) {
> // println(serialData);
> // String[] columnBreak = new String[elevRange];
> // for(int i = 0; i < elevRange; i++) {
> // columnBreak = splitTokens(serialData, "\n");
> // print(columnBreak[i]);
> // }
> //}
> 
> if (serialData != null) {
> println(serialData);
> serialData = trim(serialData);
> 
> //String[] columnBreak = new String[elevRange];
> //columnBreak = splitTokens(serialData, " ");
> String[] columnBreak = splitTokens(serialData, " ");
> println(elevRange);
> for (int i = 0; i < elevRange; i++) {
> //columnBreak = splitTokens(serialData, " ");
> print(columnBreak[i]);
> print(" ! ");
> }
> println();
> println(columnBreak);
> }
> }
> 
> ```

> **Arduino Code 1**
>
> ```auto
> void setup() 
> {
> Serial.begin(9600);
> Serial.flush();
> delay(2000);
> }
> 
> void loop() 
> {
> Serial.println("23:15:16.634 -> 336 478 474 465 458 433 420 411 398 374");
> delay(100); 
> Serial.println("23:15:16.634 -> 453 457 453 441 431 422 410 399 397 384");
> delay(100);  
> Serial.println("3:15:16.634 -> 480 470 471 460 443 436 421 411 400 399"); 
> delay(100); 
> Serial.println("23:15:16.634 -> 478 474 465 458 433 420 411 398 374 390"); 
> }
> 
> ```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/83bb2cc5cfc65391af2d38cdc6e986b82f1d45ca.png)

Start with a 1D array and then pack that into a 2D array.  
That was my next step… but have laundry to do and this is as far as I got.

There are many posts in this forum on serial communications.

Have fun exploring!

Update…

Add the “End” to Arduino code to use with code below.

This code below will receive a nice clean data set of Strings from Arduino.  
I left it as a 1D array of Strings that you can process further into useful data.

> **Processing Code 2**
>
> ```auto
> // Processing Serial Data
> // GLV
> // 2021-10-10
> 
> import processing.serial.*;
> 
> Serial arduino; // Create object from Serial class
> String serialData; // Data received from the serial port
> 
> int data, count;
> String [] sLidarData = new String [5];
> 
> boolean dataSetReady;
> 
> void setup() 
> {
> size(350, 100);
>     
> printArray(Serial.list());
> String portName = Serial.list()[2]; // 2 = COM3 = Arduino
> println();
> 
> // This will clear out the "leftovers" in the buffers
> arduino = new Serial(this, portName, 9600);
> arduino.stop();
> arduino = new Serial(this, portName, 9600);
> }
> 
> void draw()     
> {
> //background(0);
> // Receive sLidarData data set  
> if (arduino.available() > 0) 
> {
> serialData = arduino.readStringUntil('\r');
> //println(serialData); //debug
> 
> if (serialData != null) 
> {
> //println(serialData); // Try this before and after!
> serialData = trim(serialData);
> println(serialData);
> 
> String[] dataIn = splitTokens(serialData, " ");
>       
>     
> // This saves the Lidar data as Strings in an 1D array
> // It can then be processed into a 2D array or otherwise.
>   
> sLidarData [data++] = serialData;
>       
> if (dataIn[2].equals("End")) 
> {
> println(); println("End"); println();
> data = 0;
> printArray(sLidarData); //debug
> if(sLidarData.length == 5) 
> {
> println(); println(count);   
> dataSetReady = true;
> }
> }  
> }  
> }
> } 
>   
> 
> ```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/848fcad9232845f512cbf366d7b1ba6128157461.png)

This still needs work! I leave that with you.

References:  
[https://processing.org/reference/split\_.html](https://processing.org/reference/split_.html) This will be handy!  
[https://processing.org/reference/libraries/serial/index.html](https://processing.org/reference/libraries/serial/index.html)

`:)`

---

_[View the full topic](https://discourse.processing.org/t/trying-to-wrangle-serial-data-from-an-arduino/32720)._
