Code help
Blaine
Posts: 6
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!
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 Savage
Parallax Tech Support
csavage@parallax.com
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Post Edited (Jon Williams (Parallax)) : 2/21/2006 5:35:53 AM GMT
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)
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
<!--StartFragment -->
Post Edited (Bruce Bates) : 2/21/2006 5:20:29 AM GMT
' {$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!
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