Name:
ultra
Syntax:

ULTRA pin, variable

Pin - is a variable/constant which specifies the i/o pin to use.

Variable - receives the result (0; no object detected, 1-255 distance in centimetres).


Description:

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.

The 'pin' specified in the 'ultra' command 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

 

Applies To:
All M2 and X2 parts
See Also:
Related Create:
Share:
Print:

ultra example

Light an LED if there is an obstacle less than 10cm away.

Code Example:
main:
   ultra C.1, b1   ; measure the distance
   if b1 > 0 and b1 < 10 then
      high B.2      ; LED on
   else
      low B.2      ; LED off
   end if
   pause 200 	   ; wait 200 ms 
   goto main
Copy Code Submit an Example

Submit Your Own Code!

You must be logged in to submit code examples. Login now.

Comments

  • Login to leave a comment.