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() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(10);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
cm = pulseIn(echoPin, HIGH)/29/2;
Serial.print(cm);
Serial.println("cm");
delay(500);
}
Do you have any idea how I can solve the issue?
-Moritz