2

I have three TFMini sensors I need all of them to detect. I only got one to work , pins 4,5 . My info in coding is basic . I really appreciate your help . Thanks in advance.

I think the problem is with Library I can't add TFMini_1.

Here is my code,

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



//Declaring variables and constants
TFMini TFFront;
TFMini TFRight;
TFMini TFLeft;
SoftwareSerial SerialTFMini (4,5);    //(RX, TX)
SoftwareSerial SerialTFMini_1 (6,7);    //(RX, TX)
SoftwareSerial SerialTFMini_2(8,9);     //(RX, TX)


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++;
    } 
  }  
}

       //Threshold distance in "cm"; speak only when any object is nearer than this distance

void setup() 
{

  //Initializing Baud Rates
  Serial.begin (115200);
  while(!Serial);                     //Wait for USB Serial Port to connect
  Serial.println("Initializing...");
  SerialTFMini.begin (TFMINI_BAUDRATE);
  
  
  

  //Initializing TFMini Sensors
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);

}
0

First: Why are you including, declaring and initializing the library's tfmini object, if you then don't use it for reading the sensor?

Your problem will be, that you can only listen to exactly 1 SoftwareSerial interface at a time. If you listen to the first interface and the sensor on the second interface is sending something, you will miss that data.

I suggest, you switch the Uno for a board with more hardware Serial interfaces (which work via hardware, thus can work in parallel). The Arduino Mega for example has a total of 4 hardware Serial interfaces.

Though you might get away with the Uno when working with the external trigger mode. The TFMini library has an example named SingleReading. It sets the SingleScanMode in setup(). Then it issues an external trigger and then reads the sensor. I suggest trying the following: In your code you initiate all 3 TFMini objects with their respective SoftwareSerial object. Then in loop() you go through these 3 items, first calling listen() on the corresponding SoftwareSerial object. Then you issue the external trigger on the corresponding TFMini object and read the data from it. Then you go to the next SoftwareSerial interface and call listen() on it, and so on. The idea is, that with an external trigger you can control, when you will get a valid data frame. I'm not sure, if the sensor will also send some data between the triggers, but that data should just be discarded without any problem.

Note: I cannot test this idea, because I don't have the corresponding hardware. You will have to test that yourself.

4
  • I did try to use the Mega but I didn't even get one sensor to work . – Soul Nov 16 '20 at 6:15
  • @Soul Then you must have done something else wrong. You didn't show your code for the Mega, so I cannot say, what is the problem there. But of course you can try the external trigger mode with the Uno, as described above – chrisl Nov 16 '20 at 8:04
  • I basically used the some code . – Soul Nov 18 '20 at 3:21
  • @Soul Then that is your problem. You cannot listen to multiple SoftwareSerial at a time, as described above. You need to change the code for the Mega, so that you use the Hardware Serial interfaces Serial1 to Serial4 and connect the TFMinis to the respective pins – chrisl Nov 18 '20 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.