4

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 &amp;&amp; rx[1] != 0x59) {
  i = 0;
} else if(i == 8) {
  for(j = 0; j &lt; 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 &quot;cm&quot;; 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);

}

Soul
  • 47
  • 2

1 Answers1

1

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.

chrisl
  • 16,622
  • 2
  • 18
  • 27