Hc-sr04 malfuntion

Hi,

For my project I was using a sonar hc-sr04 for first few days it worked great then suddenly in between the distance it returned 0 but it was merged with the distance but slowly it started only to return distance 0

Code

/** Ultrasonic Sensor HC-SR04 and Arduino Tutorial** Crated by Dejan Nedelkovski,* www.HowToMechatronics.com**/// defines pins numbersconst int trigPin = 9;const int echoPin = 10;// defines variableslong duration;int distance;void setup() {pinMode(trigPin, OUTPUT); // Sets the trigPin as an OutputpinMode(echoPin, INPUT); // Sets the echoPin as an InputSerial.begin(9600); // Starts the serial communication}void loop() {// Clears the trigPindigitalWrite(trigPin, LOW);delayMicroseconds(2);// Sets the trigPin on HIGH state for 10 micro secondsdigitalWrite(trigPin, HIGH);delayMicroseconds(10);digitalWrite(trigPin, LOW);// Reads the echoPin, returns the sound wave travel time in microsecondsduration = pulseIn(echoPin, HIGH);// Calculating the distancedistance= duration*0.034/2;// Prints the distance on the Serial MonitorSerial.print("Distance: ");Serial.println(distance);}

That is the wrong way to post code.

If you want help, you had better learn the correct which is explained in a sticky post at the top.