This is a minimal version that I am sharing and assumes that:
Serial.list()) is “COM3” //My Arduino; one of many!
I have many Arduinos and what ends up in Serial.list()) depends on what is connected at the time and fills up lowest to highest COM port.
Here is a version close to what you did:
// The serial port:
boolean serialState = false;
//boolean lastSerialState = true;
boolean comState = false;
background(255, 0, 0);
// List all the available serial ports:
printArray("Serial ports available: ");
// If you are trying to connect to a specific COM port by name
String comPort = "COM3";
comState = false;
comState = true;
println("Arduino is connected");
myPort3 = new Serial (this, "COM3", 9600);
serialState = true;
serialState = false;
background(0, 255, 0);
I only posted minimal code to keep it simple and to the point; there is so much that can be done with this.
The version I wrote and work with (not posted) checks through serial.list(), COM ports, allows me to chose a COM port, prints messages only once to prompt user, etc.
Waiting to see if that while() loop ever times out…