Erorr on getting string from arduino to processing IDE

I have written a code which receives string from Arduino to processing IDE. but when i receive data from Arduino to processing. it shows null error. can anyone help in this please?. Here's the code I'm attaching.

heres the code for processing IDE

import processing.serial.*;

Serial myPort; // Create object from Serial class
String val; // Data received from the serial port

void setup()
{
// I know that the first port in the serial list on my mac
// is Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[2]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 115200);
}
void draw()
{
if ( myPort.available() > 0)
{ // If data is available,
val = myPort.readStringUntil('\n'); // read it and store it in val
}
println(val); //print it out in the console
}

Here's the code for Arduino

void setup()
{
//initialize serial communications at a 9600 baud rate
Serial.begin(115200);
}

void loop()
{
//send 'Hello, world!' over the serial port
Serial.write("Hello, world!");
//wait 100 milliseconds so we don't drive ourselves crazy
delay(100);
}

Almost… there are three ways to post code, and you tried two of them.

The third way is the right way !
Check the forum guidelines for posting code in [code tags]

Your comment doesn't match what the code actually does.

Try using

Serial.println("Hello, world!");

Sure. Ill try it...Thank you.

okay sure ill note it.

now, please edit your first post, and fix the code tags.

first, click the </> icon and paste your code in the highlighted area

from what i can see in your image

  • what is "myPort"?
  • why aren't you using Serial.begin(), Serial.available(), Serial.readStringUntil() and Serial.println()?
  • why do you always print val, regardless i anything is available to be read?

okay lemme check it

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.