Shop OBEX P1 Docs P2 Docs Learn Events
Help needed with programming ping sensor using basic stamp 2.0 — Parallax Forums

Help needed with programming ping sensor using basic stamp 2.0

SSUTIGER15SSUTIGER15 Posts: 1
edited 2013-06-04 08:07 in BASIC Stamp
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??????????'
' {$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

  • davejamesdavejames Posts: 4,047
    edited 2013-06-04 07:56
    Hi SSUTIGER15 - welcome to the Forum!

    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.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-06-04 07:58
    You use a SELECT statement with a CASE for each of the ranges you're interested in. Look at the description and examples of the SELECT statement in the "Basic Stamp Syntax and Reference Manual". You can also use an IF statement for each of the ranges you're interested in like this in place of your current DEBUG statement:

    IF inDistance > 12 AND inDistance < 24 THEN DEBUG "maintenance now",CR
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2013-06-04 08:07
    You can use a series of statement like this:
    [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!
Sign In or Register to comment.