Gah tried that, and I get even more erratic results! Hehe, I'm not sure what's going on here at all. I'll check this method on my other Arduino and the other SRF05 unit later on, for now here's the (new) complete code, after halving the ultrasound duration before dividing by 58.
int ultraSoundpin = 6;
char disp_string[80];
unsigned int ultrasoundDuration = 0;
unsigned int ultra_dist = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
// switch pin to output
pinMode(ultraSoundpin, OUTPUT);
// send a low, wait 2 microseconds, send a high then wait 10 microseconds
digitalWrite(ultraSoundpin, LOW);
delayMicroseconds(2);
digitalWrite(ultraSoundpin, HIGH);
delayMicroseconds(10);
digitalWrite(ultraSoundpin, LOW);
// switch pin to input
pinMode(ultraSoundpin, INPUT);
// wait for a pulse to come in as high
ultrasoundDuration = pulseIn(ultraSoundpin, HIGH);
ultra_dist = (ultrasoundDuration/2);
// output
Serial.print(ultra_dist/58, DEC);
Serial.print(" cm");
//Serial.print(dist);
//Serial.print(" cm");
Serial.println();
delay(2000);
}
I've tried this with unsigned ints and longs, and with a long for the duration and an int for the distance... just gives me random results from 7-500 odd....!
Thom