Hi,
I am currently trying to get my ultrasonic sensor to work using the NewPIng Library.
The sensor is working fine as I can use a simple sketch to get distance measurement. This works ok but for my application I need more accurate and reliable distance measurements.
My problem arises when I use the NewPing library. It is installed correctly.
I am using a sketch which I copied from the DroneBotWorkshop but I get ‘Out of Range’ whatever I do. I included a couple of extra lines to see if I was getting anything from the sensor which I am not.
Here’s the code:
// This uses Serial Monitor to display Range Finder distance readings
// Include NewPing Library
#include “NewPing.h”
// Hook up HC-SR04 with Trig to Arduino Pin 10, Echo to Arduino pin 13
// Maximum Distance is 400 cm
#define trigger_pin 10
#define echo_pin 13
#define max_distance 400
NewPing sonar(trigger_pin, echo_pin, max_distance);
float duration, distance;
void setup() {
Serial.begin (9600);
}
void loop() {
delay(50);
duration = sonar.ping();
// Determine distance from duration
// Use 343 metres per second as speed of sound
distance = (duration / 2) * 0.0343;
// Send results to Serial Monitor
Serial.print(“Duration = “); //My extra lines to see if I was getting any data from the sensor
Serial.print(duration); //
Serial.print(” Distance = “);
if (distance >= 400 || distance <= 2) {
Serial.println(“Out of range”);
}
else {
Serial.print(distance);
Serial.println(” cm”);
delay(500);
}
delay(500);
}
As I said, I get “Out of Range” and 0.00 for duration.
I have looked all over the www for a solution…Please Help Me!!