Help, Serial Monitor only outputs 0 over and over

Im using and RPLIDAR A1 and collecting data to control a servo, however, the data coming out of the serial monitor which is supposedly the distance readings from the RPLIDAR is repeatedly 0 with the occasional "________" as programmed. Im not sure why, the wiring is all correct and supposedly my code makes sense too. Someone please help

My Code:

#include <RPLidar.h>

RPLidar lidar;
#define RPLIDAR_MOTOR 3
int degreeArray[200];
int distanceArray[200];
const int RPL = 200;
const int servopin = 10;
int maxvalue = 0;
int i;
int wantdegree;
int wantedIndex;
int k;

void moveServo(int pin, int angle) {
  // Convert angle to pulse width
  int pulseWidth = map(angle, 0, 180, 500, 2500);
  // Send pulse to servo
  digitalWrite(pin, HIGH);
  delayMicroseconds(pulseWidth);
  digitalWrite(pin, LOW);
}

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

void loop() {
  // Populate arrays
  if (IS_OK(lidar.waitPoint())) {
    for (i = 0; i < RPL; i++) {
      float distance = lidar.getCurrentPoint().distance;
      float angle    = lidar.getCurrentPoint().angle;
      
      if (angle <= 180) {
        degreeArray[i] = angle;
        distanceArray[i] = distance;
      } 
    }
  }
  for (i = 0; i < RPL; i++) {
    Serial.print(distanceArray[i]);
  }
  Serial.print("__________________");

  // Print array values


  // Find maximum distance
  for (i = 0; i < RPL; i++) {
    if (distanceArray[i] > maxvalue) {
      maxvalue = distanceArray[i];
      wantedIndex = i;
      wantdegree = degreeArray[i];
    }
  }

  // Move servo to the angle with maximum distance
  moveServo(servopin, wantdegree);

  // Clear arrays
  memset(degreeArray, 0, sizeof(degreeArray));
  memset(distanceArray, 0, sizeof(distanceArray));
}

Output Example:

0000000000000000000000000000000000000000000000000000000________________0000000000000000000000000000000000000____________________000000000000...

You are storing floats in an integer array. What do you see if you use float arrays?

Have you run a library example to confirm the hardware setup?

Comparing this post with yesterday's post, it looks like you still have not familiarized yourself with the RPLidar library examples, and verified the basic operation and functions of the sensor.

Starting from the simplest library example, add code that prints out a 360 degree scan of the room, and make sure that the results are sensible. Then you can start thinking of applications.

The code associated with this unimpressive Youtube presentation might give you some ideas about what you are missing.

Dronebotworkshop shows how to use the TF and RP LIDAR with video, transcript and code...

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