Arduino to Processing String Comparison Error

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)

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:
}
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);

    }

  }

}


1 Like

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… ) )

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
example…

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
https://processing.org/reference/trim_.html
-c- the

inBuffer.equals("Red")

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

1 Like

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


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");
    }
  }
}

1 Like

please format your code posting by pasting it into the

</> 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

Repaired the above code

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

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.

1 Like

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

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

1 Like


1 Like

Some resources:


https://itp.nyu.edu/physcomp/lessons/serial-communication/interpreting-serial-data/

:slight_smile:

My test box:

External pull-up resistors were used.

:slight_smile:

1 Like

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

That is discussed in the resource I provided.

:slight_smile:

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