redwuzz
September 16, 2016, 7:04pm
1
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);
}
KeithRB
September 16, 2016, 7:23pm
2
Well, when out of range you never print the distance.
6v6gt
September 16, 2016, 7:28pm
3
It may not be programming problem. It may also be a wiring error.
redwuzz
September 16, 2016, 7:36pm
4
thanks peeps im going to sack it off for tonight and start again with the wiring and take it from there
KeithRB
September 16, 2016, 7:37pm
5
You never did tell us what sensor you are using.
Maybe.. the river didn't changed its level?
redwuzz
September 16, 2016, 8:07pm
7
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.
KeithRB
September 16, 2016, 8:16pm
8
What happens when you disconnect the sonar? (You might want to consider a timeout here, too.)