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

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?

That is my code:

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

Yeah…

Maybe use trim on the incoming data…

See reference

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

1 Like

Hello @Simon44,

Here is a topic that may be of interest to you:
Need assistance reading sequential inputs from arduino serial

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.

:)

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