How to negelct false readings using an (hc-sr04) ultrasonic sensor

I'm using the sensor to return distance to me and here's the code I'm using:

const int trigPin = 9;
const int echoPin = 10;
const uint8_t vibrate_pin = 6; 

void setup() { 
  
Serial.begin(9600); 
pinMode(trigPin, OUTPUT); 
pinMode(echoPin, INPUT); 
}

void loop() { 
  
long duration, distance;
digitalWrite(trigPin, LOW);
delay(5);
//delayMicroseconds(20);
digitalWrite(trigPin, HIGH);
delay(10);
//delayMicroseconds(100);
digitalWrite(trigPin, LOW); 
duration = pulseIn(echoPin, HIGH); 
distance= (duration*0.034)/2; 

if(distance <= 75 && distance >= 51){
  analogWrite(vibrate_pin, 100);
}else if(distance <= 50 && distance >= 26){
  analogWrite(vibrate_pin, 200);
}else if(distance <= 25 && distance >= 1){
  analogWrite(vibrate_pin, 960);
}else{
  analogWrite(vibrate_pin, 0);
}

//Serial.print("Distance: ");
Serial.println(distance);

}

This works fine for what I'm trying to do, however, every once in a while I have a +3000 cm value. This is a screenshot of it:

I'm wondering how can I change my code to ignore any value that's above some specific value X. I would like it to be totally ignored, not even printed in the monitor even. I'm a programming noob so sorry if this is obvious.

do you mean something like this ?

const int trigPin = 9;
const int echoPin = 10;
const uint8_t vibrate_pin = 6; 

long duration, distance, distance_corrected;

void setup() { 
Serial.begin(9600); 
pinMode(trigPin, OUTPUT); 
pinMode(echoPin, INPUT); 
}

void loop() { 
digitalWrite(trigPin, LOW);
delay(5);
//delayMicroseconds(20);
digitalWrite(trigPin, HIGH);
delay(10);
//delayMicroseconds(100);
digitalWrite(trigPin, LOW); 
duration = pulseIn(echoPin, HIGH); 
distance= (duration*0.034)/2; 

if(distance <= 75 && distance >= 51){
  analogWrite(vibrate_pin, 100);
}else if(distance <= 50 && distance >= 26){
  analogWrite(vibrate_pin, 200);
}else if(distance <= 25 && distance >= 1){
  analogWrite(vibrate_pin, 960);
}else{
  analogWrite(vibrate_pin, 0);
}


if (distance < 3000 ) {  //change the value to whatever you want
//Serial.print("Distance: ");
Serial.println(distance);
distance_corrected = distance; 
}

}

lenny227:
do you mean something like this ?

const int trigPin = 9;

const int echoPin = 10;
const uint8_t vibrate_pin = 6;

long duration, distance, distance_corrected;

void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {
digitalWrite(trigPin, LOW);
delay(5);
//delayMicroseconds(20);
digitalWrite(trigPin, HIGH);
delay(10);
//delayMicroseconds(100);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= (duration*0.034)/2;

if(distance <= 75 && distance >= 51){
  analogWrite(vibrate_pin, 100);
}else if(distance <= 50 && distance >= 26){
  analogWrite(vibrate_pin, 200);
}else if(distance <= 25 && distance >= 1){
  analogWrite(vibrate_pin, 960);
}else{
  analogWrite(vibrate_pin, 0);
}

if (distance < 3000 ) {  //change the value to whatever you want
//Serial.print("Distance: ");
Serial.println(distance);
distance_corrected = distance;
}

}

Yes, this is what I needed. Thank you!

Alternative (and it makes your code faster):

duration = pulseIn(echoPin, HIGH, 6000);

6000 µs is about 2 meters of travel at 330 m/s, so a distance of 1 meter. Your code checks for up to 75 cm so that's enough.

If the timeout is reached, the function returns 0. Minimum for the sensor is about 2 cm, anything less is probably a false reading.

use median filter. check my library - GitHub - enjoyneering/HCSR04: Arduino library for HC-SR04, HC-SRF05, DYP-ME007, BLJ-ME007Y, JSN-SR04T ultrasonic ranging sensor

Or the NewPing library's median method: Arduino Playground - NewPing Library NewPing has been around for a long time.

Not sure why one library is better than another for this simple device, or why a library is needed at all, really.

The "NewPing" is very cool library, but complicated & heavy. My does pretty much the same, but has a smaller foot print.

enjoyneering:
use median filter. check my library - GitHub - enjoyneering/HCSR04: Arduino library for HC-SR04, HC-SRF05, DYP-ME007, BLJ-ME007Y, JSN-SR04T ultrasonic ranging sensor

I use your library now. Its amazing! Super clean coding.

enjoyneering:
use median filter. check my library - GitHub - enjoyneering/HCSR04: Arduino library for HC-SR04, HC-SRF05, DYP-ME007, BLJ-ME007Y, JSN-SR04T ultrasonic ranging sensor

Is the library available in the online web editor?