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...