Measure with TF Mini plus

Hello Arduino community,

I´m a beginner in this, so please understand if I make mistakes.

I been fighting with the cheap infrared sensors and I change them for this TF mini plus, it looks like they work good.

In my code it measures the distance, then I try to only print the distance if its under certain number, but for some reason it print the distance above or under the certain number.

Can you help me please?

thank you

TFsensorPrueba.ino (706 Bytes)

Please read the posting guidelines at the top of the forum to post your code like this

#include <DFRobot_TFmini.h>

SoftwareSerial mySerial(12, 13); // RX, TX

DFRobot_TFmini  TFmini;
uint16_t distance, strength;
int count = 0;

void setup() {
  Serial.begin(9600);
  TFmini.begin(mySerial);
}

void loop() {
  if (TFmini.measure()) {                    //Measure Distance and get signal strength
    distance = TFmini.getDistance();       //Get distance data
    strength = TFmini.getStrength();       //Get signal strength data

  }
  if (distance < 50); {
    Serial.print("Distance = ");
    Serial.print(distance);
    Serial.println("cm");
    //Serial.print("Strength = ");
    //Serial.println(strength);
    //delay(100);
  }

}

It helps people help you and then you can see this line

  if (distance < 50); {

and note that the semicolon completes the if() statement so everything after it is NOT part of your if() statement

#include <DFRobot_TFmini.h>

SoftwareSerial mySerial(12, 13); // RX, TX

DFRobot_TFmini  TFmini;
uint16_t distance,strength;
int count=0;

void setup(){
    Serial.begin(9600);
    TFmini.begin(mySerial);
}

void loop(){
    if(TFmini.measure()){                      //Measure Distance and get signal strength
        distance = TFmini.getDistance();       //Get distance data
        strength = TFmini.getStrength();       //Get signal strength data
     
    }
       if(distance<50);{
        Serial.print("Distance = ");
        Serial.print(distance);
        Serial.println("cm");
        //Serial.print("Strength = ");
        //Serial.println(strength);
        //delay(100);
        }
        
}

Yes, I been waiting the 5 mins for the beginners timeline, thank you so much

Hi,

 if (distance < 50); {

Should be;

 if (distance < 50) {

Tom… :slight_smile:

Thank you Tom! I appreciate your help! I´m new on this and I like it!