2025-06-24 15:18:08 -07:00

60 lines
1.6 KiB
C++

/*
* Example sketch for reporting on readings from the LD2410 using whatever settings are currently configured.
*
* The sketch assumes an ESP32 board with the LD2410 connected as Serial1 to pins 8 & 9, the serial configuration for other boards may vary
*
*/
#include <ld2410.h>
ld2410 radar;
uint32_t lastReading = 0;
void setup(void)
{
Serial.begin(115200); //Feedback over Serial Monitor
//radar.debug(Serial); //Uncomment to show debug information from the library on the Serial Monitor. By default this does not show sensor reads as they are very frequent.
Serial1.begin (256000, SERIAL_8N1, 9, 8); //UART for monitoring the radar
delay(500);
Serial.print(F("\nLD2410 radar sensor initialising: "));
if(radar.begin(Serial1))
{
Serial.println(F("OK"));
}
else
{
Serial.println(F("not connected"));
}
}
void loop()
{
radar.read();
if(radar.isConnected() && millis() - lastReading > 1000) //Report every 1000ms
{
lastReading = millis();
if(radar.presenceDetected())
{
if(radar.stationaryTargetDetected())
{
Serial.print(F("Stationary target: "));
Serial.print(radar.stationaryTargetDistance());
Serial.print(F("cm energy:"));
Serial.println(radar.stationaryTargetEnergy());
}
if(radar.movingTargetDetected())
{
Serial.print(F("Moving target: "));
Serial.print(radar.movingTargetDistance());
Serial.print(F("cm energy:"));
Serial.println(radar.movingTargetEnergy());
}
}
else
{
Serial.println(F("No target"));
}
}
}