# Help with Arduino and processing connection

**URL:** <https://discourse.processing.org/t/help-with-arduino-and-processing-connection/27422>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [January 28, 2021, 2:11am UTC](https://discourse.processing.org/t/help-with-arduino-and-processing-connection/27422 "2021-01-28T02:11:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![TiBasic](https://avatars.discourse-cdn.com/v4/letter/t/ecccb3/32.png) [@TiBasic](https://discourse.processing.org/u/TiBasic)\
**Post date:** [January 28, 2021, 2:11am UTC](https://discourse.processing.org/t/help-with-arduino-and-processing-connection/27422/1 "2021-01-28T02:11:29Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

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("❤ 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!

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [January 28, 2021, 4:16am UTC](https://discourse.processing.org/t/help-with-arduino-and-processing-connection/27422/2 "2021-01-28T04:16:47Z")

</div>

Try this:

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

```

---

<div class="post-metadata">

**Author:** ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)\
**Post date:** [January 28, 2021, 8:10pm UTC](https://discourse.processing.org/t/help-with-arduino-and-processing-connection/27422/3 "2021-01-28T20:10:27Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![TiBasic](https://avatars.discourse-cdn.com/v4/letter/t/ecccb3/32.png) [@TiBasic](https://discourse.processing.org/u/TiBasic)\
**Post date:** [January 28, 2021, 9:41pm UTC](https://discourse.processing.org/t/help-with-arduino-and-processing-connection/27422/4 "2021-01-28T21:41:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TiBasic](https://avatars.discourse-cdn.com/v4/letter/t/ecccb3/32.png) [@TiBasic](https://discourse.processing.org/u/TiBasic)\
**Post date:** [January 28, 2021, 9:43pm UTC](https://discourse.processing.org/t/help-with-arduino-and-processing-connection/27422/5 "2021-01-28T21:43:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![TiBasic](https://avatars.discourse-cdn.com/v4/letter/t/ecccb3/32.png) [@TiBasic](https://discourse.processing.org/u/TiBasic)\
**Post date:** [January 28, 2021, 9:49pm UTC](https://discourse.processing.org/t/help-with-arduino-and-processing-connection/27422/6 "2021-01-28T21:49:58Z")

</div>

I’m an idiot. I used

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

And that worked. Sorry for my stupidity
