WebbotLib AVR library
WebbotLib It just does it
  C++ documentation  C documentation

Devantech/SRF04_Sonar.h

Devantech SRF04 sonar.
srf04.jpg
The sonar can be connected to any two I/O pins but requires a +5v regulated supply. On the Axon this means using an ADC pin. The sonar can measure distances up to about 300 cm. It may be defined using:
Devantech_SRF04 sensor = MAKE_Devantech_SRF04(F0,F1);
Where F0 is the microcontroller pin connected to the 'Trigger Pulse Input' of the device; and F1 is the microcontroller pin connected to the 'Echo Pulse Output' of the device.
The sensor should be initialised in appInitHardware using:-
distanceInit(sensor);
The sensor can then be read using:-
distanceRead(sensor);
and then the distance in cm will be in sensor.distance.cm
According to the datasheet you should not read the sensor within 50mS of taking the previous reading. Otherwise you will get ghost echoes from the previous reading. This library will automatically detect if that happens and, in the event, will return the previous reading.

Valid XHTML 1.0 Transitional