connecting Multiple TF mini lidar to Arduino uno

I am working on a project and I need help in my code, I hope there is someone can help me with the problem I'm facing.
My problem is I have three (3) TF mini lidar sensors plugged to Arduino uno along with a speaker . What I'm trying to do is let each sensor detect a side by itself such as Front,Left,Right and then give a voice feedback about the Direction and Distance of the object using the speaker. For example, If the first sensor let's say ''Front'' detect an abject within 1m I want the speaker to notify them by saying there is an object approaching from the front etc.
I was able to get one sensor to work and detect the objects within 30cm. I have a hard time integrating the other two. Here is my code for handling one sensor, and implementing the audio feedback please help me handle the other two.

const int vibPin = 10;
const int buzzPin = 11;

#include <SoftwareSerial.h>
#include "TFMini.h"
TFMini tfmini;

SoftwareSerial SerialTFMini(2, 3); //The only value that matters here is the first one, 2, Rx // 
// serial(1) = pin12=RX, pin13=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() {  
  
  pinMode(vibPin, OUTPUT); 
  pinMode(buzzPin, OUTPUT);
  
  // Step 1: Initialize hardware serial port (serial debug port)
  Serial.begin(115200);
  //wait for serial port to connect. Needed for native USB port only
  while (!Serial);
     
  Serial.println ("Initializing...");

  // Step 2: Initialize the data rate for the SoftwareSerial port
  SerialTFMini.begin(TFMINI_BAUDRATE);
  
   // Step 3: Initialize the TF Mini sensor
  tfmini.begin(&SerialTFMini);   
}
 
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);
if (distance < 20)
  {
   digitalWrite(buzzPin,HIGH);
    digitalWrite(vibPin,HIGH);
    
  }
  else 
  {
    digitalWrite(buzzPin,LOW);
    digitalWrite(vibPin,LOW);
  }
  delay(300);
}

Would not each TFmini want its own serial port?

You've defined one software serial port, just add in another software serial port?

If I remember correctly you will be able to only define one software serial thingy.. I'm sure someone will pipe in here on that matter. Best solution, get a MCU that has multiple hardware serial ports.

I am working on a project and I need help in my code, I hope there is someone can help me with the problem I'm facing.
My problem is I have three TF mini lidar sensors plugged to Arduino uno and speaker . What I'm trying to do is let each sensor detect a side by itself such as Front,Left,Right and then give a voice feedback about the Direction and Distance of the object using the speaker. For example, If the first sensor let's say ''Front'' detect an abject within 1m I want the speaker to notify them by saying there is an object approaching from the front by 1m. Here is the code im working on , when I run it and open the serial monitor it does't measure any distance . I got no clue why .

#include<SoftwareSerial.h>
#include<TFMini.h>
#include "Talkie.h"
#include "Vocab_US_Large.h"
#include "Vocab_Special.h"
#include "Vocab_US_TI99.h"
#include "TalkieUtils.h"

//Declaring variables and constants
TFMini TFFront;
TFMini TFRight;
TFMini TFLeft;
Talkie voice;                   //PIN 3 is by default the output for the speaker
SoftwareSerial SRight (4,5);    //(RX, TX)
SoftwareSerial SFront (6,7);    //(RX, TX)
SoftwareSerial SLeft (8,9);     //(RX, TX)

int TFBaudRate = 115200;
int USBBaudRate = 9600;
uint16_t threshold_distance = 100;       //Threshold distance in "cm"; speak only when any object is nearer than this distance

void setup() 
{
  //Initializing Baud Rates
  Serial.begin (USBBaudRate);
  while(!Serial);                     //Wait for USB Serial Port to connect
  Serial.println("Starting ......");
  SFront.begin (TFBaudRate);
  delay(50);
  SRight.begin (TFBaudRate);
  delay(50);
  SLeft.begin (TFBaudRate);
  delay(50);

  //Initializing TFMini Sensors
  TFFront.begin(&SFront);
  delay(100);
  TFRight.begin(&SRight);
  delay(100);
  TFLeft.begin(&SLeft);
  delay(100);  
}

