Hello,
I am using the Ultrasonic sensor HC-SR04 to collect some distance data. Mostly, it worked very much. But sometimes, I get some very abnormal data, which has confused me for a long time. The following data were what I got. It is clear that 55 and 5.534 are abnormal data. This is a big problem. Can someone help me to solve this problem? For your reference, I attached my arduino code. Thanks.
6
55
5.38
5.36
5.41
5.42
5.42
5.36
5.36
5.36
5.38
5.36
5.36
5.38
5.36
5.42
5.42
5.36
5.38
5.36
5.36
5.38
5.36
5.5.43
5.41
5.38
5.38
5.38
5.42
5.36
5.36
5.47
5.36
Arduino code
int trigPin = 13;
int echoPin = 11;
float pingTime;
float targetDistance;
float speedOfSound = 776.5;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigPin, LOW); //set trigger pin low
delayMicroseconds(2000); //Let signal settle
digitalWrite(trigPin, HIGH); //Delay in high state
delayMicroseconds(15); //Ping has now been sent
digitalWrite(trigPin, LOW);
delayMicroseconds(10); //Delay in low state
pingTime = pulseIn(echoPin, HIGH);
pingTime = pingTime/1000000;
pingTime = pingTime/3600;
targetDistance = speedOfSound * pingTime;
targetDistance = targetDistance / 2;
targetDistance = targetDistance * 63360;
Serial.println(targetDistance);
}
Thank you very much.