# Comparing serial Arduino inputs with Strings in if-statements don´t work

**URL:** https://discourse.processing.org/t/comparing-serial-arduino-inputs-with-strings-in-if-statements-don-t-work/30791
**Category:** Electronics (Arduino, etc.)
**Created:** [June 19, 2021, 8:26am UTC](https://discourse.processing.org/t/comparing-serial-arduino-inputs-with-strings-in-if-statements-don-t-work/30791 "2021-06-19T08:26:09Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Simon44](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@Simon44](https://discourse.processing.org/u/Simon44)
#### Post date: [June 19, 2021, 8:26am UTC](https://discourse.processing.org/t/comparing-serial-arduino-inputs-with-strings-in-if-statements-don-t-work/30791/1 "2021-06-19T08:26:09Z")

</div>

Hello Community,

I´m working on my first Arduino + Processing project. The Arduino microcontroller is built as an “roll the dice” game. When I push a button on the board random numbers between 1-6 are rolled and displayed on an LED-screen. The Arduino sketch also send the rolled numbers to Processing, where they should be shown as dots on a virtual dice.

Sending the rolled numbers to Processing works fine, but at the moment I have trouble with the if-loops, which should compare the sended numbers with the statements and draw the dots .

The numbers are sended as Strings and saved to the global variable _portStream_. In the draw() loop is checked, if Strings are sended (_portStream != null_). Is that the case, the sended numbers in format String are saved in the local String variable _wuerfel_. The next if-loops should draw the dots, when the value of the String _wuerfel_ is equal to the statements “1”, “2” …  
But the dots were never drawn, so there must be an error in my code or a difference between the sended numbers from the Arduino and the Statements in the if-loops. For troubleshooting the value of wuerfel is also printed in the console, but there the values looks fine. I also created the sketch with integer instead of strings, but there was the same problem, the console output looked good, but the if-loops for drawing the dots doesn´t work. I also tried to attache “\n” to the if-Statements, in case of that they are a part of the Strings from Arduino and hidden in the console.  
Does anyone have a hint for the solution of the failing if-loops?

 ![Processing Console](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/9e3ed587e4e063f4cd790b78586570e08a5dec53.png)

That is my code:

```auto
import processing.serial.*;

Serial myPort;
String portStream;
int diceSize = 120;

void setup() {
  String portName = Serial.list()[1];
  myPort = new Serial(this, portName, 9600);
  myPort.bufferUntil('\n');
  size(500, 500);
  background(100,255,150);
}

void draw() {
  if(portStream != null) {
    String wuerfel = portStream;
    println(wuerfel);
    
    //Würfel
    noStroke();
    fill(#FFF3D6);
    rectMode(CENTER);
    rect(width/2, height/2, diceSize, diceSize, diceSize/5);
  
    //Augen
    fill(50);
     if (wuerfel.equals("1") == true || wuerfel.equals("3") == true || wuerfel.equals("5")== true ) {
        ellipse(width/2, height/2, diceSize/5, diceSize/5); 
     }
      if (wuerfel.equals("2") == true || wuerfel.equals("3") == true || wuerfel.equals("4") == true 
        || wuerfel.equals("5") == true || wuerfel.equals("6") == true ) { 
        ellipse(width/2 - diceSize/4, height/2 - diceSize/4, diceSize/5, diceSize/5);
        ellipse(width/2 + diceSize/4, height/2 + diceSize/4, diceSize/5, diceSize/5);
      }
      if (wuerfel.equals("4") == true || wuerfel.equals("5") == true || wuerfel.equals("6") == true ) {
        ellipse(width/2 - diceSize/4, height/2 + diceSize/4, diceSize/5, diceSize/5);
        ellipse(width/2 + diceSize/4, height/2 - diceSize/4, diceSize/5, diceSize/5);
      }
      if (wuerfel.equals("6") == true ) {
        ellipse(width/2, height/2 - diceSize/4, diceSize/5, diceSize/5);
        ellipse(width/2, height/2 + diceSize/4, diceSize/5, diceSize/5);
      }
      delay(50);
  }
}

void serialEvent(Serial myPort) {
  portStream = myPort.readString();
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 19, 2021, 8:57am UTC](https://discourse.processing.org/t/comparing-serial-arduino-inputs-with-strings-in-if-statements-don-t-work/30791/2 "2021-06-19T08:57:51Z")

</div>

Yeah…

Maybe use trim on the incoming data…

See reference

---

<div class="post-metadata">

### Author: ![Simon44](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@Simon44](https://discourse.processing.org/u/Simon44)
#### Post date: [June 19, 2021, 9:47am UTC](https://discourse.processing.org/t/comparing-serial-arduino-inputs-with-strings-in-if-statements-don-t-work/30791/3 "2021-06-19T09:47:23Z")

</div>

Thank you so much for the hint, now the programm works as expected.  
I think this is missing experience but, I`ll get better with every solved problem

---

<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: [June 19, 2021, 10:55am UTC](https://discourse.processing.org/t/comparing-serial-arduino-inputs-with-strings-in-if-statements-don-t-work/30791/4 "2021-06-19T10:55:49Z")

</div>

Hello @Simon44,

Here is a topic that may be of interest to you:  
_[Need assistance reading sequential inputs from arduino serial](https://discourse.processing.org/t/need-assistance-reading-sequential-inputs-from-arduino-serial/20368)_

You will find that there are many topics on this.

Serial communication can be a challenge at first but once you master it the _world is your oyster_… at least your code will be.

`:)`

---

<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: [June 21, 2021, 11:21am UTC](https://discourse.processing.org/t/comparing-serial-arduino-inputs-with-strings-in-if-statements-don-t-work/30791/5 "2021-06-21T11:21:45Z")

</div>

You don’t need ‘== true’ everywhere. The .equals returns a boolean, and you need a boolean expression for ‘if ()’

```auto

```
