Connecting two TF mini S LiDAR sensors to the Arduino

I am working on a project and I need help in my code.

My problem is to measure the distance in each direction with two lidar sensors and calculate the speed.

The right sensor doesn't seem to work when I check it on the serial monitor.

disR does not appear on the serial monitor.

So, I swapped the left and right sensors.

The result was the same.

Also, I tried connecting it to another digital pin.

The result was the same.

Here is the code I used.

#include <DFRobot_TFmini.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerialR(8, 9);
SoftwareSerial mySerialL(11, 12);

DFRobot_TFmini TFminiR;
DFRobot_TFmini TFminiL;

uint16_t Dis[4]={0,0,0,0};
uint16_t velR, velL;

void setup()
{
  Serial.begin(115200);
  TFminiR.begin(mySerialR);
  delay(100);
  TFminiL.begin(mySerialL);
  delay(100);
}

void loop()
{
  if(TFminiR.measure())
  {
    Dis[0] = TFminiR.getDistance();
    Serial.print("DisR1 = ");
    Serial.print(Dis[0]);
    Serial.println("m");
    delay(1000);
    Dis[2] = TFminiR.getDistance();
    Serial.print("DisR2 = ");
    Serial.print(Dis[2]);
    Serial.println("m");  
  }      
  velR = ((float)(Dis[0]-Dis[2]));
  Serial.print("VelR = ");
  Serial.print(velR);
  Serial.println("km/h");
  delay(1000);
  if(TFminiL.measure())
  {
    Dis[1] = TFminiL.getDistance();
    Serial.print("DisL1 = ");
    Serial.print(Dis[1]);
    Serial.println("m");
    delay(1000);
    Dis[3] = TFminiL.getDistance();
    Serial.print("DisL2 = ");
    Serial.print(Dis[3]);
    Serial.println("m");
  }
  velL = ((float)(Dis[1]-Dis[3]));
  Serial.print("VelL = ");
  Serial.print(velL);
  Serial.println("km/h");
  delay(1000);
}

I'm attaching a screenshot in case you're curious about the serial monitor I've seen.
화면 캡처 2022-06-02 005228

In addition, I will try to fix the units correctly later.

First get rid of the delay(1000); and use the mills function. For an example see the blink without delay. The reason I say this is while the processor is doing the delay() it cannot do anything else. So each time you do a delay() you stop all processing.

I removed all "delays (1000)".
And, I replaced this place with "time = millis(); Serial.println(time);".
Am I correct in understanding your intentions?
It was typed so quickly that it was hard to follow.
So it's hard to check if the right sensor is working

Not quite. Here is a link that will help: Using millis() for timing. A beginners guide. The basis is using a counter that is cleared at reset and counts up in milliseconds. This is a 32 bit number and is defined in blink without delay as: unsigned long previousMillis = 0; // this will store last time LED was updated. The millis function returns the number of milliseconds that your Arduino board has been powered up. These are 32 bit numbers, if you forget and use an int it will sort of maybe work but funny things will happen. When you start your sketch the clock starts. Millis returns the number of milliseconds that have passed since this upload was completed. There is a lot of stuff written on this and about overflow problems. This will not happen unless your program has been running for 49 days. Spend some time with the link it will be a big help to you.

Only one instance of SoftwareSerial can be listening to a serial input pin at a time, so this code is not likely to work for your project. See the SoftwareSerial library documentation.

You need a Mega with additional hardware serial ports, or some other approach.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.