I need help getting this Lidar code up and Running

It uses a Benewake TF Mini Plus Lidar module, a nema 8, a nema 17, two DRV8825s, a LM393 Infrared motor sensor, and a limit switch to generate 3d point cloud models, when I go to verify it, it gives me a whole bunch of errors

#include <AccelStepper.h>
#include <SoftwareSerial.h>


// Define stepper motor connections and parameters
#define DIR_PIN_TILT   6 // Direction pin for NEMA 8 tilt axis
#define STEP_PIN_TILT  7 // Step pin for NEMA 8 tilt axis
#define DIR_PIN_PAN    8 // Direction pin for NEMA 17 pan axis
#define STEP_PIN_PAN   9 // Step pin for NEMA 17 pan axis
#define LIMIT_SWITCH_PIN 10 // Pin for the limit switch
#define IR_SENSOR_PIN    A0 // Pin for the LM393 Infrared sensor
#define STEPS_PER_REV_PAN 600 // Steps per revolution for NEMA 17 pan axis
#define RPM_TILT       60 // RPM for NEMA 8 tilt axis

// Define variables for stepper motors
AccelStepper stepperTilt(AccelStepper::DRIVER, STEP_PIN_TILT, DIR_PIN_TILT);
AccelStepper stepperPan(AccelStepper::DRIVER, STEP_PIN_PAN, DIR_PIN_PAN);

// Define software serial port for TF mini Plus LiDAR
SoftwareSerial lidarSerial(2, 3); // RX, TX

// Variables for LiDAR measurements
int dist; //actual distance measurements of LiDAR
int strength; //signal strength of LiDAR
int check; //save check value
int i;
int uart[9]; //save data measured by LiDAR
const int HEADER=0x59; //frame header of data package

// Variables for limit switch and IR sensor
bool panLimitSwitchState = false;
bool tiltIRSenssorState = false;
bool panHomeReached = false;

void setup() {
  Serial.begin(9600); // Initialize serial port for debugging
  lidarSerial.begin(115200); // Initialize serial port for TF mini Plus LiDAR
  
  // Prompt user to enter "Run" to start LiDAR scan
  Serial.println("Enter 'Run' to initiate LiDAR scan:");
}

void loop() {
  // Check for user input
  if (Serial.available() > 0) {
    // Read user input
    String userInput = Serial.readStringUntil('\n');
    
    // Check if user input matches "Run"
    if (userInput.trim() == "Run") {
      // Set up motor parameters
      stepperTilt.setMaxSpeed(RPM_TILT * STEPS_PER_REV_PAN / 60); // Convert RPM to steps per second
      stepperTilt.setAcceleration(1000); // Set acceleration for smooth movement
      
      stepperPan.setMaxSpeed(STEPS_PER_REV_PAN); // Max speed for NEMA 17 pan axis
      stepperPan.setAcceleration(1000); // Set acceleration for smooth movement
      
      // Initialize limit switch and IR sensor pins
      pinMode(LIMIT_SWITCH_PIN, INPUT_PULLUP); // Set pin for limit switch as input with internal pull-up resistor
      pinMode(IR_SENSOR_PIN, INPUT); // Set pin for IR sensor as input
      
      // Homing procedure for pan axis
      Serial.println("Homing the pan axis...");
      while (!panHomeReached) {
        stepperPan.runSpeed();
        if (digitalRead(LIMIT_SWITCH_PIN) == LOW) {
          stepperPan.setCurrentPosition(0);
          panHomeReached = true;
        }
      }
      stepperPan.stop();
      Serial.println("Pan axis homed.");
      
      // Homing procedure for tilt axis
      Serial.println("Homing the tilt axis...");
      while (digitalRead(IR_SENSOR_PIN) == HIGH) {
        stepperTilt.runSpeed();
      }
      stepperTilt.stop();
      stepperTilt.setCurrentPosition(0);
      Serial.println("Tilt axis homed.");
      
      // Break out of the input loop
      break;
    } else {
      // Prompt the user again for valid input
      Serial.println("Invalid input. Please enter 'Run' to initiate LiDAR scan:");
    }
  }

  // Read data from LiDAR
  if (lidarSerial.available()) { // Check if serial port has data input
    if(lidarSerial.read() == HEADER) { // Assess data package frame header 0x59
      uart[0]=HEADER;
      if (lidarSerial.read() == HEADER) { // Assess data package frame header 0x59
        uart[1] = HEADER;
        for (i = 2; i < 9; i++) { // Save data in array
          uart[i] = lidarSerial.read();
        }
        check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];
        if (uart[8] == (check & 0xff)) { // Verify the received data as per protocol
          dist = uart[2] + uart[3] * 256; // Calculate distance value
          strength = uart[4] + uart[5] * 256; // Calculate signal strength value
          Serial.print("Distance = ");
          Serial.print(dist); // Output measured distance value of LiDAR
          Serial.print('\t');
          Serial.print("Signal Strength = ");
          Serial.println(strength); // Output signal strength value
        }
      }
    }
  }

  // Check if limit switch for Pan axis is pressed
  panLimitSwitchState = digitalRead(LIMIT_SWITCH_PIN);
  if (panLimitSwitchState == LOW) {
    // Reset the position of the pan stepper motor to 0
    stepperPan.setCurrentPosition(0);
    panHomeReached = true;
  }

  // Check if IR sensor for Tilt axis detects an obstacle
  tiltIRSenssorState = digitalRead(IR_SENSOR_PIN);
  if (tiltIRSenssorState == HIGH) {
    // Reset the position of the tilt stepper motor to 0
    stepperTilt.setCurrentPosition(0);
  }

  // Move the tilt motor one step
  stepperTilt.runSpeed();
  
  // Move the pan motor one step when the tilt motor completes one revolution
  if (stepperTilt.distanceToGo() == 0) {
    if (!panHomeReached) {
      stepperPan.runSpeed();
    }
  }

  // Check if the pan motor has rotated 180 degrees
  if (stepperPan.currentPosition() >= STEPS_PER_REV_PAN * 180 / 360) {
    // Stop the pan motor
    stepperPan.stop();
    // Move the pan motor back to the home position
    stepperPan.moveTo(0);
    panHomeReached = false;
  }

  // Output angles of stepper motors to serial port
  float tiltAngle = stepperTilt.currentPosition() * 360.0 / STEPS_PER_REV_PAN; // Calculate tilt angle
  float panAngle = stepperPan.currentPosition() * 360.0 / STEPS_PER_REV_PAN; // Calculate pan angle
  Serial.print("Tilt Angle: ");
  Serial.print(tiltAngle);
  Serial.print(" degrees, Pan Angle: ");
  Serial.print(panAngle);
  Serial.println(" degrees");

  // If the pan motor has returned to the home position, restate the previous serial message
  if (panHomeReached && stepperPan.distanceToGo() == 0) {
    Serial.println("Enter 'Run' to initiate LiDAR scan:");
    panHomeReached = false;
  }
}

