Problem interfacing arduino using RPLIDAR

For my project, I am using a RPLIDAR to connect with arduino for receiving data.
First, i have to refer from https://github.com/AerospaceRobotics/RoboPeak-LIDAR/blob/master/SensorPrecision/singleDirection/singleDirection.ino#L51.

I have to try this code, but it not operate.

And i have to try with the orignal code form ROBOPEAK.
when i added some code,such as Serial.begin(9600);, Serial.println(distance);, it also not operate.
So, how can i correctly receive the data and observe the date at arduino or from the visual studio for next part.
Many THANKS.

// This sketch code is based on the RPLIDAR driver library provided by RoboPeak
#include <RPLidar.h>

// You need to create an driver instance 
RPLidar lidar;

#define RPLIDAR_MOTOR 3 // The PWM pin for control the speed of RPLIDAR's motor.
                        // This pin should connected with the RPLIDAR's MOTOCTRL signal 
                       
                        
void setup() {
  // bind the RPLIDAR driver to the arduino hardware serial
  lidar.begin(Serial);
  
  // set pin modes
  pinMode(RPLIDAR_MOTOR, OUTPUT);
}

void loop() {
  if (IS_OK(lidar.waitPoint())) {
    float distance = lidar.getCurrentPoint().distance; //distance value in mm unit
    float angle    = lidar.getCurrentPoint().angle; //anglue value in degree
    bool  startBit = lidar.getCurrentPoint().startBit; //whether this point is belong to a new scan
    byte  quality  = lidar.getCurrentPoint().quality; //quality of the current measurement
    
    //perform data processing here... 
    
    
  } else {
    analogWrite(RPLIDAR_MOTOR, 0); //stop the rplidar motor
    
    // try to detect RPLIDAR... 
    rplidar_response_device_info_t info;
    if (IS_OK(lidar.getDeviceInfo(info, 100))) {
       // detected...
       lidar.startScan();
       
       // start motor rotating at max allowed speed
       analogWrite(RPLIDAR_MOTOR, 255);
       delay(1000);
    }
  }
}

I think that you should try to change the lidar.begin(Serial) to lidar.begin(Serial3)
because serial is occupied by computer ,hope i can help you~~

Hello Alexander,

as @anyway1997 says, You need to use another Serial port as Serial is occupied by computer.

As far as my assumptions you are using Arduino UNO, You need to use Arduino Mega for this project and plugged in Serial1

As can be seen in the Example code in the given link :

#include <RPLidar.h>
// You need to create an driver instance 
RPLidar lidar;
#define RPLIDAR_MOTOR 3 // The PWM pin for control the speed of RPLIDAR's motor.
// This pin should connected with the RPLIDAR's MOTOCTRL signal 
bool transmit = false;
byte incomingByte;
void setup() {
// bind the RPLIDAR driver to the arduino hardware serial
  lidar.begin(Serial1);
  Serial.begin(115200);

I have similar problem.
My rplidar stop motor as it should do when arduino doesn’t recieve response from rplidar.
I’m using arduino leonardo.
Interesting - when I press reset button on leonardo rplidar starts spinnig, but it still not start scanning.

#include <RPLidar.h>

// You need to create an driver instance
RPLidar lidar;
int ledPin =  LED_BUILTIN;
#define RPLIDAR_MOTOR 3 // The PWM pin for control the speed of RPLIDAR's motor.
// This pin should connected with the RPLIDAR's MOTOCTRL signal
bool transmit = false;
byte incomingByte;

void setup() {
  // bind the RPLIDAR driver to the arduino hardware serial
  Serial1.begin(115200);
  lidar.begin(Serial1);
  
  Serial.begin(115200);
  while (!Serial) {
    ; //
  }

  Serial.println("Setup");
  pinMode(ledPin, OUTPUT);
  // set pin modes
  pinMode(RPLIDAR_MOTOR, OUTPUT);
  analogWrite(RPLIDAR_MOTOR, 255);
}

void loop() {
  Serial.println("Loop start");
  if (IS_OK(lidar.waitPoint())) {
    digitalWrite(ledPin, HIGH);
    float distance = lidar.getCurrentPoint().distance; //distance value in mm unit
    float angle    = lidar.getCurrentPoint().angle; //anglue value in degree
    bool  startBit = lidar.getCurrentPoint().startBit; //whether this point is belong to a new scan
    byte  quality  = lidar.getCurrentPoint().quality; //quality of the current measurement
    if (Serial.available() > 0) {
      incomingByte = Serial.read();
      transmit = !transmit;
    }
    Serial.println(distance);
    //perform data processing here...


  } else {
    analogWrite(RPLIDAR_MOTOR, 0); //stop the rplidar motor
    digitalWrite(ledPin, LOW);
    // try to detect RPLIDAR...
    rplidar_response_device_info_t info;
    if (IS_OK(lidar.getDeviceInfo(info, 100))) {
      // detected...
      lidar.startScan();
      analogWrite(RPLIDAR_MOTOR, 255);
      delay(1000);
    }
  }
}