UltraSonic + BS2
irivine
Posts: 18
I have just purchase the following product , ultra sensor and basic stamp 2 ,Parallax 2x16 Serial LCD(Backlit)·and serial version development board.
From the source code provided from the website , i could display the distance in inches and in cm on the LCD
I have some problem in adding some other features that i would like , this are the following:
1)i would like to display the Speed emit by the urltasonic sensor in the LCD
2)i would like to display the inches , cm and speed up to 2 decimal place , i wonder if its possible
The following is the code for the project
_________________________________________________________________________________________
'
[noparse][[/noparse] Program Description ]
'
[noparse][[/noparse] Revision History ]
'
[noparse][[/noparse] I/O Definitions ]
Ping··········· PIN···· 8·············· ' Ping))) IO pin
LCD············ PIN···· 15············· ' serial output to LCD
'
[noparse][[/noparse] Constants ]
#SELECT $STAMP
· #CASE BS2, BS2E
··· T2400······ CON···· 396
··· T9600······ CON···· 84
··· T19K2······ CON···· 32
··· Trigger···· CON···· 5·············· ' trigger pulse = 10 uS
··· Scale······ CON···· $200··········· ' raw x 2.00 = uS
· #CASE BS2SX, BS2P
··· T2400······ CON···· 1021
··· T9600······ CON···· 240
··· T19K2······ CON···· 110
··· Trigger···· CON···· 13
··· Scale······ CON···· $0CD··········· ' raw x 0.80 = uS
· #CASE BS2PE
··· T2400······ CON···· 396
··· T9600······ CON···· 84
··· T19K2······ CON···· 32
··· Trigger···· CON···· 5
··· Scale······ CON···· $1E1··········· ' raw x 1.88 = uS
#ENDSELECT
LcdBaud········ CON···· T19K2
LcdBkSpc······· CON···· $08············ ' move cursor left
LcdRt·········· CON···· $09············ ' move cursor right
LcdLF·········· CON···· $0A············ ' move cursor down 1 line
LcdCls········· CON···· $0C············ ' clear LCD (use PAUSE 5 after)
LcdCR·········· CON···· $0D············ ' move pos 0 of next line
LcdBLon········ CON···· $11············ ' backlight on
LcdBLoff······· CON···· $12············ ' backlight off
LcdOff········· CON···· $15············ ' LCD off
LcdOn1········· CON···· $16············ ' LCD on; cursor off, blink off
LcdOn2········· CON···· $17············ ' LCD on; cursor off, blink on
LcdOn3········· CON···· $18············ ' LCD on; cursor on, blink off
LcdOn4········· CON···· $19············ ' LCD on; cursor on, blink on
LcdLine1······· CON···· $80············ ' move to line 1, column 0
LcdLine2······· CON···· $94············ ' move to line 2, column 0
LcdCC0········· CON···· $F8············ ' define custom char 0
LcdCC1········· CON···· $F9············ ' define custom char 1
LcdCC2········· CON···· $FA············ ' define custom char 2
LcdCC3········· CON···· $FB············ ' define custom char 3
LcdCC4········· CON···· $FC············ ' define custom char 4
LcdCC5········· CON···· $FD············ ' define custom char 5
LcdCC6········· CON···· $FE············ ' define custom char 6
LcdCC7········· CON···· $FF············ ' define custom char 7
RawToIn········ CON···· 889············ ' 1 / 73.746 (with **)
RawToCm········ CON···· 2257··········· ' 1 / 29.034 (with **)
IsHigh········· CON···· 1·············· ' for PULSOUT
IsLow·········· CON···· 0
'
[noparse][[/noparse] Variables ]
rawDist········ VAR···· Word··········· ' raw measurement
inches········· VAR···· Word
cm············· VAR···· Word
spc············ VAR···· Nib············ ' spaces (for right-justification)
'
[noparse][[/noparse] EEPROM Data ]
'
[noparse][[/noparse] Initialization ]
Reset:
· HIGH LCD····························· ' setup serial output pin
· PAUSE 100···························· ' allow LCD to initialize
Customize:
· SEROUT LCD, LcdBaud, [noparse][[/noparse]LcdCC7,
······················· $00, $02, $11, $09, $11, $02, $00, $00]
Banner:
· SEROUT LCD, LcdBaud, [noparse][[/noparse]LcdOn1, LcdCls]
· PAUSE 10
· SEROUT LCD, LcdBaud, [noparse][[/noparse]LcdLine1, "Distance Sensor ",
······················· LcdLine2, "Please Wait···· "]
· PAUSE 1000
· SEROUT LCD, LcdBaud, [noparse][[/noparse]LcdOn1, LcdCls]
· PAUSE 10
'
[noparse][[/noparse] Program Code ]
Main:
· DO
··· GOSUB Get_Sonar···························· ' get sensor value
··· inches = rawDist ** RawToIn················ ' convert to inches
··· cm = rawDist ** RawToCm···················· ' convert to centimeters
··· IF (cm > 350) THEN
····· SEROUT LCD, LcdBaud, [noparse][[/noparse]LcdLine1, "···· TARGET···· ",
··························· LcdLine2, "· OUT OF RANGE· "]
··· ELSE
····· 'SEROUT LCD, LcdBaud, [noparse][[/noparse]LcdLine1, "PING", 7,")··· "]
····· LOOKDOWN inches, >=[noparse][[/noparse]100, 10, 0], spc
····· SEROUT LCD, LcdBaud, [noparse][[/noparse]LcdLine1, "Dist(In)· ",
··························· REP " "\spc, DEC inches,
··························· " in"]
····· LOOKDOWN cm, >=[noparse][[/noparse]100, 10, 0], spc
····· SEROUT LCD, LcdBaud, [noparse][[/noparse]LcdLine2, "Dist(Cm)· ",
··························· REP " "\spc, DEC cm,
··························· " cm"]
··· ENDIF
··· PAUSE 500
· LOOP
· END
'
[noparse][[/noparse] Subroutines ]
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
From the source code provided from the website , i could display the distance in inches and in cm on the LCD
I have some problem in adding some other features that i would like , this are the following:
1)i would like to display the Speed emit by the urltasonic sensor in the LCD
2)i would like to display the inches , cm and speed up to 2 decimal place , i wonder if its possible
The following is the code for the project
_________________________________________________________________________________________
'
[noparse][[/noparse] Program Description ]
'
[noparse][[/noparse] Revision History ]
'
[noparse][[/noparse] I/O Definitions ]
Ping··········· PIN···· 8·············· ' Ping))) IO pin
LCD············ PIN···· 15············· ' serial output to LCD
'
[noparse][[/noparse] Constants ]
#SELECT $STAMP
· #CASE BS2, BS2E
··· T2400······ CON···· 396
··· T9600······ CON···· 84
··· T19K2······ CON···· 32
··· Trigger···· CON···· 5·············· ' trigger pulse = 10 uS
··· Scale······ CON···· $200··········· ' raw x 2.00 = uS
· #CASE BS2SX, BS2P
··· T2400······ CON···· 1021
··· T9600······ CON···· 240
··· T19K2······ CON···· 110
··· Trigger···· CON···· 13
··· Scale······ CON···· $0CD··········· ' raw x 0.80 = uS
· #CASE BS2PE
··· T2400······ CON···· 396
··· T9600······ CON···· 84
··· T19K2······ CON···· 32
··· Trigger···· CON···· 5
··· Scale······ CON···· $1E1··········· ' raw x 1.88 = uS
#ENDSELECT
LcdBaud········ CON···· T19K2
LcdBkSpc······· CON···· $08············ ' move cursor left
LcdRt·········· CON···· $09············ ' move cursor right
LcdLF·········· CON···· $0A············ ' move cursor down 1 line
LcdCls········· CON···· $0C············ ' clear LCD (use PAUSE 5 after)
LcdCR·········· CON···· $0D············ ' move pos 0 of next line
LcdBLon········ CON···· $11············ ' backlight on
LcdBLoff······· CON···· $12············ ' backlight off
LcdOff········· CON···· $15············ ' LCD off
LcdOn1········· CON···· $16············ ' LCD on; cursor off, blink off
LcdOn2········· CON···· $17············ ' LCD on; cursor off, blink on
LcdOn3········· CON···· $18············ ' LCD on; cursor on, blink off
LcdOn4········· CON···· $19············ ' LCD on; cursor on, blink on
LcdLine1······· CON···· $80············ ' move to line 1, column 0
LcdLine2······· CON···· $94············ ' move to line 2, column 0
LcdCC0········· CON···· $F8············ ' define custom char 0
LcdCC1········· CON···· $F9············ ' define custom char 1
LcdCC2········· CON···· $FA············ ' define custom char 2
LcdCC3········· CON···· $FB············ ' define custom char 3
LcdCC4········· CON···· $FC············ ' define custom char 4
LcdCC5········· CON···· $FD············ ' define custom char 5
LcdCC6········· CON···· $FE············ ' define custom char 6
LcdCC7········· CON···· $FF············ ' define custom char 7
RawToIn········ CON···· 889············ ' 1 / 73.746 (with **)
RawToCm········ CON···· 2257··········· ' 1 / 29.034 (with **)
IsHigh········· CON···· 1·············· ' for PULSOUT
IsLow·········· CON···· 0
'
[noparse][[/noparse] Variables ]
rawDist········ VAR···· Word··········· ' raw measurement
inches········· VAR···· Word
cm············· VAR···· Word
spc············ VAR···· Nib············ ' spaces (for right-justification)
'
[noparse][[/noparse] EEPROM Data ]
'
[noparse][[/noparse] Initialization ]
Reset:
· HIGH LCD····························· ' setup serial output pin
· PAUSE 100···························· ' allow LCD to initialize
Customize:
· SEROUT LCD, LcdBaud, [noparse][[/noparse]LcdCC7,
······················· $00, $02, $11, $09, $11, $02, $00, $00]
Banner:
· SEROUT LCD, LcdBaud, [noparse][[/noparse]LcdOn1, LcdCls]
· PAUSE 10
· SEROUT LCD, LcdBaud, [noparse][[/noparse]LcdLine1, "Distance Sensor ",
······················· LcdLine2, "Please Wait···· "]
· PAUSE 1000
· SEROUT LCD, LcdBaud, [noparse][[/noparse]LcdOn1, LcdCls]
· PAUSE 10
'
[noparse][[/noparse] Program Code ]
Main:
· DO
··· GOSUB Get_Sonar···························· ' get sensor value
··· inches = rawDist ** RawToIn················ ' convert to inches
··· cm = rawDist ** RawToCm···················· ' convert to centimeters
··· IF (cm > 350) THEN
····· SEROUT LCD, LcdBaud, [noparse][[/noparse]LcdLine1, "···· TARGET···· ",
··························· LcdLine2, "· OUT OF RANGE· "]
··· ELSE
····· 'SEROUT LCD, LcdBaud, [noparse][[/noparse]LcdLine1, "PING", 7,")··· "]
····· LOOKDOWN inches, >=[noparse][[/noparse]100, 10, 0], spc
····· SEROUT LCD, LcdBaud, [noparse][[/noparse]LcdLine1, "Dist(In)· ",
··························· REP " "\spc, DEC inches,
··························· " in"]
····· LOOKDOWN cm, >=[noparse][[/noparse]100, 10, 0], spc
····· SEROUT LCD, LcdBaud, [noparse][[/noparse]LcdLine2, "Dist(Cm)· ",
··························· REP " "\spc, DEC cm,
··························· " cm"]
··· ENDIF
··· PAUSE 500
· LOOP
· END
'
[noparse][[/noparse] Subroutines ]
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
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
http://www.parallax.com/Store/Sensors/ObjectDetection/tabid/176/CategoryID/51/List/0/Level/a/ProductID/92/Default.aspx?SortField=ProductName%2cProductName
the speed i am refering to is the pulse emit by the sensor , i am not sure about the caculation of the speed .
Thanks !
The Stamps do not have floating point capability. It is possible though to do the conversion from microseconds to 1/100s of an inch or 1/100s of a cm. You just have to change the divisors used to scale the microsecond values.
Post Edited (Mike Green) : 9/22/2008 6:18:05 AM GMT