I bought these sensors for their accuracy. I was wondering how can I make these able to be an intrusion alarm. I found the code online that I am able to modify as I need but the reaction time is too slow. What can I add or modify in order to have this sensor operate quicker?
Here is the code I have:
/* This program is a parsing routine of TF02 product standard output protocol on Arduino.
The format of data package is 0x59 0x59 Dist_L Dist_H Strength_L
Strength_H Sequence_L Sequence_H
CheckSum_L
Refer to the product specification for detailed description.
For Arduino board with one serial port, use software to virtualize serial port’s functions: such as UNO board. /
#include<SoftwareSerial.h>// soft serial port header file
SoftwareSerial Serial1(2,3); // define the soft serial port as Serial1, pin2 as RX, and pin3 as TX
/For Arduino board with multiple serial ports such as DUE board, comment out the above two codes, and directly use
Serial1 port/
int dist;// LiDAR actually measured distance value
int strength;// LiDAR signal strength
int check;// check numerical value storage
int i;
int uart[9];// store data measured by LiDAR
int ledPin = 13;
const int HEADER=0x59;// data package frame header
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(600);//set the Baud rate of Arduino and computer serial port
Serial1.begin(115200);//set the Baud rate of LiDAR and Arduino serial port
}
void loop()
{
if (Serial1.available())//check whether the serial port has data input
{
if(Serial1.read()==HEADER)// determine data package frame header 0x59
{
uart[0]=HEADER;
if(Serial1.read()==HEADER)//determine data package frame header 0x59
{
uart[1]=HEADER;
for(i=2;i<9;i++)// store data to array
{
uart=Serial1.read();* } check=uart[0]+uart[1]+uart[2]+uart[3]+uart[4]+uart[5]+uart[6]+uart[7]; if(uart[8]==(check&0xff))// check the received data as per protocols { dist=uart[2]+uart[3]*256;// calculate distance value strength=uart[4]+uart[5]*256;// calculate signal strength value Serial.print("dist = ");
*Serial.print(dist);// output LiDAR tests distance value * Serial.print('\t'); Serial.print("strength = "); Serial.print(strength);// output signal strength value Serial.print('\n'); if (dist <=100)
You give no information about the used board!
From my experience: All 16 MHz boards (UNO, Nano, ...) cannot reliable work with 115200 bps and SoftwareSerial! The best choice is a Board with a real Serial1 pair.
I have spend some days with a Nano - its impossible.