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.
In addition, I will try to fix the units correctly later.