Are all these zeros the same thing you get when you use a different sensor library? As other libraries may not return zero for a failed ping, you'd probably need to do an additional command to make the out of range a zero and in range a period.
I'll re-test with that other implementation, but I began to code for false positives and negatives based on my experience with it, not NewPing. Still, I'll see if I can get a comparison going.
I switched to pins 7 and 8 for these tests. Here are my two test sketch baselines:
#include <NewPing.h>
#define trigPin 7
#define echoPin 8
NewPing sonar(trigPin, echoPin, 200);
byte i = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
unsigned int us = sonar.ping();
if (us==0) {
Serial.print("0");
} else {
Serial.print(".");
}
i++;
if (i==80) {
Serial.print(" ");
Serial.println(us);
i=0;
}
delay(10);
}
And the trollmaker sketch, modified to do the same test:
#define trigPin 7
#define echoPin 8
byte i = 0;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
unsigned int us = pulseIn(echoPin, HIGH);
if (us==0) {
Serial.print("0");
} else {
Serial.print(".");
}
i++;
if (i==80) {
Serial.print(" ");
Serial.println(us);
i=0;
}
delay(10);
}
I reverted to the v1.4 baseline for the NewPing tests.
For my flat surface tests, I varied distance (9cm, 22cm, and 40cm) and the ms delay in loop(). To summarize, a 10ms delay was insufficient for any distance and library. 20ms worked flawlessly in NewPing for distances 22cm and above but not 9cm. 20ms worked flawlessly for trollmaker at all distances tested. 40ms worked flawlessly for all distances and libraries tested.
EDIT: I tested flat objects at 50cm, too, and the results mirrored the 40cm distance tests.
Raw data:
Test Suite 1: Flat object 9cm away from emitter rim. Varied ms delay in loop().
10ms: I get false negatives in both sketches (~30% - 50%). The trollmaker sketch has fewer, but it's also slower.
20ms: very few false negatives (maybe 5%) in NewPing. There were 0 false negatives in the trollmaker sketch during the time I ran it.
40ms: no false negatives in NewPing during the time I ran it. There were 0 false negatives in the trollmaker sketch during the time I ran it.
Test Suite 2: Flat object 22cm away from emitter rim. Varied ms delay in loop().
10ms: I get false negatives in both sketches (~30% - 50%). The trollmaker sketch has fewer, but it's also slower.
20ms: no false negatives in NewPing during the time I ran it. There was 1 false negative in the trollmaker sketch during the time I ran it (at start; possible algorithm badness).
40ms: no false negatives in NewPing during the time I ran it. There was 1 false negative in the trollmaker sketch during the time I ran it (at start; possible algorithm badness).
Test Suite 3: Flat object 40cm away from emitter rim. Varied ms delay in loop().
10ms: I get false negatives in both sketches (~20% - 50%). The trollmaker sketch has
many fewer, but it's also slower.
20ms: no false negatives in NewPing during the time I ran it. There was 1 false negative in the trollmaker sketch during the time I ran it (at start; possible algorithm badness).
40ms: no false negatives in NewPing during the time I ran it. There was 1 false negative in the trollmaker sketch during the time I ran it (at start; possible algorithm badness).
Next, I will test irregular objects at rest and at motion. (I do wish I had a spare human head.)