After changing the baud rate and simplifying the code with GTP.
We tested the wiring, power supply from the laptop port, and connection between Mega and Laptop is all right.
Sleep tight Tom.
Now the code is
#include <RPLidar.h>
RPLidar lidar;
void setup() {
Serial.begin(115200);
Serial1.begin(460800);
delay(1000);
Serial.println("Initializing RPLIDAR...");
// Try to detect RPLIDAR...
if (lidar.begin(Serial1)) {
Serial.println("RPLIDAR detected!");
} else {
Serial.println("Failed to connect to RPLIDAR");
}
}
void loop() {
// Read data from RPLIDAR
if (lidar.waitPoint()) {
float distance = lidar.getCurrentPoint().distance; // Distance in mm
float angle = lidar.getCurrentPoint().angle; // Angle in degrees
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" mm, Angle: ");
Serial.println(angle);
}
}