View Full Version : Decimal conversion

04-16-2007, 05:11 AM
I need to calculate the speed of sound using the Ds1620 digital thermometer. The formula is

Speed of sound = 331.5 +(0.6× TC)m/s

How can I enter this in to the stamp? I want·to use the speed of sound to calculate the distance of the ping))) sensor at the current temperature. Thanks

Mike Green
04-16-2007, 05:35 AM
You can't enter it into a Stamp because the Stamp does all arithmetic with 16-bit integers where this formula uses floating point. Tracy Allen's website has some good information on fixed point and multiple precision arithmetic (www.emesystems.com (http://www.emesystems.com)). One of the things you can do is to convert these values to fixed point with one decimal point like "speed of sound = 3315 + (06 * TC / 10)" where TC is in tenths of a degree. The extra factor of 10 compensates for the fact that multiplying tenths by tenths gives hundreths. The resulting speed of sound is in tenths of a meter/second.

04-16-2007, 06:02 AM
I just did
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
rawDist = rawDist */ Scale
How does that work?

04-16-2007, 06:04 AM
opps I ment when they use
Scale CON $0CD ' raw x 0.80 = uS

Mike Green
04-16-2007, 06:11 AM
Have a look at the "*/" operator description in the PBasic manual. Basically, it scales the data by 256. Notice that the constant "Scale" is 2 x 256.

04-16-2007, 06:43 AM
I have another problem. I have the speed of sound in M/S but I need it in CM/uS and when I convert it I need to keep the decmal for best accuracy. I am getting answers like 28 us / CM but in the demo program they use 29.034us/cm and that 0.034 is probably a lot of difference. What formula could I use to get the distance with the Ping)))?
uS for return * what?

Mike Green
04-16-2007, 07:01 AM
Do have a look at the EMEsystems website, particularly this page: www.emesystems.com/BS2math1.htm (http://www.emesystems.com/BS2math1.htm).

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 (http://www.parallax.com/dl/docs/prod/sic/SmartSensors-v1.0.pdf).

04-16-2007, 08:04 AM
Thanks I was looking for a sight like that. I think I will be able to get it to work.

04-16-2007, 08:27 AM
I understand how to multiply by fractions now but is there a way to store my result in a fraction? Would multiplying everything by 256 and using the */ work?

Mike Green
04-16-2007, 08:35 AM
The issue is that you have only a 16-bit word to store your values in. The range is 00000 to 65535 or (if signed) -32768 to 32767. If you multiply everything by 256, that leaves a range of 000 to 255 plus a fraction or -128 to 127 plus a fraction. That may work fine, but it may not give you enough precision for what you're doing. The important thing is to figure out the appropriate precision for each value in your program and program fo that.

04-16-2007, 11:14 AM
I finally did it. After an entire day of work (well sorta I played with my new 3 axis accelerometer a lot) I did it. This is a mod of the demo program that uses the temperature to convert to speed of sound and then uses the ping))) to mesure distance. I tested it with a tape mesure and it was dead on each CM.
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}
' ================================================== =======================

' -----[ 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 ]-------------------------------------------------

Ping PIN 15

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

Trigger CON 5 ' trigger pulse = 10 uS
Scale CON $200 ' raw x 2.00 = uS
Trigger CON 13
Scale CON $0CD ' raw x 0.80 = uS
Trigger CON 5
Scale CON $1E1 ' raw x 1.88 = uS

IsHigh CON 1 ' for PULSOUT
IsLow CON 0

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

rawDist VAR Word ' raw measurement
inches VAR Word
cm VAR Word
rawToCM VAR Word

' -----[ EEPROM Data ]-----------------------------------------------------

' -----[ Initialization ]--------------------------------------------------

DEBUG CLS, ' setup report screen
"Parallax Ping Sonar ", 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.
'================================================= =======================================
'================================================= =======================================
SndCvn CON 6 'Speed of sound converter.
SpdSn0C CON 3315 'Speed of sound at 0 C.
'================================================= =======================================
'================================================= =======================================
' Note: DS1620 has been preprogrammed for mode 2.
OUTS=%0000000000000000 ' Define the initial state of all pins,
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, [238] ' Send the "start conversions" command.
LOW 6 ' Do the command.
'================================================= =======================================
'================================================= =======================================
DO ' Going to display once per second.
HIGH 6 ' Select the DS1620.
SHIFTOUT 8, 7, LSBFIRST, [170] ' Send the "get data" command.
SHIFTIN 8, 7, LSBPRE, [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,


' -----[ 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.

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