Processing IDE code not working (Arduino / Processing)

I make code that do not receive data from Arduino
Capture
, angle should be along x-axis and h_dot should be along y-axis

import processing.serial.*;

Serial port;
float Angle;
float h_dot;

void setup() {
  size(400, 300);
  printArray(Serial.list());  // Print available serial ports in the console
  port = new Serial(this, "COM5", 9600);  // Replace "COM5" with the appropriate port name
  port.bufferUntil('\n');  // Set the character to buffer until newline
}

void draw() {
  background(255);
  // Draw X-axis
  stroke(0);
  line(0, height, width, height/1);
  fill(0);
  textAlign(CENTER, CENTER);
  text("Angle (X-axis)", width/2, height/2 - 20);
  text(Angle, width/2, height/2 + 20);

  // Draw Y-axis
  stroke(0);
  line(width/2, 0, width/2, height);
  fill(0);
  textAlign(CENTER, CENTER);
  text("h_dot (Y-axis)", width/2 + 30, height/2);
  text(h_dot, width/2 - 30, height/2);
}

void serialEvent(Serial port) {
  String data = port.readStringUntil('\n');  // Read the received data until newline character
  if (data != null) {
    data = data.trim();
    if (data.startsWith("Angle: ")) {
      Angle = float(data.substring(7));
    } else if (data.startsWith("h_dot: ")) {
      h_dot = float(data.substring(7));
    }
  }
}

arduino code is working and getting both terms but processing is blank

please please guide me thank you

arduino is connected to processing i am sure because Arduino restart

thankyou

1 Like

We need to see the Arduino code also to see how you are sending the data.

2 Likes

Does that mean the variables angleX and velocityY are always shown as zero?

3 Likes

yes both zero not moving i moving mpu6050 sensor but no data printing

You appear to be printing to both an lcd and then sending the data to your Processing app. I presume that the lcd output is correct and that you have checked your output in SerialMonitor. As far as serial communication goes, personally I would forget the text labels "Angle: " and "h_dot: ". Furthermore, I can’t see that you ever sent the "h_dot: " label on the Arduino side by Serial, so you can’t expect to see it on the Processing side. I would send data like this:

Serial.print(firstNumber);
Serial.print(“,”); // creates comma separated values
Serial.println(secondNumber); // Note the line feed terminator.

Processing will then recreate the data string by first dividing it when it sees the linefeed. You can then split this string at the comma and reconstruct the data which you can then plot in the Processing app.

Temporarily forget about your demo and run the following to see if you can use the technique described above. If you can get this simple demo working then perhaps you can apply it to your project. Two random floats are sent by Arduino and then parsed in Processing:

Processing source code:

import processing.serial.*;

Serial myPort;    // The serial port
String inStr;    // Input string from serial port
int lf = 10;    // ASCII linefeed
float x, y;

void setup() {
  surface.setVisible(false); // Output is in console at bottom of editor
  // List all the available serial ports:
  printArray(Serial.list());
  // Enter device number for your system
  myPort = new Serial(this, Serial.list()[3], 9600);
  myPort.bufferUntil(lf);
}

void draw() {
}

void serialEvent( Serial myPort) {
  //'\n' (line feed) is delimiter indicating end of packet
  inStr = myPort.readStringUntil('\n');
  //make sure our data isn't empty before continuing
  if (inStr != null) {
    //trim whitespace and formatting characters (like carriage return and/or line feed)
    inStr = trim(inStr);  //Data is sent as a string
    println("inStr = ", inStr);
    String[] myData = split(inStr, ','); // String is split into array at comma
  //  printArray(myData);
    x = Float.valueOf(myData[0]); // Array elements are converted back to floats
    println("x = ", x);
    y = Float.valueOf(myData[1]);
    println("y = ", y);
  }
}

Arduino code:

void setup() {
 Serial.begin(9600);
}

void loop() {
   // print a random number from 0 to 99
  Serial.print(random(100));
  Serial.print(','); // Comma separated values
  // print a random number from 0 to 9
  Serial.println(random(10)); // line feed terminated
  delay(50);
}