Edwooong:
Good day ! i'm currently making a project (distance measuring device) that can measure distance max range of 500cm by using HC-SR04.
Now what ive noticed measuring distance from 1 to 200 cm is accurate but when it reaches 300,400,500cm it will now have an error of 5-10cm. Im new to arduino programming and i think one problem is my code . thanks in advance.This is the code im using:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int trigPin = 9 ;
const int echoPin = 10;
long duration;
int distanceCm, distanceInch;void setup() {
Serial.begin(9600);
lcd.begin(16,2);
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
}
void loop() {
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
duration = pulseIn(echoPin,HIGH);
distanceCm = duration*0.034/2;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Measuring.....");
lcd.setCursor(0,1);
lcd.print("Distance:");
lcd.print(distanceCm);
lcd.print(" Cm ");
}
Quite simple, the distance is wrong because you're not using the correct speed of sound for your temperature. Also, you're not even using the NewPing library so your entire question is out of scope. Use the NewPing library in your sketch and use the built-in speed of sound calculations and then if it's wrong I can give guidance.
Tim