TFMini LASER Intrusion alarm

Hi,

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)

  • digitalWrite(ledPin, HIGH);*
  • delay(5);*
  • digitalWrite(ledPin, LOW);*
  • delay(5);*
    }
    }
    }
    }
    }
    Thanks all!

What do you meant by slow?

Here:

void fDoLIDAR()
{
  //
  long iLIDAR_Distance = 0;
  long iLIDAR_Strength = 0;
  String sMsgToSend;
  String s_cm = "cm ";
  sMsgToSend.reserve( 16 );
  while (1)
  {
    Kernel.Tm_WakeupAfter( lLIDAR_SequenceInterval );   // Wake up after ... milliseconds
    Kernel.Sm_Claim(SEM_ID_02, uMT_WAIT); // // stop lidar servo
    Kernel.Sm_Claim(SEM_ID_03, uMT_WAIT); // stop x y servo
    if ( digitalReadDirect( MWM_PINin ) )
    {
      digitalWriteDirect( MWM_PINout, HIGH );
      tfmini.externalTrigger();
      iLIDAR_Distance = tfmini.getDistance();
      iLIDAR_Strength = tfmini.getRecentSignalStrength();
      if (iLIDAR_Distance < 600)
      {
        // Serial.println( "TF Mini distance " + String(iLIDAR_Distance) + s_cm + " strength " + String(iLIDAR_Strength ) );
        // Serial.flush();
        sMsgToSend.concat( s_cm + String(iLIDAR_Distance) + " " + String(iLIDAR_Strength ) );
        Kernel.Sm_Claim(SEM_ID_01, uMT_WAIT); // // claim a single SEMAPHORE token
        sMessageToSend = sMsgToSend;
        Kernel.Ev_Send(iEvtID_F, EVENT_F);  // trigger fSendOut_SEMAPHORE + consume a single SEMAPHORE token
        sMsgToSend = "";
      }
    }
    else
    {
      digitalWriteDirect( MWM_PINout, LOW );
    }
    Kernel.Sm_Release( SEM_ID_02 ); // release a single SEMAPHORE token
    Kernel.Sm_Release( SEM_ID_03 );
  }
}

I use single trigger mode to single trigger a TFMini once every 50mS (lLIDAR_SequenceInterval = 50mS).

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.

I am Using OSEPP Uno R3 Plus.

Here is the code I use to test a TFMini:

#include <HardwareSerial.h>
HardwareSerial SerialTFMini( 2 );
// serial(1) = pin27=RX, pin26=TX
// serial(2) = pin16=RX green, pin17=TX white




void getTFminiData(int* distance, int* strength) {
  static char i = 0;
  char j = 0;
  int checksum = 0; 
  static int rx[9];
  if(SerialTFMini.available())
  {  
    // Serial.println( "tfmini serial available" );
    rx[i] = SerialTFMini.read();
    if(rx[0] != 0x59) {
      i = 0;
    } else if(i == 1 && rx[1] != 0x59) {
      i = 0;
    } else if(i == 8) {
      for(j = 0; j < 8; j++) {
        checksum += rx[j];
      }
      if(rx[8] == (checksum % 256)) {
        *distance = rx[2] + rx[3] * 256;
        *strength = rx[4] + rx[5] * 256;
      }
      i = 0;
    } else 
    {
      i++;
    } 
  }  
}


void setup() {
  Serial.begin(115200);
  
SerialTFMini.begin( 115200 );

}

void loop() 
{
  int distance = 0;
  int strength = 0;
  getTFminiData(&distance, &strength);
  while(!distance) {
    getTFminiData(&distance, &strength);
    if(distance) {
      Serial.print(distance);
      Serial.print("cm\t");
      Serial.print("strength: ");
      Serial.println(strength);
    }
  }

delay(100);
}

You'll have to change the serial thingies to match your microController.

What portion of the code I need to modify, i'm a bit lost

chris442:
What portion of the code I need to modify, i'm a bit lost

You'd modify the portion of the code that will not work with an Uno; the references to Serial.

To be honest, I have no clue where I need to modify anything. could you give me another hint. My rx tx are pin 3 and 4.

The last page of the spec sheet

says that the trigger rate is adjustable from 1 to 100 Hz. Perhaps it defaults to 1 Hz. Is that the 'slow' speed you are seeing?

The product manual
http://en.benewake.com/res/wuliu/docs/15487890645199935TF02%20Product%20Manual%20V1.1.pdf
has information about a whole bunch of customisable settings including the trigger rate.