Welcome and thanks for using code tags bin your first post.

You will have to post the errors. Copy all of them and paste here in a new reply; please use code tags when posting them.

Please tell use which board you're using.

Your problem does not sound like it is related to problems with the IDE and hence has been moved to a more suitable location on the forum.

The trim method in the String class does not return a value. It modifies the String in place.

With STEPS_PER_REV_PAN equal to 600 and RPM_TILT equal to 60, the expression RPM_TILT * STEPS_PER_REV_PAN / 60 will overflow the limit of a 16 bit integer. Declare at least one constant as a long type.

      // Break out of the input loop
      break;

This statement is not with a loop. If you intend to break out of the loop() function early, perhaps you meant to use return;?

And once again, you have a 16 bit integer overflow in the expression STEPS_PER_REV_PAN * 180 / 360. At least once of the constants needs to be a long type.

Did you write this code yourself? If so, I find it extremely odd that you're unable to deal with the very clear error messages that the compiler provided you with.

Hi, @cmdub1211
Welcome to the forum.

Did you write all that code at once, instead of in stages?

One of the biggest mistakes programmers make is to try and implement too much at a time without spending adequate time testing each section.
As a result, bugs start piling on top of other bugs, not only making them harder to find and isolate, but also making the number of bugs needing be squashed seem unmanageable.

A much better idea is to work incrementally.
Write your code in sections.
After each section of code has been written, think, “How am I going to test this?”, and then actually test it. If you find any bugs, either in the code you’ve just written, or in code that your code is relying on, those bugs should be taken care of immediately.

Focus down on one area at a time.
Then when each section has been debugged and working, combine sections ONE at a TIME, each time getting that part bug free before adding the next.

It sounds laborious, BUT it is the most efficient and quickest way to get your code working.

Tom.. :smiley: :+1: :coffee: :australia:

I originally tried to write this code on my own, but I'm a beginner to most of this stuff, so I tried to see how chatgpt would go at creating this code, then I modified my code to include some of the suggestions it gave, here are the error messages just in case you want to see what was actually happening

/Users/carterwilson/Documents/Arduino/3d TFMini Plus LIDAR Scanner/3d TFMini Plus LIDAR Scanner.ino: In function 'void loop()':
/Users/carterwilson/Documents/Arduino/3d TFMini Plus LIDAR Scanner/3d TFMini Plus LIDAR Scanner.ino:50:26: error: invalid operands of types 'void' and 'const char [4]' to binary 'operator=='
     if (userInput.trim() == "Run") {
         ~~~~~~~~~~~~~~~~~^~~~~~~~
/Users/carterwilson/Documents/Arduino/3d TFMini Plus LIDAR Scanner/3d TFMini Plus LIDAR Scanner.ino:84:7: error: break statement not within loop or switch
       break;
       ^~~~~

exit status 1

Compilation error: invalid operands of types 'void' and 'const char [4]' to binary 'operator=='

I know that AI like Chatgpt are still prone to mistakes, so after using it's suggestions, I decided to debug my code to prevent these errors from happening, but no matter what I could do, the errors kept popping up, and this is how I got here.

I'm also using an Elegoo Uno R3, and I'm going to use Cloud Compare to visualize the raw data

So start with the basics, you write some code JUST for the LIDAR and get it working.

If you have problems with it your basic code, then show us what you have and we will help.

Tom... :smiley: :+1: :coffee: :australia:

Thanks, I’ll try that!

Software serial does not work reliably on an Uno at 115200, you need to decrease the rate to 57600 or less.
However, you need to communicate with the TFmini at 115200 in order to change the baudrate.
See this thread:

A very wise man said, "There is no royal road to learning."

You cannot use ChatGPT to write code that you are not capable of writing yourself.

ChatGPT is really good at coming up with answers that look good. Not so good at coming up with answers that are actually correct. You're wasting your time with it. Unless you're already proficient in what you're asking it about, you can't tell when it's just made stuff up!

And by not admitting that you tried to shortcut your way right from the start, you've wasted my time. So I won't be helping you any further.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.