Help with Arduino and processing connection

please format code with </> button * homework policy * asking questions

I’m trying to connect a processing program to arduino. It’s for a heart sensor. It works great except that the processing code can repeat the arduino serial messages, but not read then to look for a certain thing. My arduino code is below (it’s from a tlibrary so if it looks familiar, that’s why!)

/* Getting_BPM_to_Monitor prints the BPM to the Serial Monitor, using the least lines of code and PulseSensor Library.

#define USE_ARDUINO_INTERRUPTS true // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h> // Includes the PulseSensorPlayground Library.

// Variables
const int PulseWire = 0; // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
const int LED13 = 13; // The on-board Arduino LED, close to PIN 13.
int Threshold = 550; // Determine which Signal to “count as a beat” and which to ignore.
// Use the “Gettting Started Project” to fine-tune Threshold Value beyond default setting.
// Otherwise leave the default “550” value.
PulseSensorPlayground pulseSensor; // Creates an instance of the PulseSensorPlayground object called “pulseSensor”
void setup() {
Serial.begin(9600); // For Serial Monitor
// Configure the PulseSensor object, by assigning our variables to it.
pulseSensor.analogInput(PulseWire);
pulseSensor.blinkOnPulse(LED13); //auto-magically blink Arduino’s LED with heartbeat.
pulseSensor.setThreshold(Threshold);
// Double-check the “pulseSensor” object was created and “began” seeing a signal.
if (pulseSensor.begin()) {
Serial.println(“We created a pulseSensor Object !”); //This prints one time at Arduino power-up, or on Arduino reset.
}
}
void loop() {
int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on our pulseSensor object that returns BPM as an “int”.
// “myBPM” hold this BPM value now.
if (pulseSensor.sawStartOfBeat()) { // Constantly test to see if “a beat happened”.
Serial.println(":heart: A HeartBeat Happened ! "); // If test is “true”, print a message “a heartbeat happened”.
Serial.print("BPM: "); // Print phrase "BPM: "
Serial.println(myBPM);
if (myBPM != 203)
{
Serial.print(“h”);
}
}
delay(20); // considered best practice in a simple sketch.
}

the processing code is

/ import processing.serial.*;
Serial myPort; // Create object from Serial class
String val; // Data received from the serial port
void setup()
{
// I know that the first port in the serial list on my mac
// is Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you’re using.
String portName = Serial.list()[2]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
}
void draw()
{
if ( myPort.available() > 0)
{ // If data is available,
String inBuffer = myPort.readString();
if (inBuffer != null) {
if (inBuffer == “h”){
print(inBuffer);
}
}
// read it and store it in val
}
// println(val); //print it out in the console
} >

The console doesn’t print anything, but I know the sensor is being read. Any help would be great!

Try this:

if ( myPort.available() > 0) { 
String inBuffer = myPort.readStringUntil(10);
if (inBuffer != null) {
  println(inBuffer);
}
}

Hi @TiBasic Stop your Processing sketch. Can you see the output on the Arduino Serial Monitor? Stop the Serial Monitor, Start Processing sketch. Does sketch print now? (Only 1 program can connect to Ard’s port.) Can you see the heartbeat on the led?

That works, but what I want is to be able to find the specific character “h” in the port. I see it in the Arduino port but when I look for it specifically in the processing code, it can’t identify it.

I can see the output in the serial monitor and I see the “h” variable. I can even print it back into the serial monitor but for some reason the processing code can’t recognize it as a string. Do you know how to get it to identify it?

I’m an idiot. I used

if(inBuffer.indexOf(“h”)!=-1){
print(“found”);
}

And that worked. Sorry for my stupidity