Shop OBEX P1 Docs P2 Docs Learn Events
Code help — Parallax Forums

Code help

BlaineBlaine Posts: 6
edited 2006-02-28 23:30 in BASIC Stamp
Sorry if this is in the worng forum [noparse]:([/noparse]
I have succsefully built my Ping sensor to take raw data and use FREQOUT to make a tone. Now I would like to ask the programming gods why only one of my IF...ENDIF staments work(only the middle one does) I don't understand why and would love someone to help me.
Here is the code I used--
' PingTest.bs2
' {$STAMP BS2p}
' {$PBASIC 2.5}
msReturn VAR Word
DO
PULSOUT 15, 5
PULSIN 15, 1, msReturn

DEBUG HOME, "time = ", DEC5 msReturn
IF msReturn < 700 AND msReturn >= 100 THEN
FREQOUT 11,500,220 ' play A below middle C
ENDIF
IF msReturn < 500 AND msReturn >= 700 THEN
FREQOUT 11,500,233 ' play A# below middle C
ENDIF
IF msReturn < 300 AND msReturn >= 500 THEN
FREQOUT 11,500,247 ' play B below middle C
ENDIF
PAUSE 100
LOOP


It seems though that only the
IF msReturn < 500 AND msReturn >= 700 THEN
FREQOUT 11,500,233 ' play A# below middle C
ENDIF

stament works. I'm not sure why. Do I need a loop function or something?

Blaine Conner

p.s. I also found some interesting code for a "sonamin" whitch is the same idea as a Thermin
whitch you just put your had next to the sonar device and it changes pitch according to where your hand is. Here is some of the code I found, but it doesn't work for the PING for some reason, it has some things that are already built in to the PING, but I don't know how to adapt it. I also think the code is for a BS2, not my bs2p. Again thanks for the help!

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-02-21 00:00
    How can you tell the code is working as you say?· The only difference in feedback from the BASIC Stamp is 3 different tones that are so close it would be hard to discern them.· You might want to try a DEBUG statement to be sure.· In fact, you may want to DEBUG the direct return value from the sensor to see exactly what you're getting back.· It may not be what you expect.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-02-21 00:05
    You have some basic logic errors: you cannot have a value be less than 500 AND greater than or equal to 700 -- perhaps you meant to use OR.· What notes to you want to play for given values?· In this application, SELECT-CASE might be a cleaner coding choice.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax

    Post Edited (Jon Williams (Parallax)) : 2/21/2006 5:35:53 AM GMT
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-02-21 05:03
    Gents -

    I suspect this is just a "rookie" error with the greater than ( > ) and less than ( < ) symbols being reversed in some cases <oops>. I finally stopped making that "rookie" error early on, when I set it straight in my mind that the the less than symbol ( < ) looks much like an angled "L" (l-ess than) smile.gif

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->

    Post Edited (Bruce Bates) : 2/21/2006 5:20:29 AM GMT
  • BlaineBlaine Posts: 6
    edited 2006-02-24 22:23
    ' PingTest.bs2
    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    msReturn VAR Word
    DO
    PULSOUT 15, 5
    PULSIN 15, 1, msReturn
    DEBUG HOME, "time = ", DEC5 msReturn
    IF msReturn < 700 AND msReturn >= 100 THEN
    FREQOUT 11,1000,440 ' play A
    ENDIF
    IF msReturn < 1300 AND msReturn >= 701 THEN
    FREQOUT 11,1000,392 ' play g
    ENDIF
    IF msReturn < 2000 AND msReturn >= 1301 THEN
    FREQOUT 11,1000,349 ' play f
    ENDIF
    IF msReturn < 2700 AND msReturn >= 2001 THEN
    FREQOUT 11,1000,329 ' play e
    ENDIF
    IF msReturn < 3400 AND msReturn >= 2701 THEN
    FREQOUT 11,1000,293 ' play d
    ENDIF
    IF msReturn < 4100 AND msReturn >= 3401 THEN
    FREQOUT 11,1000,261 ' play c
    ENDIF
    IF msReturn < 4800 AND msReturn >= 4101 THEN
    FREQOUT 11,1000,247 ' play b
    ENDIF
    IF msReturn < 5500 AND msReturn >= 4801 THEN
    FREQOUT 11,1000,220 ' play a
    ENDIF


    LOOP

    this is it now. And it works great!
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-02-24 23:40
    Congrats -- and let me make a suggestion for your program.

    I don't use SELECT CASE much, but here's a program that benefits from it.· You can make your IF-THENs more readable by doing this:

    · SELECT msReturn
    ··· CASE 100 TO 699
    ····· FREQOUT Spkr, NoteTm, 440
    ··· CASE 700 TO 1299
    ····· FREQOUT Spkr, NoteTm, 392
    ··· CASE 1300 TO 1999
    ······FREQOUT Spkr, NoteTm, 349
    ··· CASE 2000 TO 2699
    ····· FREQOUT Spkr, NoteTm, 329
    ··· CASE 2700 TO 3399
    ····· FREQOUT Spkr, NoteTm, 293
    ··· CASE 3400 TO 4099
    ····· FREQOUT Spkr, NoteTm, 261
    ··· CASE 4100 TO 4799
    ····· FREQOUT Spkr, NoteTm, 247
    ··· CASE 4800 TO 5499
    ····· FREQOUT Spkr, NoteTm, 220
    · ENDSELECT

    As you can see this is a lot easier to follow, and actually fixes the inter-note gaps you have in your IF-THEN statements (if you follow your logic, nothing will be played for return values of 700, 1300, 2000, etc.).· Another thing I very strongly suggest is using definitions for the output pin and the note time so you can adjust things quickly.· The code above would need this:

    Spkr··· PIN··· ·11
    NoteTm· CON···· 1000


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • BlaineBlaine Posts: 6
    edited 2006-02-28 23:30
    wow, I can't wait to try that code. I showed my physics teacher my project and he loved it, and played with it for ten minutes! Thanks again!
Sign In or Register to comment.