Decimal conversion
Commander_Bob
Posts: 17
I need to calculate the speed of sound using the Ds1620 digital thermometer. The formula is [font=ComicSansMS,Bold size=2]
Speed of sound = [font=ComicSansMS,Bold size=2]331.5 [/font]+([font=ComicSansMS,Bold size=2]0.6[/font]
Speed of sound = [font=ComicSansMS,Bold size=2]331.5 [/font]+([font=ComicSansMS,Bold size=2]0.6[/font]
Comments
Speed of sound = ( 3315 + ( 6 * degc) / 10)
and it worked.
Also how did they do it in the ping demo program? They have
Scale CON $200 ' raw x 2.00 = uS
and
rawDist = rawDist */ Scale
How does that work?
Scale CON $0CD ' raw x 0.80 = uS
uS for return * what?
Thanks
To get cm from meters, you need to multiply by 100. To get us from seconds, you need to multiply by 1,000,000.
To get cm/us from ms/s, that means multiplying by 100/1000000 = 1/10000 or dividing by 10,000. In doing this
on the Stamp while maintaining the maximum accuracy, you need to work this in from the beginning of the calculation,
not just use a single formula. It will help you to write down all the steps, like a "pseudo-program". Read the PING
documentation and the Nuts and Volts articles on it. The "Smart Sensors" tutorial has a whole chapter on using the PING
and working with the speed of sound. Have a look: www.parallax.com/dl/docs/prod/sic/SmartSensors-v1.0.pdf.
Thanks for all your help
' =========================================================================
'
' 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}
'
' =========================================================================
'
[noparse][[/noparse] 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.
'
[noparse][[/noparse] Revision History ]
'
[noparse][[/noparse] I/O Definitions ]
Ping PIN 15
'
[noparse][[/noparse] Constants ]
#SELECT $STAMP
#CASE BS2, BS2E
Trigger CON 5 ' trigger pulse = 10 uS
Scale CON $200 ' raw x 2.00 = uS
#CASE BS2SX, BS2P, BS2PX
Trigger CON 13
Scale CON $0CD ' raw x 0.80 = uS
#CASE BS2PE
Trigger CON 5
Scale CON $1E1 ' raw x 1.88 = uS
#ENDSELECT
IsHigh CON 1 ' for PULSOUT
IsLow CON 0
'
[noparse][[/noparse] Variables ]
rawDist VAR Word ' raw measurement
inches VAR Word
cm VAR Word
rawToCM VAR Word
'
[noparse][[/noparse] EEPROM Data ]
'
[noparse][[/noparse] Initialization ]
Reset:
DEBUG CLS, ' setup report screen
"Parallax Ping Sonar ", CR,
"=====================", CR,
CR,
"Time (uS)..... ", CR,
"Centimeters... "
x VAR Byte ' General purpose variable, byte.
degC VAR Byte ' Variable to hold degrees Celsius.
SpdSnd VAR Word 'Speed of sound.
'========================================================================================
'Constants
'========================================================================================
SndCvn CON 6 'Speed of sound converter.
SpdSn0C CON 3315 'Speed of sound at 0 C.
'========================================================================================
'Initializing
'========================================================================================
' Note: DS1620 has been preprogrammed for mode 2.
OUTS=%0000000000000000 ' Define the initial state of all pins,
'FEDCBA9876543210
DIRS=%1111111111111111 ' as low outputs.
FREQOUT 0, 20, 3800 ' Beep to signal that it is running.
HIGH 6 ' Select the DS1620.
SHIFTOUT 8, 7, LSBFIRST, [noparse][[/noparse]238] ' Send the "start conversions" command.
LOW 6 ' Do the command.
'========================================================================================
'Main
'========================================================================================
DO ' Going to display once per second.
HIGH 6 ' Select the DS1620.
SHIFTOUT 8, 7, LSBFIRST, [noparse][[/noparse]170] ' Send the "get data" command.
SHIFTIN 8, 7, LSBPRE, [noparse][[/noparse]x] ' Get the data.
LOW 6 ' End the command.
degC = x / 2 ' Convert the data to degrees C.
SpdSnd = (3315 + (6 * degC) / 10 )
rawtocm = spdsnd */ 1678
GOSUB Get_Sonar ' get sensor valu
cm = rawDist ** RawToCm ' convert to centimeters
DEBUG CRSRXY, 15, 3, ' update report screen
DEC rawDist, CLREOL,
CRSRXY, 15, 4,
DEC cm, CLREOL
PAUSE 100
LOOP
END
'
[noparse][[/noparse] 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_Sonar:
Ping = IsLow ' 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 ' remove return trip
RETURN