Hello everyone,
I'm working on a project involving a laser range finder from DF Robot (Laser Ranging Sensor UART 4m SKU SEN0491). I am using a Seeed Xiao NRF52840 microcontroller for this setup. My Arduino code reads sensor values over Serial, but I'm running into an issue where the sensor works fine for about 5 minutes and then freezes. By "freezing," I mean it starts reporting the same distance value over and over.
Problem:
- Laser works well for 5 minutes
- Then it "freezes," i.e., gets stuck on the same distance value
- Requires power cycling the laser to recover
- Upon recovery, it freezes again after approximately 4 minutes
Code:
#define DEBUG true
#define DEBUG_SERIAL \
if (DEBUG) Serial
void setup(void) {
Serial.begin(115200);
Serial1.begin(115200);
while (!Serial1);
//init_dac();
}
void loop() {
print_reply_wait();
delay(10);
}
void print_reply_wait()
{
//DEBUG_SERIAL.println("print/wait reply start");
int32_t timeoutStart = millis();
int32_t timeoutDuration = 100;
int32_t responseReceivedTimestamp = 0;
bool responseReceived = false;
const int bufferSize = 128;
char readBuffer[bufferSize];
int bufferIndex = 0;
while (millis() - timeoutStart < timeoutDuration && bufferIndex < bufferSize - 1)
{
if (Serial1.available())
{
if (!responseReceived)
{
responseReceived = true;
responseReceivedTimestamp = millis();
}
char incomingChar = (char)Serial1.read();
readBuffer[bufferIndex] = incomingChar;
bufferIndex++;
//#if DEBUG
//DEBUG_SERIAL.print(incomingChar);
//#endif
}
if (responseReceived && millis() - responseReceivedTimestamp >= 50)
{
break; // Break the loop if a response was received and 200ms have passed
}
}
readBuffer[bufferIndex] = '\0'; // Null-terminate the string
char* startPtr = strstr(readBuffer, "d:");
if (startPtr != NULL)
{
startPtr += 3; // Move the pointer to the beginning of the distance value
char* endPtr = strstr(startPtr, " mm");
if (endPtr != NULL)
{
*endPtr = '\0'; // Null-terminate the distance string
int distance = atoi(startPtr);
DEBUG_SERIAL.print("Extracted distance: ");
DEBUG_SERIAL.println(distance);
}
}
print_reply();
//DEBUG_SERIAL.println("print/wait reply end");
}
void print_reply()
{
while (Serial1.available())
{
#if DEBUG
DEBUG_SERIAL.println("leftovers remain");
DEBUG_SERIAL.print((char)Serial1.read());
DEBUG_SERIAL.println("leftovers remain end");
#else
Serial1.read();
#endif
}
}
Things I've Tried:
- Power-cycling the laser
- Checked the buffer read and write operations
Questions:
- Could I be reading the serial port too fast or too slow? The laser's documentation isn't very comprehensive.
- Is there a potential issue with the buffer size or the way I'm reading the Serial data?
- Could this be a hardware issue? I don't face the problem when the microcontroller is reset, only when the laser is power-cycled.
I would greatly appreciate any insights or suggestions on how to resolve this issue. Thank you in advance for your time and help!