'**************************************************************** 
'*  Name    : Ping_Demo.BAS                                     * 
'*  Author  : Joo Youn Paek modified from Parallax              * 
'*  Notice  : Copyright (c) 2006 Joo Youn Paek                  * 
'*                                                              * 
'*          : All Rights Reserved                               * 
'*  Date    : 2/9/2006                                          * 
'*  Version : 1.0                                               * 
'*  Notes   : This code is for PING))) Sonar Sensor             *                                      
'*          : from Parallax                                     * 
'**************************************************************** 

DEFINE OSC 4 

start: 

'-------[I/O Definitions]----------------------------------------------- 

Ping var portd.1 


'-------[Constants]----------------------------------------------------- 

Trigger  VAR word                               '10 uS trigger pluse 
Scale    var word                               'raw x 10.00 =uS 

RawToIn  var WORD                               '1/73.746 (with**) 
RawToCm  var word                               '1/29.034(with**) 

IsHigh   var word                               'for PULSOUT 
IsLow    var word 

Trigger = 1 
Scale = 10 

RawToIn = 889 
RawTocm = 2257 

IsHigh = 1 
IsLow = 0 


'--------[Variables]----------------------------------------------------- 

rawDist  var word                               'raw measurement 
inches   var word 
cm       var word 


'-------[Program Code]--------------------------------------------------- 

main: 
 Goto Get_Sonar                               'get sensor value 
 after_Get_Sonar:                                                              
  inches = rawDist ** RawToIn                  'convert to inches 
  cm = rawDist ** RawToCm                      'convert to sentimeters 

  Serout2 portc.6,16468,[ "Time(uS)=",DEC rawDist, "  Inches=", DEC inches, "  Centimeters=", DEC cm,13,10] 'report

  pause 500 
  GOTO main 


'------[Subroutines]----------------------------------------------------- 

Get_Sonar: 
  LOW Ping                                  'make trigger 0-1-0 
  PULSOUT  Ping, Trigger                      'activate sensor 
  PULSIN  Ping, Ishigh, rawDist               'measure echo pulse 
  rawDist = rawDist * Scale                      'convert to uS 
  rawDist = rawDist / 2                          'remover return trip 
  goto after_Get_Sonar