Sending string from processing and reading with arduino.

My post of yesterday Sending String with MyPort.write from processing to arduino. - Programming Questions - Arduino Forum

I made a post yesterday about sending a String with myPorty.write();
As response someone told me to follow example 2 guide of this post.
Example 2 guide i found = Serial Input Basics - updated - Introductory Tutorials - Arduino Forum

From my point of view when i press my mouse button when running the gui of Processing my led should turn to high.

Processing code

import processing.serial.*;

Serial myPort;  // Create object from Serial class

void setup() 
{
  size(200, 200); //make our canvas 200 x 200 pixels big
  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
}

void draw() {
  
  if (mousePressed == true) 
  {                           //if we clicked in the window
    myPort.write("hallo");         //send a 1
    println("test");
  } else 
  {                           //otherwise
    myPort.write('0');          //send a 0
  }
}

Arduino code

// Example 2 - Receive with an end-marker

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data
int ledPin = 3;
boolean newData = false;

void setup() {
  Serial.begin(9600);
  Serial.println("<Arduino is ready>");
  pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
}

void loop() {
  recvWithEndMarker();
  showNewData();
}

void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;


  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
    }
  }
}

void showNewData() {
  if (newData == true) {
    Serial.print("This just in ... ");
    Serial.println(receivedChars);
    newData = false;

    digitalWrite(ledPin, HIGH); // turn the LED on
    delay(1000);
    digitalWrite(ledPin, LOW); // turn the LED on

  }
}

Can someone tell me what i am doing wrong or show me how to send a string with myPort.write();.

  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port

Are you absolutely certain that the Arduino is connected to the first item in the list? Mine never is.

  if (mousePressed == true)
  {                           //if we clicked in the window
    myPort.write("hallo");         //send a 1
    println("test");
  } else
  {                           //otherwise
    myPort.write('0');          //send a 0
  }

I'm sorry, but this is stupid. When the mouse BECOMES pressed, send a value. When the mouse BECOMES released, send a value. Do NOT spam the Arduino.

  recvWithEndMarker();

You are NOT sending start or end markers.

Oftentimes there are two different functions just like it is with Serial.print and Serial.println. From the code, and without ever having looked at the library you are using (end of caveat!), your Receiver is looking for a carriage return (like Serial.println would send). If your Transmitter is using the equivalent of Serial.print (without carriage return) then the Receiver will never get the command.

I found a solution using this do while loop.

char val; // Data received from the serial port
int ledPin = 3; // Set the pin to digital I/O 13
String inputread;

void setup() {
  pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
  Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
  inputread = "";
  if (Serial.available())
  { // If data is available to read,
    do{ 
      val = Serial.read(); // read it and store it in val
      if (val != -1){
        inputread = inputread +val;
        }
      }while(val != -1);
  }
  if (inputread == "kkk")
  { // If 1 was received
    digitalWrite(ledPin, HIGH); // turn the LED on
    delay(1000);
    digitalWrite(ledPin, LOW); // turn the LED on
    
  } else {
    digitalWrite(ledPin, LOW); // otherwise turn it off
  }
  delay(10); // Wait 10 milliseconds for next reading
}

I found a crappy solution using this do while loop.

The proper solution was to add the end marker to what Processing sent.

Sad little Pauls.

ghettokon:
Sad little Pauls.

Does that help @kingchakir after 4 years?