Can you write ANNEX32 BASIC code to communicate with a HLK-LD2410B?
Assistant
Here's a sample ANNEX32 BASIC code to communicate with a HLK-LD2410B human presence radar sensor:
text
RX_pin = 21 'Serial RX pin connected to LD2410B
TX_pin = 25 'Serial pin connected to LD2410B RX
'Initialize serial communication
de serial2.mode 256000, RX_pin, TX_pin
pause 100
wend
This code initializes serial communication with the LD2410B sensor, enables configuration and engineering modes, then continuously reads and parses the radar data. It extracts the target state, moving target distance, and static target distance from the sensor output.cicciocb+2
The code uses Serial2 for communication, sets the baud rate to 256000, and specifies the RX and pins. It sends the necessary commands to enable configuration and engineering modes. In the main loop, it reads available data, converts it to hexadecimal, and parses the relevant information when a valid data frame is detected.peterneufeld.wordpress+1
You may need to adjust the pin numbers and add error handling as needed for your specific setup. This code provides a basic framework for interfacing with the HLK-LD2410B sensor using ANNEX32 BASIC.
Share
Export
Thank you for pointing that out. Let's ensure we follow the correct syntax and conventions as per the Annex32 BASIC help file. Here's the revised code with proper variable usage and correct commands:
text
' Pin definitions
const RX_pin = 21 ' Serial RX pin connected to LD2410B
const TX_pin = 25 ' Serial pin connected to LD2410B RX
' Global variables for radar data
dim TState as integer
dim as float
dim STDis as float
dim LastDetectionTime as integer
dim AlarmPlayed as boolean
' Initialize serial communication
SERIAL2.MODE 256000, , TX_pin
' Enable configuration mode for LD2410B
sub EnableConfig()
Enable_config="fdfcfbfa0400ff00010004030201"IObuff.FromHex(1,Enableconfig)
SERIAL2.WRITE_IOBUFF(1)
end sub
' Enable engineering mode for LD2410B
sub EnableEngineering()
Enable_engineering="<REDACTED>"IObu)
SERIAL2.WRITE_IOBUFF(1)
end sub
' Check if a breathing body is detected but no motion
if TState = 2 and <REDACTED> = 0 and STDis > 0 then
if not AlarmPlayed then
print "Breathing body detected with no motion. Playing alarm."
PLAY.MP3 "/alarm.mp3"
LastDetectionTime = TICKS
AlarmPlayed = true
endif
endif
endif
end sub
' Serial2 event handler
sub SERIAL2_EVENT()
while SERIAL2.AVAILABLE > 0
RADAR=SERIAL2.READSTRING
ParseRadarData(RADAR$)
wend
end sub
' Main program
PLAY.SETUP 0 ' Set up audio output to internal speaker
EnableConfig()
pause 100
EnableEngineering()
pause 100
' Main loop
while 1
' Display current status
cls
print "Radar Sensor Status:"
print "Target State: "; TState
print "Moving Target Distance: "; MTDis; " m"
print "Static Target Distance: "; STDis; " m"
' Check if detection was recent
if (TICKS - LastDetectionTime) < 5000 then
print "Recent Detection: Yes"
else
print "Recent Detection: No"
AlarmPlayed = false
endif
' Add any other system checks or operations here
pause 1000 ' Update every second
wend
Explanation:
Global Variables:
TState, , , LastDetectionTime, and AlarmPlayed are declared correctly using dim.
Serial Communication:
SERIAL2.MODE 256000, RX_pin, TX_pin initializes the serial communication with the correct baud rate and pin assignments.
Subroutines:
EnableConfig and EnableEngineering subroutines send the necessary commands to the radar sensor.
ParseRadarData subroutine parses the radar data and plays the alarm if the conditions are met.
Event Handling:
SERIAL2_EVENT subroutine handles incoming serial data and calls ParseRadarData.
Main Loop:
The main loop displays the current status of the radar sensor and checks if there has been a recent detection.
It resets the AlarmPlayed flag if no recent detection is found.
This code should now be more in line with the actual Annex32 BASIC syntax and conventions as described in the help file. It initializes the radar sensor, processes the data, and plays an alarm when a breathing body is detected without motion, while providing continuous status updates.
Share
Export
I apologize for the confusion. You are absolutely correct, and thank you for pointing that out. The Annex32 BASIC help file indeed mentions MILLIS and not TICKS. Let's correct our code to use MILLIS instead.
Here's the revised code using MILLIS:
text
' Pin definitions
const RX_pin = 21 ' Serial RX pin connected to LD2410B
const TX_pin = 25 ' Serial pin connected to LD2410B RX
' Global variables for radar data
dim TState ' Numeric variable
dim MTDis ' Numeric variable
dim STDis ' Numeric variable
dim LastDetectionTime ' Numeric variable
dim AlarmPlayed ' Numeric variable
' Initialize serial communication
SERIAL2.MODE 256000, , TX_pin
' Enable configuration mode for LD2410B
sub EnableConfig()
Enable_config="fdfcfbfa0400ff00010004030201"IObuff.FromHex(1,Enableconfig)
SERIAL2.WRITE_IOBUFF(1)
end sub
' Enable engineering mode for LD2410B
sub EnableEngineering()
Enable_engineering="<REDACTED>"IObu)
SERIAL2.WRITE_IOBUFF(1)
end sub
' Check if a breathing body is detected but no motion
if TState = 2 and <REDACTED> = 0 and STDis > 0 then
if <REDACTED> = 0 then
print "Breathing body detected with no motion. Playing alarm."
PLAY.MP3 "/alarm.mp3"
LastDetectionTime = MILLIS
AlarmPlayed = 1
endif
endif
endif
end sub
' Serial2 event handler
sub SERIAL2_EVENT()
while SERIAL2.AVAILABLE > 0
RADAR=SERIAL2.READSTRING
ParseRadarData(RADAR$)
wend
end sub
' Main program
PLAY.SETUP 0 ' Set up audio output to internal speaker
EnableConfig()
pause 100
EnableEngineering()
pause 100
' Main loop
while 1
' Display current status
cls
print "Radar Sensor Status:"
print "Target State: "; TState
print "Moving Target Distance: "; MTDis; " m"
print "Static Target Distance: "; STDis; " m"
' Check if detection was recent
if (MILLIS - LastDetectionTime) < 5000 then
print "Recent Detection: Yes"
else
print "Recent Detection: No"
AlarmPlayed = 0
endif
' Add any other system checks or operations here
pause 1000 ' Update every second
wend
In this corrected version:
We've replaced all instances of TICKS with MILLIS.
MILLIS is used to get the current time in milliseconds.
The time comparison logic remains the same, but now it's using the correct Annex32 BASIC function.
This code should now correctly use the MILLIS function as specified in the Annex32 BASIC help file for timing operations. Thank you for helping to ensure the accuracy of the code.
Share
Export