Hi all,
I'm quite new to programming and using various sensors with Arduino.
As part of a bigger project, I need precise distance measurement, now I'm experimenting with a HC-SR04 ultrasonic sensor. At this point I only need a reliable distance measurement, all the other things will be put together later.
The good thing is that gives nice linear results for the range I've tested for (10-80 cm). However, when it comes to converting the raw measurement results (echo times) to distance, the code I've written gives some totally unexpected numbers, not even close to the real value.
What's even stranger is that using the value of the variable "echoTime" in some lines of code seems to alter the value stored in "echoTime", while it isn't given a new value at all.
So here is my very basic test code for the sensor:
int triggerPin=7;
int echoPin=6;
int distance;
int echoTime;
void setup() {
// put your setup code here, to run once:
pinMode(triggerPin, OUTPUT); // Trigger lab kimenet
pinMode(echoPin, INPUT); // Echo lab bemenet
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(triggerPin,HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin,LOW);
echoTime=pulseIn(echoPin,HIGH);
distance=echoTime*(344/20000)-(0,1801*(echoTime*(344/20000))-0,2015);
Serial.println(echoTime);
delay(500);
}
All it does is reading out the value from the sensor and sending it to the serial port. The formula for the distance is based on some corrections, which I figured out by putting the echotime values in an Excel table, but it doesn't matter at the moment. The distance value is used for nothing in this program intentionally, because my problem is, that if I exclude that line of code with a "// " the echotime will print different numbers.
How is that possible when I don't change its value at all? If I try with another formula, let's say "distance = echoTime+5; " than it gives different results for echotime again. (Greater than it should be.) If I just simply insert the value of echotime into the variable "distance" (distance = echoTime;) than I get smaller numbers for the echotime.
This is something very strange. How could the value of a variable change if no line of code alters is?
I just use it to calculate another variable, but it changes itself...
Any idea would be very much appreciated!
(Arduino Due & HC-SR04 ultrasonic sensor)

