I ve got two HC-SR04 ultrasonic sensor. I am trying make a radar with that but before, I ve faced with a problem. When there is an stable object(simply a wall) sensor's output is changing. Actually it gives values more than its range. Its range is 400cm but sensor returns 2995 even thought there is no movement.This code is jsut for 1 sensor. I am gonna use to which are mounted on a servo motor and perpendicular to each other.
Things I have tried are supplying external voltage. Changing sensors, unmount from top of servo, trying across to different surface. What else can u suggest?
My Code is
#define ECHOPIN_A 40
#define TRIGPIN_A 10
long duration, duration2;
void setup() {
Serial.begin(9600);
pinMode(TRIGPIN_A, OUTPUT); // Sets the trigPin as an Output
pinMode(ECHOPIN_A, INPUT); // Sets the echoPin as an Input
}
void loop() {
float distance1 = calculateDistance(TRIGPIN_A,ECHOPIN_A);
delay(50);
Serial.print(distance1);
Serial.print(" ");
}
float calculateDistance(int trig, int echo) {
float distance;
digitalWrite(trig, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
distance = duration * 0.034 / 2;
return distance;
}