Trying to wrangle serial data from an Arduino

Hi @pl4sma2389 ,

Is the amount of lines and number per line fixed everytime the scan sends? I will assume that you always send 4 lines and 10 columns per line, followed by an “End”
If so, I would do something like, get the string, parse it and assign it.

Check the code below. Hope it helps!

import processing.serial.*;

Serial arduino; // Create object from Serial class

String serialData; // Data received from the serial port
int aziRange = 10; // Must be the same as on Arduino
int elevRange = 3; // Must be the same as on Arduino
int[][] LiDARdata; //Serial data formatted into an array
int data, count;
boolean dataSetReady;
int Width = 320;
int Height = 240;

int currentLine = 0;
void setup() {
  String portName = Serial.list()[2]; // 2 = COM3 = Arduino
  println();
  
  arduino = new Serial(this, portName, 9600);
  arduino.bufferUntil(13);
  
  LiDARdata = new int[elevRange][aziRange]; //4 lines by 10 columns 
  
  size(Width, Height);
}

void draw() {
 
}

void serialEvent(){
  String oneLine = arduino.readString();
  if(!oneLine.contains("End")){
    String parseLine[] = split(oneLine.trim(),'\t');
    if(parseLine.length == 10){
     for(int i = 0; i < aziRange; i++){
       LiDARdata[currentLine][i] = parseInt(parseLine[i]);
     }
     //DEBUG
     printArray(LiDARdata);
     
     currentLine++;
    }
  }
  else{
    currentLine = 0;
  }
}

EDIT: I did not test this code

1 Like