0

I want to read data from Arduino. My Arduino code is simple:

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

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

In Processing I have the following code

import processing.serial.*;

Serial myPort;
String val;

void setup()  {

    String portName = Serial.list()[1];
    myPort = new Serial(this, portName, 9600);
}

void draw() {

    if (myPort.available() > 0)
    {
        // If data is available,
        val = myPort.readStringUntil('\n');
    }
    println(val); // pr
}

But val is always Null. I don't understand why it returns this value every time. The port is available.

2 Answers 2

1

In Processing, if you put in:

if (val == null) {
    val = "0";
}

it should work. It'll keep printing 'val' without halting the program, but it'll return a value of "0" instead of "null".

Basically, you have to tell Processing to run even if it receives a "null" value.

Sign up to request clarification or add additional context in comments.

Comments

0

The code posted should work. The things to check here are whether you are using the correct serial port. Try printing out the list of serial ports in setup:

println( Serial.list() );

If your code is correct, the port you want will be the second one. If that's not correct, change the number of the array index when you specify portName.

The other thing to check is whether the Arduino is actually printing what you think it's printing. When you open up the Serial Monitor, is it actually printing "Hello, world!" a bunch of times?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.