by Tom Igoe; altered for an 18f252 by Daniel Bartolini

This PicBasic Pro code reads a Devantech SRF04 Sonar ranger.

Define osc 4

distance var word               ‘variable to store the distance
conversionFactor con 14  ‘constant for the speed of sound

initPin var portc.4                ‘what I used on the 18f252
echoPin var portc.3

txPin var portc.6

serout2 txPin, 16468, ["start"]
pause 500                           ‘ pause the whole thing half a second before reading

    Pulsout initPin, 1
    Pulsin echoPin, 1, distance
    distance = distance/conversionFactor
    serout2 txPin, 16468, ["Distance = ", DEC distance, "inches", 10, 13]
    pause 100
goto MAIN