HC sr04 ultrasonic distance ranger module programming

Hey I found out that my sensor program is printing all the analog values in the serial monitor that it is detecting…i.e. when detecting an obstacle.
But now i want to limit my sensor range of detecting obstacles…and say that…
if the distance is >=300 cm, then it should print 300 irrespective of the distance above this range…
Can u help me how to do that…

Should i define the analog value got from the echo output of the ultrasonic hc sr04 sensor as constant…in a range >=300…
This is my logic…How else can u do the above?Please suggest…
HC-sr04 sensor image is been attached here

HC-SR04.pdf (526 KB)

include "Ultrasonic.h"

include "Arduino.h"

Ultrasonic ultrasonic( 3, 4 );

int cm;

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

void loop() {

int timing; cm=ultrasonic.Ranging(CM);

if (cm>=300) Serial.println("over 300cm"); else { Serial.print("distance: "); Serial.print(cm); Serial.println("cm"); }

delay(500); }

i think this would be helpful: http://dduino.blogspot.com/2011/10/ultrasonic-thermin.html

unzip into the libraries folder and compile the code i posted…

Ultrasonic.zip (1.8 KB)

to which pins did u connect the echo and trigger pins.@cjdelphi…i hv dis program of mine…logic>>
motor vibrates more when obstacle near…and vice versa…
program is as follows

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

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

void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);

digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <400 && distance >300)
Serial.print(“Out of range”);
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");


@sirblow..link is not opening..either its broken or something..

my link works just fine for me.

look at what you have:

#define trigPin 12
#define echoPin 11

trigger on pin 12, echo on 11. it doesnt matter what pin; any digital pin will do.

you could use map() instead of all the if statements: http://arduino.cc/en/Reference/map