Shop OBEX P1 Docs P2 Docs Learn Events
Sonar Code... — Parallax Forums

Sonar Code...

ArchiverArchiver Posts: 46,084
edited 2000-11-29 21:37 in General Discussion
Hi John and Everyone,

Here is the code that I am using with the 6500 sonar module:

Thanks for your time,
Tim

'================================================
' POLAROID RANGING TEST PROGRAM - 11/26/2000
'================================================



'

'BSII CONFIGURATION OF PINS USED

Output 0 'Serial out to LCD display.
Output 14 'Ranging module control.
Input 15 'Input from ranging module.

'

'INITIALIZE LCD DISPLAY

Pause 2000 'Pause for the LCD display/driver backpack to initialize.

'

'VARIABLES

TIMEOFFLIGHT var word 'Echo variable from ranging module.
DISTANCE var word 'Working variable for calculating distance.

'

'CONSTANTS

ECHO con 15 'Ranging module echo result.
INIT con 14 'Ranging module control.
SETTLE con 1 'Delay to ensure proper ranging module initialization.
DELAY con 2000 'Time between sonar samples - in milliseconds.
CONVFACT con 74 'Result in inches. See CONVERSION FACTORS below.
OFFSET con 700 'Delay between start of sonar reading and measurement of result
- prevents false measurement.

'
'

MAIN_LOOP:

Gosub PING_CALC

Serout 0,$4054,[noparse][[/noparse]254,1] 'Clear the LCD display and reset cursor position.

Pause 2 'Pause for LCD display to clear and cursor position to reset.

Serout 0,$4054,[noparse][[/noparse]"DISTANCE: ",dec3 DISTANCE,"in"] 'Send the title and distance
result to the LCD display.

Pause DELAY 'Time between sonar samples.

Goto MAIN_LOOP

'
'

PING_CALC:

High INIT

Pause SETTLE

Rctime ECHO,0,TIMEOFFLIGHT

Low INIT

'

'DISTANCE FORMULA AND CONVERSION FACTORS

'Distance = (timeofflight + offset time) / (conversion factor)

'Use 74 as a conversion factor for inches. (73.764 microseconds per in.)
'Use 29 as a conversion factor for centimeters. (29.033 microseconds per cm.)

'

DISTANCE=(TIMEOFFLIGHT+OFFSET)/CONVFACT

Return

'

Comments

Sign In or Register to comment.