Tfmini-s with Arduino mega2560

hello everyone, please i need your help with this project I’m working. i connected 4 tfmini to logic level converters and to MEGA. it doesn’t show the distance or strength, but when I connect 1 tfmini it works perfectly fine. If I connect more than 1 it doesn’t work. please help me review the code below.

#include <DFRobot_TFmini.h>

SoftwareSerial TF1Serial(10, 11);    //TF mini TXpin = 10 and TFmini RX pin = 11
SoftwareSerial TF2Serial(12, 13);    //TF mini TXpin = 12 and TFmini RX pin = 13
SoftwareSerial TF3Serial(50, 51);    //TF mini TXpin = 50 and TFmini RX pin = 51
SoftwareSerial TF4Serial(52, 53);    //TF mini TXpin = 52 and TFmini RX pin = 53

DFRobot_TFmini tf1mini;
DFRobot_TFmini tf2mini;
DFRobot_TFmini tf3mini;
DFRobot_TFmini tf4mini;

uint16_t dist1;
uint16_t dist2;
uint16_t dist3;
uint16_t dist4;

int limit = 2000;
const int buzzer = 4;

void setup() {
  Serial.begin(115200);
  tf1mini.begin(TF1Serial);
  tf2mini.begin(TF2Serial);
  tf3mini.begin(TF3Serial);
  tf4mini.begin(TF4Serial);
  pinMode(buzzer, OUTPUT);
}

void loop() {
  if (tf1mini.measure()) { //Measure Distance and get signal strength
    dist1 = tf1mini.getDistance(); //Get distance data
    delay(25);
  }

  if (tf2mini.measure()) { //Measure Distance and get signal strength
    dist2 = tf2mini.getDistance(); //Get distance data
    delay(25);
  }

  if (tf3mini.measure()) { //Measure Distance and get signal strength
    dist3 = tf3mini.getDistance(); //Get distance data
    delay(25);
  }

  if (tf4mini.measure()) { //Measure Distance and get signal strength
    dist4 = tf4mini.getDistance(); //Get distance data
    delay(25);
  }

  Serial.print("Distance1 = ");
  Serial.print(dist1);
  Serial.print("mm    ");
  Serial.print("Distance2 = ");
  Serial.print(dist2);
  Serial.print("mm    ");
  Serial.print("Distance3 = ");
  Serial.print(dist3);
  Serial.print("mm    ");
  Serial.print("Distance4 = ");
  Serial.print(dist4);
  Serial.print("mm    ");
  if (dist1 < 100 || dist2 < 100 || dist3 < 100 || dist4 < 100) {
    digitalWrite(buzzer, HIGH);
  }
  else {
    digitalWrite(buzzer, LOW);
  }
}

Software serial doesn't let you run multiple instances at the same time - only one can be listening. Since you have a Mega though, why not use the hardware serial ports instead.

wildbill:
Software serial doesn't let you run multiple instances at the same time - only one can be listening. Since you have a Mega though, why not use the hardware serial ports instead.

could you please send me the code

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