void loop() 
{ 
  uint16_t Distances[3]={0,0,0};  //Initializing a blank array to store the distances of each side in each iteration
  Serial.println("All distances are in cm");

  //Getting and storing the distance from each side
  Distances[0] = TFFront.getDistance();
  Serial.print ("Front distance: ");
  Serial.println (Distances[0]);
  delay(50);
  Distances[1] = TFRight.getDistance();
  Serial.print ("Right distance: ");
  Serial.println (Distances[0]);
  delay(50);
  Distances[2] = TFLeft.getDistance();
  Serial.print ("Left distance: ");
  Serial.println (Distances[0]);
  delay(50);

  //Checking if another object is nearer than our set threshold and speaking
  if(Distances[0] < threshold_distance)    //Front Sensor
  {
    int dist_m0 = Distances[0]/100;
    voice.say(spt_SOME);
    voice.say(spt_THING);
    voice.say(spt_IN);
    voice.say(spt_FRONT);
    voice.say(sp2_AT);
    sayQFloat(&voice, dist_m0, 2, true, true);
    voice.say(sp2_METER);
    delay (100);
  }

  if(Distances[1] < threshold_distance)    //Right Sensor
  {
    int dist_m1 = Distances[1]/100;
    voice.say(spt_SOME);
    voice.say(spt_THING);
    voice.say(spt_ON);
    voice.say(spt_RIGHT);
    voice.say(sp2_AT);
    sayQFloat(&voice, dist_m1, 2, true, true);
    voice.say(sp2_METER);
    delay (100);
  }

  if(Distances[2] < threshold_distance)    //Left Sensor
  {
    int dist_m2 = Distances[2]/100;
    voice.say(spt_SOME);
    voice.say(spt_THING);
    voice.say(spt_ON);
    voice.say(spt_LEFT);
    voice.say(sp2_AT);
    sayQFloat(&voice, dist_m2, 2, true, true);
    voice.say(sp2_METER);
    delay (100);
  }  
}
Distances[1] = TFRight.getDistance();
  Serial.print ("Right distance: ");
  Serial.println (Distances[0]);

Here, you are getting the right distance stored in Distances[1] but printing Distances[0] ( Same mistake for printing left distance )...Have you connected the UNO to a computer?

You have declared 9600 as the baud rate. Make sure it is same in the serial monitor as well...

of course I have connected it to my computer and declared it as 9600 bud in the serial monitor .

it should measure the distance when I open the serial monitor. but instead it just say "All distances are in cm"

What was it about this that you didn’t understand?

TheUNOGuy:

Distances[1] = TFRight.getDistance();

Serial.print ("Right distance: ");
 Serial.println (Distances[0]);



Here, you are getting the right distance stored in Distances[1] but printing Distances[0] ( Same mistake for printing left )

Also I don’t think you can run three instances of software serial on a Uno.

And another thing how does each instance of TFMini know which instance of software serial it is supposed to use?

Distances[0] = TFFront.getDistance();
  Serial.print ("Front distance: ");
  Serial.println (Distances[0]);
  delay(50);
  Distances[1] = TFRight.getDistance();
  Serial.print ("Right distance: ");
  Serial.println (Distances[1]);
  delay(50);
  Distances[2] = TFLeft.getDistance();
  Serial.print ("Left distance: ");
  Serial.println (Distances[2]);
  delay(50);

Still didn't measure any distance form the three sensors . In addition, I believe there is a way to make it work.

In addition, I believe there is a way to make it work.

Believe is a powerful thing in many aspects of life, but in electronics it often takes more than that.

If you want to do some debugging then sprinkle print commands about to see where things are going wrong.

I would start with one either side of this

 Distances[0] = TFFront.getDistance();

To see if it enters and emerges from this call.

Also I would not try and make three sensors work before I got one sensor to work. The trick to development is to build things up slowly and test as you go.

Each software serial connection requires two digital input/output pins so there are enough pins on the Uno to do four software serial connections. Just make sure to stop one software serial connection before starting another.

Got it from here...I don't understand why you need so many serial communications established...?

Grumpy_Mike:
Also I would not try and make three sensors work before I got one sensor to work.

In the OP's other post (which should be merged with this one) connecting Multiple TF mini lidar to Arduino uno - Sensors - Arduino Forum it is revealed that the OP did get one sensor to work...

Thanks Dave, I do wish people would stop doing this.

So if you have one working then try two.
Remove the begin for each instance of software serial from the setup, and enable them just before you use them and disable them just after you have used them, like this:-

  TFFront.begin();
  Distances[0] = TFFront.getDistance();
  TFFront.end();
  Serial.print ("Front distance: ");
  Serial.println (Distances[0]);
  delay(50);
  TFRight.begin();
  Distances[1] = TFRight.getDistance();
  TFRight.end();
  Serial.print ("Right distance: ");
  Serial.println (Distances[1]);
  delay(50);

If this works can you guess how you extend it to three?

Duplicate Post ( reported )...Can you please learn how to use the forum?

Threads merged.

OP's gone...Hate when this happens >:(