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?