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
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.
#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 .
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
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.
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.