Alright, I just got in the HC-SR04. And, as I feared, I'm not getting enough range. I used the example you package with your NewPing library ("NewPingExample") with MAX_DISTANCE changed to 5000 and an if statement so that it'd only display the range if it was greater than the range that was displayed before (so I didn't just have tons of data constantly shooting across the serial monitor). The max range I got (and only if I stood still in front of the sensor so that it could measure the range several times) was about 150cm. I need the sensor to go off with a person walking toward it at normal (or slightly slower than normal) walking speed at about twice that range.
So should I try a different sensor? Do I need an amplifier?
First, you can't set MAX_DISTANCE to 5000cm. The max you ever need to set that to is 500cm as that's the maximum distance the sensor is able to read. Setting it to 5000 will give you a much longer delay and possible problems if you're trying to ping frequently. You should set MAX_DISTANCE to either 500 or the maximum distance you care about, but never more than 500.
I also believe you're making things too complicated for testing. Use this sketch:
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
NewPing sonar(TRIGGER_PIN, ECHO_PIN); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
delay(35); // Wait 35ms between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
if (uS != NO_ECHO) {
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
Serial.println("cm");
}
}
Don't use pin 13 or a pin with an LED on the main board (pins 11 and 12 are safe if you're using an Arduino Uno). Point it in a direction where there's nothing within around 17 feet and start the serial monitor. Then, starting in front of the sensor, walk away till you're 17 feet away. Flank the beam and review the results.
Tim