Issues with NewPing HC-SR04.


hello,
i've noticed ping() returns constant, unreal values unless put ping() somewhere before that.
so this
code: [select]

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


returns
quote
47
51
47
but
code: [select]
void loop() {
  delay(30);
  sonar.ping();
  serial.println(sonar.ping());
}

returns real values.

why so?

also
  ping_cm() returns distance decreased 10cm. whenever sensor less 10cm close returns 1cm. @ example 50cm returns 40cm. how fix that?

hi matixyo,
                  never have problem u/s senor, either 3,4 or 5 pined versions. library using!! code looks bit odd, if it's odd library, suppose that's normal?

my code different yours:

code: [select]
long scanner(long cm)         // use u/s distance sensor detect object.
{
const int pingpin=4, echopin=5;
long duration;

// srf005 etc triggered high pulse of 2 or more microseconds.
// give short low pulse before ensure 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 time distance
// inches = microsecondstoinches(duration);
cm = microsecondstocentimeters(duration);
return (cm);
}
//------------------------------------------------------------------
long microsecondstocentimeters(long microseconds)
{
// speed of sound 340 m/s or 29 microseconds per centimetre.
// ping travels out , back, find distance of the
// object take half of distance travelled.
return microseconds / 29 / 2;
}
//------------------------------------------------------------------


this code needs no library @ all! it's normal arduino arduino stuff. should trying learn.. give try!! 

one last thought pins have ping , echo on?  don't use 0 or 1.

is bot? see bots @ :www.melsaunders.co.uk

hope helps.
regards

mel.


Arduino Forum > Using Arduino > Sensors > Issues with NewPing HC-SR04.


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code