Processing IDE code not working(not receiving data from arduino)

I make code that do not receive data from Arduino,, angle should be along x-axis and h_dot should be along y-axis(something wrong with outline along x and y axis

import processing.serial.*;

Serial port;
float angleX;
float velocityY;

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(angleX, 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(velocityY, 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: ")) {
      angleX = float(data.substring(7));
    } else if (data.startsWith("h_dot: ")) {
      velocityY = 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

awaiting for kind reply

thank you very much
Capture

That prints a list of the inputs. This list includes the name of the input plus a number in brackets. So when you do this:-

You have to use the number in brackets not the name. So if this is your only input, and comes up with 0 against the COM5 name then you use:-

port = new Serial(this, 0, 9600); // Replace "COM5" with the appropriate port number

1 Like

i have changed in code some error come

Can you post the list of ports that you get in the console window when you try this please.

I think that you're mistaken. I use the name (COMx) and it works fine.

I suggest that you start by printing the received String in the serialEvent; before the trim(). Only than you know if you did receive something.

Well that is what I have been doing for the last 22 Years, ever since the very first version of processing. Before even the Arduino got going.

However I did try putting in the string of the port I wanted to use and that worked too.

1 Like

Can you please post the Arduino code you are using.

Normally I use two way communication between the Arduino and Processing, so that Processing is prompting the Arduino in response to a change in some control and the Arduino is sending back the information.

You must not have the Arduino serial monitor open because that swallows any data and processing then cannot connect to it.

So what prompts your Arduino to send data?

1 Like

Sir Mike is one of the best leader , guider of this forum , no doubt

please find the code

#include <Wire.h>    //Include wire library 
#include <MPU6050_light.h>  //Include library for MPU communication
#include <LiquidCrystal_I2C.h>  //Library for LCD Display

MPU6050 mpu(Wire);   //Create object mpu
LiquidCrystal_I2C lcd(0x27, 16, 2);     //Define LCD address and dimension

unsigned long timer = 0;    

void setup() {

  Serial.begin(9600);    //Start serial communication

  lcd.begin();     //Start LCD Display
  lcd.backlight();    

  Wire.begin();     
  mpu.begin();     
  Serial.print(F("MPU6050 status: "));
  Serial.println(F("Calculating offsets, do not move MPU6050"));   
  delay(1000);
  mpu.calcGyroOffsets();     //Calibrate gyroscope
  Serial.println("Done!\n");

}
void loop() {
  mpu.update();    //Get values from MPU

  if ((millis() - timer) > 100) { // print data every 100ms
    timer = millis();
    lcd.clear();
    lcd.print(" Angle: ");
    int angleZ = mpu.getAngleZ();  // Get Z angle value from MPU
    lcd.print(angleZ);

 
    float b = 5.0;   // Value of b
    float L = 10.0;  // Value of L
    float theta = angleZ;  // Value of θ obtained from angleZ
    float s_dot = 1.0;  // Value of s^.

    float h_dot = (2.0 * sqrt(pow(b, 2) + pow(L, 2) - (2.0 * L * b * cos(radians(theta))))) * (s_dot) / (L * tan(radians(theta)));
    lcd.setCursor(0, 1);  // Set the LCD cursor to the second line
    lcd.print("h_dot: ");
    lcd.print(h_dot, 2);  // Print the calculated value of h_dot on the LCD
    delay(10);
      Serial.print(angleZ);
     Serial.print(",");
      Serial.println(h_dot, 2);
  }
}

this code print angle and h_dot in arduino , for graph angle should be along x -axis and h_dot is along y -axis .

Capture

kindly check this is com port

OK a couple of mistakes at both ends.
On the sending end you never send the headers to Processing
Your parsing in Processing requires either a header of
Serial.print("Angle: "); OR a header of Serial.print(h_dot, 2);
You are sending nether. The header needs to be followed by the numbers according to that data type.

Now in Processing you actually are getting data but you are making mistakes in the data parsing, first because the Arduino never sent them in the first place, and when they did the data was not being parsed correctly.

The way to find out what is going on in the serialEvent call is to use print statements to see where you get to. I have done this to the point of
identifying that:-

Parsed angle is NaN

NaN is short for Not A Number, so see if you can work out what you are doing wrong here.
This is the function so far:-

void serialEvent(Serial port) {
  String data = port.readStringUntil('\n');  // Read the received data until newline character
  if (data != null) {
    data = data.trim();
    print("trimmed data = ",data);
    if (data.startsWith("Angle: ")) {
      println("yes data startsWith angle");
      angleX = float(data.substring(7));
      println("Parsed angle is", angleX);
    } else if (data.startsWith("h_dot: ")) {
      velocityY = float(data.substring(7));
    }
  }
}

And yes you can use
port = new Serial(this, "COM5", 9600); // Replace "COM5" with the appropriate port name

1 Like

processing IDE receieve data like this


by replacing serial event

As @Grumpy_Mike explained, your Arduino code does not send what the Procressing side expects. Your screenshot does not show "Angle: " or "h_dot:" for the trimmed data.

 // Send data to Processing IDE
    Serial.print(angleZ);
    Serial.print(",");
    Serial.println(h_dot, 2);

is this code fine?

No, see mike's comment below

You have to send The sentence "Angle: " or "h_dot: " before you send the actual values.

this is modified algorathim

#include <Wire.h>
#include <MPU6050_light.h>
#include <LiquidCrystal_I2C.h>

MPU6050 mpu(Wire);
LiquidCrystal_I2C lcd(0x27, 16, 2);

unsigned long timer = 0;

void setup() {
  Serial.begin(9600);
  lcd.begin();
  lcd.backlight();
  Wire.begin();
  mpu.begin();
  Serial.print(F("MPU6050 status: "));
  Serial.println(F("Calculating offsets, do not move MPU6050"));
  delay(1000);
  mpu.calcGyroOffsets();
  Serial.println("Done!\n");
}

void loop() {
  mpu.update();

  if ((millis() - timer) > 100) {
    timer = millis();
    lcd.clear();
    lcd.print(" Angle: ");
    int angleZ = mpu.getAngleZ();
    lcd.print(angleZ);

    Serial.print("Angle: ");
    Serial.print(angleZ);
    Serial.print(",");
    
    float b = 5.0;
    float L = 10.0;
    float theta = angleZ;
    float s_dot = 1.0;

    float h_dot = (2.0 * sqrt(pow(b, 2) + pow(L, 2) - (2.0 * L * b * cos(radians(theta))))) * (s_dot) / (L * tan(radians(theta)));
    lcd.setCursor(0, 1);
    lcd.print("h_dot: ");
    lcd.print(h_dot, 2);

    Serial.println(h_dot, 2);
  }
}

You are still not sending what processing is expecting.
Processing is expecting two types of data. One starts with "Angle: "
OR the other starts with "h_dot: ".

You are not sending "h_dot" to Processing, you are sending it to the lcd but not to Processing.
But you seem to be sending two lots of data on the same message. Processing is not expecting this. If you want to send it all in one message then Processing has to expect just one message with all the data in it.
At the moment it is only expecting one or the other message, as you can see by this section :-

if (data.startsWith("Angle: ")) {
      println("yes data startsWith angle");
      angleX = float(data.substring(7));
      println("Parsed angle is", angleX);
    } else if (data.startsWith("h_dot: ")) {
      velocityY = float(data.substring(7));
    }

Please do not send images of the data you get back from Processing, just copy the console and paste it as code to your answers.

this is modified code please check

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