' =========================================================================
'
'   File....... Ping_Demo.BS2
'   Purpose.... Demo Code for Parallax Ping Sonar Sensor
'   Author..... Jon Williams -- Parallax, Inc.
'   E-mail..... jwilliams@parallax.com
'   Started....
'   Updated.... 08 JUN 2005
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------
'
' This program demonstrates the use of the Parallax Ping Sonar sensor and
' converting the raw measurement to English (inches) and Metric (cm) units.
'
' Sonar Math:
'
' At sea level sound travels through air at 1130 feet per second.  This
' equates to 1 inch in 73.746 uS, or 1 cm in 29.034 uS).
'
' Since the Ping sensor measures the time required for the sound wave to
' travel from the sensor and back.  The result -- after conversion to
' microseconds for the BASIC Stamp module in use -- is divided by two to
' remove the return portion of the echo pulse.  The final raw result is
' the duration from the front of the sensor to the target in microseconds.


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------
PingA						PIN			14
PingB           PIN     15

' -----[ Constants ]-------------------------------------------------------
Trigger     		CON     5                       ' trigger pulse = 10 uS
Scale       		CON     $200                    ' raw x 2.00 = uS

RawToIn         CON     889                     ' 1 / 73.746 (with **)
RawToCm         CON     2257                    ' 1 / 29.034 (with **)

IsHigh          CON     1                       ' for PULSOUT
IsLow           CON     0

' -----[ Variables ]-------------------------------------------------------
rawDistA         VAR     Word                    ' raw measurement
rawDistB         VAR     Word                    ' raw measurement
inchesA          VAR     Word
inchesB          VAR     Word
cmA              VAR     Word
cmB              VAR     Word

' -----[ EEPROM Data ]-----------------------------------------------------


' -----[ Initialization ]--------------------------------------------------

Reset:
  DEBUG CLS,                                    ' setup report screen
        "Parallax Ping Sonar  ", CR,
        "=====================", CR,
        CR,
        "Time (uS).....       ", CR,
        "Inches........       ", CR,
        "Centimeters...       "


' -----[ Program Code ]----------------------------------------------------

Main:
  DO
    GOSUB Get_Sonars                            ' get sensor value
    inchesA = rawDistA ** RawToIn                 ' convert to inches
    inchesB = rawDistB ** RawToIn                 ' convert to inches
    cmA = rawDistA ** RawToCm                     ' convert to centimeters
    cmB = rawDistB ** RawToCm                     ' convert to centimeters
    DEBUG CRSRXY, 15, 3,                        ' update report screen
          DEC rawDistA, CLREOL,
          DEC rawDistB, CLREOL,
          CRSRXY, 15, 4,
          DEC inchesA, CLREOL,
          DEC inchesB, CLREOL,
          CRSRXY, 15, 5,
          DEC cmA, CLREOL,
          DEC cmB, CLREOL
    PAUSE 100
  LOOP
  END


' -----[ Subroutines ]-----------------------------------------------------

' This subroutine triggers the Ping sonar sensor and measures
' the echo pulse.  The raw value from the sensor is converted to
' microseconds based on the Stamp module in use.  This value is
' divided by two to remove the return trip -- the result value is
' the distance from the sensor to the target in microseconds.

Get_Sonars:
  PingA = IsLow                                  ' make trigger 0-1-0
  PULSOUT PingA, Trigger                         ' activate sensor
  PULSIN  PingA, IsHigh, rawDistA                 ' measure echo pulse
  rawDistA = rawDistA */ Scale                    ' convert to uS
  rawDistA = rawDistA / 2                         ' remove return trip
  PAUSE 100																				'pause between PingA and PingB so the sonar signals can dicipate.
  PingB = IsLow                                  ' make trigger 0-1-0
  PULSOUT PingB, Trigger                         ' activate sensor
  PULSIN  PingB, IsHigh, rawDistB                 ' measure echo pulse
  rawDistB = rawDistB */ Scale                    ' convert to uS
  rawDistB = rawDistB / 2                         ' remove return trip
RETURN