How to extract the distance from HMMD_wave_sensor program

Hello

I have built a model boat with a figure on the fore deck that can rotate the body to the left and to the right and rise his arm and start playing a sound though a loudspeaker. The recorded sound tell ”ship O´hoj”. Greetings to meeting boats. The movements and arm raising is controlled by a Arduino nano. In order to detect meeting boats I have tried different sensors. The one I am now testing is HMMD_mmwave_sensor. it works fine with the enclosure program found on the net.

I intend to use two sensors (one on each side). The program is made in a advanced way and I do not know how to change the program. What I want is catch the calculated distance and save it in order to be able to make a pin HIGH. If I have the the distance i a register I can also use different terms in order to make different things happen related to the distance HIGH pin can then be used to start the figure movements.

Please help me.

Best regards

Åke Silén

Encl.

#include <SoftwareSerial.h>

int val=0;

SoftwareSerial mySerial(5, 4); // RX, TX

void setup() {

// Start the serial communication with a baud rate of 115200

Serial.begin(115200);

mySerial.begin(115200);

// Wait for the serial port to initialize

while (!Serial) {

delay(100);

}

// Hex string to send

String hex_to_send = "FDFCFBFA0800120000006400000004030201";

sendHexData(hex_to_send);

}

void loop() {

// Read and print serial data

readSerialData();

}

void sendHexData(String hexString) {

// Convert hex string to bytes

int hexStringLength = hexString.length();

byte hexBytes[hexStringLength / 2];

for (int i = 0; i < hexStringLength; i += 2) {

hexBytes\[i / 2\] = strtoul(hexString.substring(i, i + 2).c_str(), NULL, 16);

}

// Send bytes through software serial

mySerial.write(hexBytes, sizeof(hexBytes));

}

void readSerialData() {

// Read and print data from software serial

while (mySerial.available() > 0) {

char incomingByte = mySerial.read();

Serial.print(incomingByte);

}

}

Please, format your code and paste it in a <CODE> block for better reading and use.

Have you tested it in the environment it will be used? Does it detect only the other "ship" or the water, too?

Your code shows no "time/distance" measuring device or code.

You could possibly send a synchronization "time" (millis()), then send a "ping" (another millis()), then interpolated your synchronized send-time with your synchronized receive-time. Or, initiate an "echo" (receive and resend) to increase the distance in order to decrease the error.

Who made it and do you know for sure it actually works? The Baud rate for the software serial is way beyond what anyone has ever used and works.

The program is downloaded from Wiki. The program works fine when loaded into the Nano. It prints continuously out the distans in cm when hooked up to my Mac mini.

My question is how to catch the printed distance and put in a register for further program modifications?

Best regards

Aake Silen

We are still waiting for you to post the code and serial output also in a code block.

HMMD mmWave Sensor - Waveshare Wiki

I can not find what you are asking for. The sketch below is a "copy for forum" version made in Arduino IDE. Above the picture is a link to the file/Aake Silen

///////////////////////////////////////////////////////////////////////////////////

#include <SoftwareSerial.h>
int val = 0;
SoftwareSerial mySerial(5, 4); // RX, TX

void setup() {
// Start the serial communication with a baud rate of 115200
Serial.begin(115200);
mySerial.begin(115200);

// Wait for the serial port to initialize
while (!Serial) {
delay(100);
}

// Hex string to send
String hex_to_send = "FDFCFBFA0800120000006400000004030201";
sendHexData(hex_to_send);
}

void loop() {

readSerialData(); // Read and print serial data
}

void sendHexData(String hexString) {
// Convert hex string to bytes
int hexStringLength = hexString.length();
byte hexBytes[hexStringLength / 2];
for (int i = 0; i < hexStringLength; i += 2) {
hexBytes[i / 2] = strtoul(hexString.substring(i, i + 2).c_str(), NULL, 16);
}

// Send bytes through software serial
mySerial.write(hexBytes, sizeof(hexBytes));
}

void readSerialData() {
// Read and print data from software serial
while (mySerial.available() > 0) {
char incomingByte = mySerial.read();
Serial.print(incomingByte);
}
}

Find in the program where the value is printed. Use the variable being printed for whatever you want to do with it.

Generally you would want to input the entire line, determine if the line begins with “Range”, then convert the number to an integer. All your code is doing is sending whatever is received from the sensor directly to Serial, one character at a time, regardless of content.

That code is particularly convoluted, taking hex data in a String, converting to hex, and sending to the sensor, when you could directly declare a byte array with hex data and eliminate the String.

You missed Steps 1 and 2 from Post #2.

No. Your link is broken. This is a direct link to the ZIP file "demo" code you are using: https://files.waveshare.com/wiki/HMMD-mmWave-Sensor/HMMD_mmWave_Sensor.zip

This web site has a little more description, but uses Blynk protocol, but follows the WaveShare wiki. ESP32 + HMMD mmWave Sensor: Real Tests + Blynk IoT!

Seems the distance is here:

  String line = Serial2.readStringUntil('\n');
    .
    .
    String distanceStr = line.substring(6);
    int distance = distanceStr.toInt();

Thank you very much! It worked very well. If this will work to detect other modelboats will be tested. Another unknown effect is if the boat is rocking in the sea. I will save the distance recorded during a test period on a SD-card and then analyze the data. If it is not recognizing other boats I can always use it for demonstation.

The figure on the foredeck say ship O´hoy and I send Best regards, Åke Silén