do i have a programming problem or is it me?

Hi All
im in the middle of a river level monitor project and keep getting a reading of 112 nomatter how far my sensor is from objects, here is the code im using any pointers would be great,

#define trigPin 10
#define echoPin 13

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {
float duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * 0.0344;

if (distance >= 400 || distance <= 2){
Serial.print("Distance = ");
Serial.println("Out of range");
}
else {
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
delay(500);
}

Well, when out of range you never print the distance.

It may not be programming problem. It may also be a wiring error.

thanks peeps im going to sack it off for tonight and start again with the wiring and take it from there :slight_smile:

You never did tell us what sensor you are using.

Maybe.. the river didn't changed its level?

sorry its a HC-SR04 and its still in a test environment not in the river yet want to make sure its working accurately before it goes out int he field.

What happens when you disconnect the sonar? (You might want to consider a timeout here, too.)