Code to Communicate From a Devantech SRF04 Ultrasonic Range Finder to a Nettmedia BX24 Microcontroller and to Send Those Numbers Serially to a Computer.


(It's a long title, but I did it for searchability)

The following piece of code was translated from the BasicStamp II code included with the Range Finder by Acroname. It uses pulse in and out to measure the ping and sends the data (measured in inches) through a serial port into the computer. I talked to MAX just fine with this code, but I did not test it on Director. Just select, copy and paste. It should be ready to go.

Sub Main()

dim wdist as integer

dim invar as byte
dim outvar as byte
dim inputbuffer(1 to 10) as byte
dim outputbuffer(1 to 10) as byte

call openqueue(inputbuffer,10)
call openqueue(outputbuffer,10)

' I used pins 14 and 13
call definecom3(14,13,bx1000_1000)
call opencom(3,9600,inputbuffer,outputbuffer)

'------
do
call srsonar(wdist)
call putqueue(outputbuffer,wdist,1)
call getqueue(inputbuffer,invar,1)

call delay(0.1)
loop
End Sub

sub srsonar(byref wdist as integer)
' 10 us high init pulse
call pulseout(12, 10, 0)
' The pulsein command returns the round-trip echo time in 1.0824us units.
' I used pin 11
wdist = pulsein (11,1)

' dividing by 74 converts wdist into inches
wdist=wdist\74
debug.print cstr(wdist)
call delay(0.01)
end sub

So, that's about it. Any questions, email me!

Return to Sasha's PhysComp Journal.

Sasha Harris-Cronin
sasha@mortalspaces.com

] mortalspaces [