Issues with NewPing HC-SR04.

Hello,
I've noticed that ping() returns almost constant, unreal values unless you put another ping() somewhere before that.
So this

void loop() {
  delay(30);
  Serial.print(sonar.ping());
}

Returns

47
51
47

But

void loop() {
  delay(30);
  sonar.ping();
  Serial.println(sonar.ping());
}

Returns real values.

Why is it so?

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.

Is this a bot? see my bots at :www.melsaunders.co.uk

Hope it helps.
Regards

Mel.

Are you calling NewPing an odd library? :grinning:
At least it won't freeze Arduino when distance is over 5m.

Anyway my question is still unsolved. I have connected it to pin other than 0 or 1. How to make it work as it properly?

Hi,
If it's a standard HC-S04/5 etc. You don't need a library at all!! Have a look in the library code. it might be set to work on certain pins only.

Doing it yourself is the only way to learn, work my code into yours? and forget the library.

Regards

Mel.

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..

Regards

Mel.