Backround Timer

Hi guys,
I'm working on a project with meassuring distances with the HC-SR04.
I have the problem, that when the distance is too far then it gets stuck on always sending 0, even if I put something back in it's range. The standard timeout of the sensor is 1 sec. Now I want to crate a timeout that is smaller, that scips the pulseIn part if it deoesn't receive a signal in 900ms so it doesnt get suck on receiving nothing. Here's the code i'm using:

const int trigPin = 5;
const int echoPin = 6;
long cm;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

void loop()

  digitalWrite(trigPin, LOW);
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);

  cm = pulseIn(echoPin, HIGH)/29/2;



Do you have any idea how I can solve the issue?

Do you have any idea how I can solve the issue?

You could check out the documentation in the reference for 'pulseIn()' :-

pulseIn(pin, value, timeout);

Edit: The online reference appears to be out-of-action just at the moment, but check out your local copy in >Help >Reference >pulseIn()

Alternatively, you could take a look at the "NewPing" library. (I highly recommend it.)
It has a "MAX_DISTANCE" parameter, same as a timeout, plus other features.

Edit: I'm not sure if this will cure your problem - there have been a lot of faulty HC-SR04 modules lately.
(Fingers crossed that the timeout fixes it, and that your HC-SR04 is a good one.)

Thanks for the answer! I can't make the timeout work but i'll try.