r/arduino•
MuTeRiiX

Ultrasonic sensor NEWPING library

Hello,

i want to build a car, with a ultrasonic sensor for the distance. If the distance to a wall is too low, then the car should just drive backwards for a couple of centimeters.

I use the ultrasonic sensor with the NewPing Library. And my Problem is, that the sensor shows sometimes a wrong number in cm. Like 50 50 50 1 50 50 50 50.

Is there an option to take the average of the numbers in the library?

I am a beginner, so pls dont hate me if the wrong is not optimized or something else :D

Serial.print("Print: ");
Serial.print(sonar.ping_cm());
Serial.println("cm");


if (sonar.ping_cm() < 10){     // I come in this block because the wrong Distance 
  while (sonar.ping_cm() < 50) { 
  digitalWrite(en1A, LOW);
  digitalWrite(en2A, HIGH);
  digitalWrite(en1B, LOW);
  digitalWrite(en2B, HIGH);

}}

Posted Dec 1, 2019, 1:30 PM UTC

00
Loading comments…