The 'ultra' command calculates the distance to an obstacle using the SRF005 ultrasonic range finder (distance sensor).
The SRF005 module should be wired for "mode 2" operation (with pin 4 to 0V) so that it uses a single pin for both trigger and echo. This is described in the SRF005 datasheet. If you wish to use 'single pin mode' instead specify two pins (trigger, echo) in the command.
The 'pin' specified in the 'ultra' command for single pin mode must be a bi-directional pin (capable of both input and output) and is used to trigger the ultrasound sensor and read the sensor echo pulse.
The 'ultra' command initiates a triggering of the SRF005 ultrasound sensor connected to the specified pin, then the returned echo pulse is measured and converted to a byte value which is placed in the 'variable' as a 1 to 255 distance in centimetres. A value of zero indicates no object was detected within range. Any object detected over 255 cm away (e.g. at 3m) will also return a value of 255.
Effect of Increased Clock Speed
This command will only function correctly at the default 4MHz (8MHz on X2 parts).
Equivalent commands
An 'ultra pin, variable' command on a PICAXE M2 is equivalent to -
pulsout pin, 2
pulsin pin, 1, wordvariable
variable = wordvariable max 1479 * 10 / 58
An 'ultra pin, variable' command on a PICAXE X2 is equivalent to -
pulsout pin, 2
pulsin pin, 1, wordvariable
variable = wordvariable max 2958 * 5 / 58
'Inch' equivalent
There is no 'inch' version of the ultra command within the PICAXE language, but inch calculation could be programmed by the user as follows:
Inch version equivalent on PICAXE M2 -
pulsout pin, 2
pulsin pin, 1, wordvariable
variable = wordvariable max 3774 * 10 / 148
Inch version equivalent on PICAXE X2 -
pulsout pin, 2
pulsin pin, 1, wordvariable
variable = wordvariable max 7548 * 5 / 148