I recently made some code for the
srf04 and the
PING))). I think if you use the pulseIn function, you can get better resolution on the sensor. Here is an example.
#include <WProgram.h>
#include "srf04.h"
#define echoPin 4
#define initPin 3
#define PINGPIN 2
#define SRF04 0
#define PING 1
void setup()
{
pinMode(initPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop()
{
Serial.print("SRF04: ");
Serial.print(getdist(SRF04), DEC);
Serial.print(" PING: ");
Serial.println(getdist(PING), DEC);
}
unsigned long getdist(int sensor)
{
unsigned long time = 0;
switch (sensor)
{
case SRF04:digitalWrite(initPin, HIGH);
delayMicroseconds(10);
digitalWrite(initPin, LOW);
time = pulseIn(echoPin, HIGH);
break;
case PING:pinMode(PINGPIN, OUTPUT);
digitalWrite(PINGPIN, HIGH);
delayMicroseconds(5);
digitalWrite(PINGPIN, LOW);
pinMode(PINGPIN, INPUT);
time = pulseIn(PINGPIN, HIGH);
break;
}
return time;
}