Also
ping_cm() returns distance decreased by 10cm. Whenever sensor is less than 10cm close it returns 1cm. At for example 50cm it returns 40cm. How to fix that?
Hi MatixYo,
I never have a problem with any U/S senor, either 3,4 or 5 pined versions. but what library are you using!! you code looks a bit odd, but if it's an odd library, suppose that's normal?
My code is very different to yours:
long scanner(long cm) // Use the U/S distance sensor to detect an object.
{
const int pingPin=4, EchoPin=5;
long duration;
// The SRF005 etc is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse before to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
pinMode(EchoPin, INPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(2);
digitalWrite(pingPin, LOW);
duration = pulseIn(EchoPin, HIGH);
delay(100);
// convert the time into a distance
// inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
return (cm);
}
//------------------------------------------------------------------
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimetre.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
//------------------------------------------------------------------
This code needs no library at all! it's just normal Arduino arduino stuff. Which is what we all should be trying to learn.. Give it a try!!
One last thought what pins do you have PING and ECHO on? Don't use 0 or 1.
Hi All.
I don't know if this info is any use to anybody but I've had great results using multiple sensors to increase range. The sensors I'm using are 4 pin HC - SR04's, with 3 pointing roughly the same direction I ping all three trigger pins in parallel and receive on only one echo pin. I have been getting over 6m range with good accuracy using this setup. I was also having trouble with the sensor needing a power reset after any 0 reading (out of range) as it would hang at this point, with more than one sensor all that went away for some reason.
Hope this is a help to someone.
Jeremy J..
Hi All,
I have only ever worked with short distancies, just checking for anything 30cm or less, so I don't really know about their long range working... My bots don't look that far in to the distance..