Sending serial data to processing??

I have an idea in mind for a program that I think is viable but I keep getting errors. I want to get data from a hcsr04 over to processing so that I can output the sound in a much nicer way than just outputting it to a piezo buzzer. In essence, I want to make a theremin. Here is the code

#include <NewPing.h>

#define TRIGGER_PIN  12
#define ECHO_PIN     11
#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
  Serial.begin(115200);
}

void loop() {
  delay(50);
  int uS = sonar.ping();
  Serial.println(uS / US_ROUNDTRIP_CM);
}

and here is the processing code:

import processing.serial.Serial;
 
static final int PORT_IDX = 3, BAUDS = 9600;
String myString;
 
void setup() {
  noLoop();
  final String[] ports = Serial.list();
  println(ports);
  new Serial(this, ports[PORT_IDX], BAUDS).bufferUntil(ENTER);
}
 
void draw() {
  println(myString);
}
 
void serialEvent(final Serial s) {
  myString = s.readString().trim();
  redraw = true;
}

I have also tried this processing code but it also isn't working:

import processing.serial.*;
Serial myPort;  
int val;    
void setup() 
{
 String portName = Serial.list()[1]; //com3, same as arduino
 myPort = new Serial(this, portName, 9600);
}

void draw()
{
 if ( myPort.available() > 0)  // If data is available,
 {  
   val = myPort.read();         // read it and store it in val
   println(val);
 }
}

I'm getting errors about the array of the COM port being out of exception and I don't know why...

Any help, please?

  new Serial(this, ports[PORT_IDX], BAUDS).bufferUntil(ENTER);

Creating an instance of the Serial class, and throwing the instance away seems pointless.

Are you CERTAIN that the Arduino is connected to the 4th port in the list?

void setup() {
  noLoop();
  final String[] ports = Serial.list();
  println(ports);
  new Serial(this, ports[PORT_IDX], BAUDS).bufferUntil(ENTER);
}

From https://processing.org/reference/noLoop_.html:

If using noLoop() in setup(), it should be the last line inside the block.

Last is not the same as first.

void draw() {
  println(myString);
}

You don't want draw() called, but you gave it something to do, just in case?

void serialEvent(final Serial s) {
  myString = s.readString().trim();
  redraw = true;
}

What is that list line supposed to do?

void draw()
{
 if ( myPort.available() > 0)  // If data is available,
 { 
   val = myPort.read();         // read it and store it in val
   println(val);
 }
}

Serial IO does not belong in draw().

In any case, what does having Processing print a value to the console window have to do with building a theremin, whatever that is?

I want the data outputted to a sine like graph which changes when the pitch changes. I have seen similar projects online. Would you be able to give me tips on how the code should be?

Would you be able to give me tips on how the code should be?

No, because I can not see a relationship between the distance being measured and pitch. I have three distinct meanings for pitch (airplane nose up, throwing a baseball, and sticky stuff from a pine tree). I can't see how any of them relate to distance.

Pitch is also how squeaky or low a sound is. I suppose its an English term. Does this help?

Pitch is also how squeaky or low a sound is.

How does THAT relate to distance?

Music pitch is note frequency and the relation between that and sensor returns is an abstraction made by the programmer.

GoForSmoke:
Music pitch is note frequency and the relation between that and sensor returns is an abstraction made by the programmer.

You misspelled obfuscation.