Help needed with programming ping sensor using basic stamp 2.0
I used the below code to detect distance with the ping sensor but now I want to convert that distance into a message i.e. If distance between 12-24 inches "maintenance immediately", If distance between 24-36 inches 'maintenance needed within two weeks". How do I include that in the below
code??????????'
code??????????'
' {$STAMP BS2}
' {$PBASIC 2.5}
Ping_Pin CON 0
InConstant CON 890
inDistance VAR Word
time VAR Word
DO
PULSOUT Ping_Pin, 5 ' Send short pulse to Ping
PULSIN Ping_Pin, 1, time ' Wait for echo
inDistance = inConstant ** time ' Convert to inches
DEBUG CR, DEC inDistance ' Display result
PAUSE 200 ' Short delay until next read
LOOP
Comments
You'll need to use one of the comparison operations in PBasic such as IF/THEN, or SELECT/CASE.
Take a look at those commands in the help file or syntax manual for an explanation of how they work. Also, those sections have code samples.
BTW - I added some key words to your code that make it more readable - check 'em out.
IF inDistance > 12 AND inDistance < 24 THEN DEBUG "maintenance now",CR
[FONT=courier new][SIZE=1]IF inDistance > 12 AND inDistance <=24 THEN DEBUG "maintenance immediately",13 ENDIF [/SIZE][/FONT]
More elegant way to do it:[FONT=courier new][SIZE=1]SELECT inDistance CASE <12 DEBUG "!!!!!!!",13 CASE 12 TO 23 DEBUG "maintenance immediately",13 CASE 24 TO 36 DEBUG "'maintenance needed within two weeks",13 CASE > 36 DEBUG "'All okay",13 ENDSELECT[/SIZE][/FONT]
PS, I see you don't have to wait around long here to get concurring opinions!