# Arduino to Processing String Comparison Error

**URL:** <https://discourse.processing.org/t/arduino-to-processing-string-comparison-error/15968>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [November 30, 2019, 3:55am UTC](https://discourse.processing.org/t/arduino-to-processing-string-comparison-error/15968 "2019-11-30T03:55:00Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![HAOZOO](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@HAOZOO](https://discourse.processing.org/u/HAOZOO)\
**Post date:** [November 30, 2019, 3:55am UTC](https://discourse.processing.org/t/arduino-to-processing-string-comparison-error/15968/1 "2019-11-30T03:55:01Z")

</div>

Hello, I am trying to have an Arduino use six different buttons to play six different playlists based off of comparing strings sent by Arduino to the value of a string in Processing, but am consistently running into a wall with the string printing correctly in processing but seemingly not recognizing its value to do anything with that information.

Below is the Arduino Code and the Processing Code that just deals with the button segment of things, as a new user I can’t also include the longer code in addition to this(two links only) but can the cimpler version is easier for troubleshooting most likely any advice on this is greatly appreciated, I’m a bit of a beginner to all of this still / shaking off the cobwebs after a few years:

(I’ve tried the string comparison using == .equals, and with a seperate predefined string as opposed to just a local quoted value and none of those have worked, my error returned is the ever helpfull NullPointerException which I don’t fully understand)

```auto
int redPin=0;
int orangePin=1;
int yellowPin=2;
int greenPin=3;
int bluePin=4;
int purplePin=5;

void setup() {
  // put your setup code here, to run once:
pinMode(redPin, INPUT_PULLUP); 
pinMode(orangePin, INPUT_PULLUP);
pinMode(yellowPin, INPUT_PULLUP);
pinMode(greenPin, INPUT_PULLUP);
pinMode(bluePin, INPUT_PULLUP);
pinMode(purplePin, INPUT_PULLUP);
  // this will use the Arduino PullUp resistors
Serial.begin(9600);
//initialize serial communications at a 9600 baud rate
}

void loop() {
  String buttonSend = "";
  // put your main code here, to run repeatedly:
//send 'Hello, world!' over the serial port
//Serial.println("Hello, world!");
//wait 100 milliseconds so we don't drive ourselves crazy
//delay(100);
// read the input pin:

  int redState = digitalRead(redPin);
  int orangeState = digitalRead(orangePin);
  int yellowState = digitalRead(yellowPin);
  int greenState = digitalRead(greenPin);
  int blueState = digitalRead(bluePin);
  int purpleState = digitalRead(purplePin);

if (redState == 0) Serial.println("Red");
if (orangeState == 0) Serial.println("Orange");
if (yellowState == 0) Serial.println("Yellow");
if (greenState == 0) Serial.println("Green");
if (blueState == 0) Serial.println("Blue");
if (purpleState == 0) Serial.println("Purple");
  // print out the state of the button:
}

```

```auto
import processing.serial.*;

Serial myPort; // The serial port  

void setup() {

  // List all the available serial ports:

  printArray(Serial.list());

  // Open the port you are using at the rate you want:

  myPort = new Serial(this, Serial.list()[0], 9600);

}

void draw() {

  String Red = “Red”;

  while (myPort.available() > 0) {

    String inBuffer = myPort.readStringUntil(’\n’);

    if (inBuffer.equals(Red)) {

      println(inBuffer);

    }

  }

}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 30, 2019, 4:18am UTC](https://discourse.processing.org/t/arduino-to-processing-string-comparison-error/15968/2 "2019-11-30T04:18:13Z")

</div>

hi, communication over serial link might  
be more efficient if you send ONE line ( every ? second? )  
( i call this CSV line ( like used by spreadsheets… ) )

```auto
0,1,0,0,0,1

```

* * *

you not mention your connected hardware on the inverted inputs  
but if you connect Arduino IDE and start the Monitor  
what print you get, can you post it here?

> When connecting a sensor to a pin configured with INPUT\_PULLUP, the other end should be connected to ground. In the case of a simple switch, this causes the pin to read HIGH when the switch is open, and LOW when the switch is pressed.

* * *

processing  
-a- use better  
[https://processing.org/reference/libraries/serial/serialEvent\_.html](https://processing.org/reference/libraries/serial/serialEvent_.html)  
example…

```auto
String data, val="NaN";
int var;

void serialEvent(Serial p) { // handle serial data
  data = trim(p.readStringUntil('\n'));
  if (data != null) {
    val = data;
    var = int(val);
    println("val "+val+" var "+var); // print every GOOD line and if it is understood 
  }
}

```

-b- add use a

> **[trim() / Reference](https://processing.org/reference/trim_.html)**
>
> Removes whitespace characters from the beginning and end of a String. In addition to standard whitespace characters such as space, carriage return, and tab, this function also removes the Unicode "nbs…

-c- the

```auto
inBuffer.equals("Red")

```

actually looks good.  
so -b- trim might solve the problem

---

<div class="post-metadata">

**Author:** ![HAOZOO](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@HAOZOO](https://discourse.processing.org/u/HAOZOO)\
**Post date:** [November 30, 2019, 4:35am UTC](https://discourse.processing.org/t/arduino-to-processing-string-comparison-error/15968/3 "2019-11-30T04:35:46Z")

</div>

Thanks so much for you advice, it seems to be helping but not fully, just delaying the issue by a bit sometimes, as I’ll explain more below:

Just added a delay function to the code in arduino so it is now sending every second, inputs just have buttons connected to them, and the serial monitor looks like this(with and without timestamps, the red is sent when the button is pressed/ held down):  
23:31:15.255 -\> Red  
23:31:18.269 -\> Red  
23:31:19.260 -\> Red  
23:31:20.254 -\> Red  
23:31:21.242 -\> Red  
23:31:25.246 -\> Red  
23:31:26.237 -\> Red  
23:31:27.233 -\> Red  
Red  
Red  
Red  
Red  
Red

I tried the trim function like this in processing and it appears to somewhat work, on my I can get one good read before the null pointer exception sometimes, other times it goes straight to the error:  
[0] “COM3”  
yes  
NullPointerException

I’m a bit confused about option A for processing since I’ve never used a serial event before but maybe that is the answer, below is the processing code using a trim

```auto

import processing.serial.*;

Serial myPort; // The serial port  

void setup() {
  // List all the available serial ports:
  printArray(Serial.list());
  // Open the port you are using at the rate you want:
  myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
  String Red = "Red";
  while (myPort.available() > 0) {
    String inBuffer = myPort.readStringUntil('\n'); 
    String input = trim(inBuffer);
    if (input.equals("Red")) {
      println("yes");
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 30, 2019, 4:42am UTC](https://discourse.processing.org/t/arduino-to-processing-string-comparison-error/15968/4 "2019-11-30T04:42:04Z")

</div>

please format your code posting by pasting it into the

```auto
</> code button

```

of the editor header menu ( context name: Preformatted text )  
it looks like  
```  
type or paste code here  
```

also can use the ``` manually above and below your code.

thank you.

* * *

please REPAIR both above posts

\_\_

possibly here some help:

[http://kll.engineering-news.org/kllfusion01/articles.php?article\_id=92#example3](http://kll.engineering-news.org/kllfusion01/articles.php?article_id=92#example3)

---

<div class="post-metadata">

**Author:** ![HAOZOO](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@HAOZOO](https://discourse.processing.org/u/HAOZOO)\
**Post date:** [November 30, 2019, 4:46am UTC](https://discourse.processing.org/t/arduino-to-processing-string-comparison-error/15968/5 "2019-11-30T04:46:42Z")

</div>

Repaired the above code

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 30, 2019, 4:53am UTC](https://discourse.processing.org/t/arduino-to-processing-string-comparison-error/15968/6 "2019-11-30T04:53:57Z")

</div>

hm: your serial issues, that the COM is lost???  
? bad USB cable?  
? on touch ( electrostatic )?  
? Arduino reboot ?power fail?

you can not run the Arduino IDE Monitor AND the processing sketch same time

---

<div class="post-metadata">

**Author:** ![HAOZOO](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@HAOZOO](https://discourse.processing.org/u/HAOZOO)\
**Post date:** [November 30, 2019, 4:57am UTC](https://discourse.processing.org/t/arduino-to-processing-string-comparison-error/15968/7 "2019-11-30T04:57:47Z")

</div>

seems to be working now, looks like there was a mechanical flaw in there as well, thanks! seems like that and the trim work well in conjunction, now to see if i can get it to work in the 6 variable system.

---

<div class="post-metadata">

**Author:** ![HAOZOO](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@HAOZOO](https://discourse.processing.org/u/HAOZOO)\
**Post date:** [November 30, 2019, 5:00am UTC](https://discourse.processing.org/t/arduino-to-processing-string-comparison-error/15968/8 "2019-11-30T05:00:17Z")

</div>

Still somewhat unreliable, is there a way to negate the mechanical issues in the code?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 30, 2019, 5:01am UTC](https://discourse.processing.org/t/arduino-to-processing-string-comparison-error/15968/9 "2019-11-30T05:01:33Z")

</div>

i know it sounds funny,  
on the first look you might say ,  
oh analog values… so difficult / create / transport / understand

push button more easy…  
NO  
actually need to check on the

> **[thomasfredericks/Bounce2](https://github.com/thomasfredericks/Bounce2)**
>
> Debouncing library for Arduino and Wiring. Contribute to thomasfredericks/Bounce2 development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 30, 2019, 10:43am UTC](https://discourse.processing.org/t/arduino-to-processing-string-comparison-error/15968/10 "2019-11-30T10:43:04Z")

</div>

> [@\[SOLVED\] ArrayIndexOutOfBoundsException: 1](https://discourse.processing.org/t/solved-arrayindexoutofboundsexception-1/2288/7):
>
> For multiple data transmission, I recommend you to read this post below: electric_plug[Forum.Processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item\_1](http://Forum.Processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item_1) As you can see, on the Arduino side, separate the data w/ \t using print() + write(). And then send the last data w/ println(). On the Processing side, inside serialEvent(), use Serial::readString() + PApplet.splitTokens() in order to get all the data as a String[] array. Of course, you’re still gonna need to conver…

> [@Using two different readStringUntil "characters"](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/17):
>
> Well, I don’t have any Arduino hardware and neither even seen 1 in front of me yet! woozy_face So I can only do blind coding in the dark for you. dark_sunglasses Theoretically, an Arduino C code like this 1: thinking void setup() { Serial.begin(115200); } void loop() { const int x = map(analogRead(A1), 0, 1023, -10, 10); const int y = map(analogRead(A0), 0, 1023, -10, 10); Serial.print(x); // "-5" Serial.write('\t'); // '\t' Serial.println(y); // "10" + "\r\n" delay…

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 30, 2019, 4:49pm UTC](https://discourse.processing.org/t/arduino-to-processing-string-comparison-error/15968/11 "2019-11-30T16:49:29Z")

</div>

Some resources:

> **[Make It Switch](https://learn.adafruit.com/make-it-switch/overview)**
>
> Many people look to incorporate switches with their microcontroller projects. Here is the guide that will help you do that with the tricks of the trade. Make your externally switched project today.

  
[https://itp.nyu.edu/physcomp/lessons/serial-communication/interpreting-serial-data/](https://itp.nyu.edu/physcomp/lessons/serial-communication/interpreting-serial-data/)

🙂

My test box:

 ![IMG_20191130_112327603](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/74551e12f425c8bb9f3846d67871617f4ed49ace.jpeg)

External pull-up resistors were used.

🙂

---

<div class="post-metadata">

**Author:** ![HAOZOO](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@HAOZOO](https://discourse.processing.org/u/HAOZOO)\
**Post date:** [November 30, 2019, 9:54pm UTC](https://discourse.processing.org/t/arduino-to-processing-string-comparison-error/15968/12 "2019-11-30T21:54:55Z")

</div>

would adding in a if (string != null) clause to everything help imporve reliability in processing? ideally this code can run in a room on its own without these button issues crashing it

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 30, 2019, 9:57pm UTC](https://discourse.processing.org/t/arduino-to-processing-string-comparison-error/15968/13 "2019-11-30T21:57:52Z")

</div>

> [@HAOZOO](#):
>
> would adding in a if (string != null) clause to everything help imporve reliability in processing?

That is discussed in the resource I provided.

🙂

---

<div class="post-metadata">

**Author:** ![HAOZOO](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@HAOZOO](https://discourse.processing.org/u/HAOZOO)\
**Post date:** [November 30, 2019, 10:00pm UTC](https://discourse.processing.org/t/arduino-to-processing-string-comparison-error/15968/14 "2019-11-30T22:00:07Z")

</div>

didn’t see that in there, lot of resources to parse through
