Trying to wrangle serial data from an Arduino

Hello,

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

Processing Code 1
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
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"); 
  }

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
// 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;
          }
        }  
      }  
    }
  } 
  

This still needs work! I leave that with you.

References:
https://processing.org/reference/split_.html This will be handy!
https://processing.org/reference/libraries/serial/index.html

:)

2 Likes