HC sr04 and arduino obstacle detection

making a project to let the blind people know of the distance before them…so principle is>>>calculate the distance and let the blind person know of the obstacle how much closer is the object by haptic feedback…i.e…if the object is closer…the haptic feedback should be more strong…and if the obstacle is far away, the feedback shud be of less magnitude…

P.S: Haptic feedback—touch by vibration motor in different patterns to the user…

if the obstacle is in the range
calculated distance through hc sr04 (with sonar principle). and arduino…

our code for arduino:
Logic:Making the motor vibrate more when obstacle is closer…
The following program :some doubts in it:

#define trigPin 12
#define echoPin 11
#define motorPin 3

void setup() {
  Serial.begin (9600);

void loop() {
  int duration, distance;
  digitalWrite(trigPin, HIGH);
   delayMicroseconds(1000);        //WHAT DOES IT DO..waits for   //1ms..rite? but when we trigger the sensor hc sr04 we first             //give a low signal of 2uS and then 10uS High pulse and then a low //signal .will this logic also gonna work..as its not working //sometimes with my program..

  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance <400 && distance >300)
    Serial.print("Out of range");// here in the serial monitor..it prints the //distance of 6/14 cm repeatedly even if the obstacle is out of range.and //sometimes displays out of range which is the desired result
    Serial.println("  ");
   else if (distance <=300 && distance >250)
   else if (distance <=250 && distance >200)
   else if (distance <=200 && distance >150)
   else if (distance <=150 && distance >100)
   else if (distance <=100 && distance >80)
   else if (distance <=80 && distance >10)
  Serial.println(" cm");
   delay(500);   //explain me this line of the code..

Problem:Now ,In the serial monitor we are observing the analog distance values and comparing it with the vibration whether it’s strong or less…
But, after a certain range…like 250 cm,even when’s there no obstacle, we are getting random output(distance in the serial monitor) of 6/8/12/14/16/18/1140 cm…sometimes.
donno…why it is happening…can u tweak the code…and suggest me with the needful…please.project submission within a week

Moderator edit: [code][/code] tags added. (Nick Gammon)

 delay(500); //explain me this line of the code..

It stops the processor doing anything except respond to interrupts for 500 milliseconds. I'd guess it is there to allow echoes from the previous pulse to die away, but limits your update rate to less than 2Hz, which for fast blind walker might be painful.

I saw on a project that they took 5 readings and averaged them before using the output. Maybe the sensor throws up occasional noisy readings??

PS I'm very new to this so don't waste time on my answer if there are others :)

Awol..explain me the first doubt regarding the out of range problem..in the program..if u can..Thanx for ur time btw

digitalWrite(trigPin, HIGH);

I'm not sure what you're saying, but assume that the sonar unit starts transmitting on the rising edge of "trigPin". Your sonar is effectively blind for one millisecond, or about 30 cm (15 round-trip). How long does the manual say that "trigPin" should be held high?

Is there a library for running the vibration motor or wat?