please help me about ultrasonic sensor

Hello everybody.I am student of class seven of Rajshahi Unversity School,Bangladesh.I am very new in Arduino world.

I am facing a problem with ultrasonic sensor HC-SR04 .Here is my code…

int trigger=7; 
int echo=6; 
long time=0; 
long dist=0; 
void setup()
Serial.begin (9600);
pinMode(trigger, OUTPUT); 
pinMode(echo, INPUT); 
void loop()
digitalWrite(trigger, LOW); 

digitalWrite(trigger, HIGH); 
digitalWrite(trigger, LOW); 

time = pulseIn(echo, HIGH); 

dist = (time/2) / 29.1; 
if (dist >= 10000 || dist <= 0) 

Serial.println("No measurement"); 

in serial monitor ,always give me distance between 3300cm-3400cm.but the real distance is 200cm.please help me .I need your help…

Here is a code example that works:

const int triggerPin = 7;
const int echoPin = 8;

long duration;
int cm, mm;

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

void loop() {
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse
  digitalWrite(triggerPin, LOW);
  digitalWrite(triggerPin, HIGH);
  digitalWrite(triggerPin, HIGH);
  // Read the signal from the sensor : a HIGH pulse whose
  // duration is the time (in microseconds) from the sending of
  // the ping to the reception of its echo off an object
  duration = pulseIn(echoPin, HIGH);
  // Convert the time into a distance
  cm = (duration / 2.0) / 29.1;
  mm = (duration / 2.0) / 2.91;



The timings in the pulse are a factor of 1000 too high. The function delay() is a millisecond delay. The functioning examples use a microsecond delay.

thanks a lot, I was suffering from this problem .Your suggestion help me to detect my problem.thanks again......