you could allow user to select port with keyboard?
import processing.serial.*;
Serial myPort;
String datas; // latest arduino line
int[] datai; // as array of integers
StringList list = new StringList(); // stored lines
int listlong = 18; // textlines only
int w= 640, h = 360; // canvas settings
// rev plot
int a=120, d = 5; // scope background area rectangle
int[] datainew, dataiold; // as array of integers to make lines
float range = 0.3, basex = 130, basey = h-30; // scope area
float step = ( w-basex-d )/ ( listlong-1 ); // line step calc of array length
boolean diag = true; // print diagnostic info
boolean connect=false; // start unconnected but list ports
int connectto=0; // start port
void setup_serial() { // USB arduino..
//printArray(Serial.list());
String portName = Serial.list()[connectto]; // adjust 0.. x port
myPort = new Serial(this, portName, 115200);
myPort.clear();
myPort.bufferUntil('\n');
println("try connect to "+portName);
}
void serialEvent(Serial p) { // handle serial data
datas = trim(p.readStringUntil('\n'));
if (datas != null) {
if (diag) println(datas); // print every ?GOOD? line
datai = int( split(datas, ",") ); // create int array from line to check valid vals
if ( datai.length >= 3 ) {
list.append( datas ); // good, min 3 vals, store line at String list
} else {
println("bad line: "+datas);
}
if ( list.size() >= listlong ) list.remove(0); // erase the oldest
}
}
void settings() {
size(w, h);
}
void setup() {
printArray(Serial.list());
//setup_serial();
println("select port with key [0] ..[9]");
}
void draw() {
background(0, 0, 80);
for ( int i = 0; i < list.size(); i++ ) text( list.get(i), 10, 20+i*20 ); // running list of arduino lines
plot();
}
void plot() {
push();
fill(100);
rect(a, d, width-a-d, height-2*d);
for ( int i = 0; i < list.size()-1; i++ ) {
dataiold = int( split(list.get(i), ",") );
datainew = int( split(list.get(i+1), ",") );
if ( dataiold.length >=2 && datainew.length >=2 ) { // avoid array errors
stroke(0, 200, 0);
line(basex+i*step, basey-range*dataiold[0], basex+(1+i)*step, basey-range*datainew[0]); // line A0
stroke(200, 200, 0);
line(basex+i*step, basey-range*dataiold[1], basex+(1+i)*step, basey-range*datainew[1]); // line A1
stroke(0, 200, 200);
line(basex+i*step, basey-range*dataiold[2], basex+(1+i)*step, basey-range*datainew[2]); // line A2
}
}
pop();
}
void keyPressed() {
println(key, keyCode);
if ( key >= 48 && key <= 57 ) {
connectto = key-48;
setup_serial();
}
if ( key == 'c' ) setup_serial(); // unused
}
/*
[0] "COM1"
[1] "COM7"
try connect to COM7
1023,880,778,
1023,880,779,
*/
// using arduino code ( test on Arduino Leonardo and IDE 1.8.10 hourly )
/*
int dt = 100; // msec between sending
void setup() {
Serial.begin(115200);
}
void loop() {
Serial.print( analogRead(0) ); // A0
Serial.print(",");
Serial.print( analogRead(1) ); // A1
Serial.print(",");
Serial.print( analogRead(2) ); // A2
Serial.println(",");
delay(dt); // sample rate
}
*/
/*
// with A0 jumper to 5V see like
1023,885,788,
1023,884,786,
1023,884,786,
1023,883,786,
1023,885,788,
*/