Error compiling for board Arduino Mega or Mega 2560

Is the ground of the external supply also connected to the Arduino ground.
Can you provide a simple hand drawn sketch of your setup?

The ground of the external supply is only connected to the breadboard, and the power supply is plugged into the arduino. Will do now, thanks

Here it is, thanks:

A power connection through the barrel jack needs to be at least 7v.

Is the external supply a regulated 5v supply? If so, the Arduino can be powered from the 5v pin from the external regulated supply. The power supply ground, the Arduino ground and the module ground should all be connected.

Yes, the power supply is a 5v regulated supply. I've got rid of the external power supply and it still works the same way as before, so I've connected both 5v and gnd to arduino, as it is powered by the laptop. This is what the serial monitor shows, reads 0 until I open the serial monitor, when it shows the other characters:

I don't quite understand this. How can you see a print out if the serial monitor is not open?

The sketch has the Serial at 115200 baud but the monitor is set at 9600. The Serial.begin( baudrate) and the baudrate selected in the monitor pulldown need to agree. They should both be at 115200.

2 Likes

Sorry for the misunderstanding, once I open the serial monitor it shows all the data showing 0s, then after like a second it shows the other characters.

Thank you so much for that, it's just fixed that problem. Now the last thing we would need to get are the actual values, as the lidar works in the developer's app :grinning: Do I need the IMU for this?

Please post your latest code.
I think you might need one of these
lidar.begin(Serial1);
lidar.begin(&Serial1);

#include <RPLidar.h>

#define RPLIDAR_MOTOR 3
#define SDCARD_MODULE 4

RPLidar lidar;

void setup() {
  Serial.begin(115200);
  lidar.begin(Serial);
  pinMode(RPLIDAR_MOTOR, OUTPUT);
  digitalWrite(RPLIDAR_MOTOR, HIGH);
}

void loop() {
  bool dataAvailable = lidar.waitPoint();
  if (dataAvailable) {
    float distance = lidar.getCurrentPoint().distance;
    float angle = lidar.getCurrentPoint().angle;
    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.print(" cm, Angle: ");
    Serial.print(angle);
    Serial.println();
  }
}

lidar.begin(Serial1);

Just changed it to lidar.begin(Serial) to lidar.begin(Serial1) and still gives 0

I'm still using the same connections as on the hand drawn diagram, just to let you know (pins 3 and 19))

also add a Serial1.begin(115200) before the lidar.begin(Serial1)

1 Like

Working on it now, thanks!

I've checked using both lidar.begin(Serial) and Serial1 simultaneously, and using serial1.print instead of serial.print, but none of those options displayed any message at all.

When you had the lidar device communicating with the pc over the usb/ttl adaptor what was the baud rate you used?

115200 too

You are correct about the default baud rate of the module being 115200.
I think the sketch should look like this with a few serial debug statements added

#include <RPLidar.h>

#define RPLIDAR_MOTOR 3
#define SDCARD_MODULE 4

RPLidar lidar;

void setup() {
  Serial.begin(115200);
  Serial1.begin(115200);
  byte statusS = lidar.begin(Serial1);
  Serial.println(statusS); //should print 1
  byte statusO = lidar.isOpen();
  Serial.println(statusO); //should print 1
  pinMode(RPLIDAR_MOTOR, OUTPUT);
  digitalWrite(RPLIDAR_MOTOR, HIGH);
}

void loop() {
  bool dataAvailable = lidar.waitPoint();
  if (dataAvailable) {
    float distance = lidar.getCurrentPoint().distance;
    float angle = lidar.getCurrentPoint().angle;
    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.print(" cm, Angle: ");
    Serial.print(angle);
    Serial.println();
  }
}
1 Like

Hi, just checked it, but unfortunately it's not working very well. It has shown a few distance and angle measurements but then stopped, I wonder whether this is because of Arduino Mega's capabilities? If so, there's nothing else we can do

That's good progress. There are two things to try.

  1. What do you see if you don't run the motor to rotate the lidar.

  2. Change from the default timeout for .waitPoint() of 500 ms to something like 2 seconds
    bool dataAvailable = lidar.waitPoint(2000);

1 Like