Hello,
I'm reading value from a rplidar A1M8 to send data from an arduino mega to an esp32 and i have some issues when the arduino is powered via vin.
My alimentation is 12v battery with a buck converter (max 3A)
Here's my code:
#include <RPLidar.h>
#include <ezButton.h>
ezButton StartSwitch(53);
ezButton ElevatorSwitchFront(52);
ezButton ElevatorSwitchBack(51);
#define RPLIDAR_MOTOR 3 // The PWM pin for control the speed of RPLIDAR's motor (MOTOCTRL).
#define USE_LIDAR true
RPLidar lidar;
void setup() {
Serial.begin(115200);
Serial2.begin(115200);
Serial3.begin(115200); // For RPLidar
lidar.begin(Serial3);
pinMode(RPLIDAR_MOTOR, OUTPUT); // set pin modes
StartSwitch.setDebounceTime(50);
ElevatorSwitchFront.setDebounceTime(50);
ElevatorSwitchBack.setDebounceTime(50);
}
float minDistance = 100000;
float angleAtMinDist = 0;
void loop() {
StartSwitch.loop();
ElevatorSwitchFront.loop();
ElevatorSwitchBack.loop();
int startState = StartSwitch.getState();
int frontElevatorState = ElevatorSwitchFront.getState();
int backElevatorState = ElevatorSwitchBack.getState();
if(USE_LIDAR){
if (IS_OK(lidar.waitPoint())) {
//perform data processing here...
float distance = lidar.getCurrentPoint().distance;
float angle = lidar.getCurrentPoint().angle; // 0-360 deg
if (lidar.getCurrentPoint().startBit ) {
// a new scan, display the previous data...
Serial.print("lidar:");
Serial.print(minDistance);
Serial.print("/");
Serial.println(angleAtMinDist);
Serial2.print("lidar:");
Serial2.print(minDistance);
Serial2.print("/");
Serial2.println(angleAtMinDist);
Serial.print("switch:");
Serial.print(startState);
Serial.print("/");
Serial.print(frontElevatorState);
Serial.print("/");
Serial.println(backElevatorState);
Serial2.print("switch:");
Serial2.print(startState);
Serial2.print("/");
Serial2.print(frontElevatorState);
Serial2.print("/");
Serial2.println(backElevatorState);
angleAtMinDist = 0;
minDistance = 100000;
} else {
if ( distance > 0 && distance < minDistance) {
minDistance = distance;
angleAtMinDist = angle;
}
}
}
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();
analogWrite(RPLIDAR_MOTOR, 255);
delay(3000);
}
}
}
else{
Serial.print("switch:");
Serial.print(startState);
Serial.print("/");
Serial.print(frontElevatorState);
Serial.print("/");
Serial.println(backElevatorState);
Serial2.print("switch:");
Serial2.print(startState);
Serial2.print("/");
Serial2.print(frontElevatorState);
Serial2.print("/");
Serial2.println(backElevatorState);
delay(150);
}
}
(I'm also sending data from limit switchES)
-The code works very well when the arduino is powered via usb and I retrieve all the data I need
-The code does not work when powered by vin (I checked with a multimeter and it's 5V) , The motor of the lidar does not start
-when USE_LIDAR is at false it works well and I retrieve my limit switch states on the esp
-If the usb is connected and i powered the arduino via vin and then i unplugged the usb port, the esp still retrieve the data.
If you have any idea how to have this working without plugging the arduino each time i would really appreciate it
Rodolphe