code issue
mikea
Posts: 283
I'm having a code problem. Everything works fine until it gets to the case section. It has executed the first case line to play wave file 0(it did so when i waived my hand in front of the ping ..not when it was at its limit of sight ), also it correctly executed the second line to play file 1 when my hand was within a foot, but it wont do either of these things consistently or any of the ones below them at all. The case is supposed to check the value of" range" from ping and play a file if it is in the correct inches range for each file. Does anything look wrong in the case statement? Thanks-mike
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 PING_Pin = 16 ' I/O Pin For PING))) Distlimit = 120 ' In inches do = 0 clk = 1 di = 2 cs = 3 laudio = 11 ' -1 if unused. raudio = 10 ' -1 if unused. volume = 20 ' Default volume. VAR long stack[100] long range long spinPlayerStack[100] OBJ Debug : "FullDuplexSerial" ping : "ping" wav : "WAV-Player_DACEngine.spin" PUB Start cognew(talker,@stack) Debug.start(31,30,0,9600) waitcnt(clkfreq + cnt) repeat ' Repeat Forever debug.str(string(1,"PING))) Demo ", 13, 13, "Inches = ", 13, "Centimeters = ", 13)) debug.str(string(2,9,2)) range := ping.Inches(PING_Pin) ' Get Range In Inches debug.dec(range) debug.tx(11) { debug.str(string(2,14,3)) range := ping.Millimeters(PING_Pin) ' Get Range In Millimeters debug.dec(range / 10) ' Print Whole Part debug.tx(".") ' Print Decimal Point debug.dec(range // 10) ' Print Fractional Part debug.tx(11) } range := ping.Inches(PING_Pin) ' Get Range In Inches pub talker dira[6]~ wav.FATEngineStart(do, clk, di, cs, -1, -1, -1, -1, -1) wav.DACEngineStart(laudio, raudio, volume) waitcnt(clkfreq*4+cnt) if ina[6]==1 wav.playWAVFile(@file12) '"wanna play a game?" ''change back to 12 waitcnt(clkfreq*4+cnt) wav.playWAVFile(@file11) ''''''''''''want me to guess how far away you are? file 11 waitcnt(clkfreq*4+cnt) feet_guesser pub feet_guesser repeat case range range >distlimit: wav.playWAVFile(@file0) ''''''''''' come closer, i cant see you file 0 waitcnt(clkfreq*4+cnt) range >0 and range <24 : wav.playWAVFile(@file1) '''''you are 1 foot away waitcnt(clkfreq*4+cnt) range >12 and range <36 : wav.playWAVFile(@file2) '''you are 2 feet away waitcnt(clkfreq*4+cnt) range >24 and range <48 : wav.playWAVFile(@file3) '''you are 3 feet away waitcnt(clkfreq*4+cnt) range >36 and range <60 : wav.playWAVFile(@file4) '''you are 4 feet away waitcnt(clkfreq*4+cnt) range >48 and range <72 : wav.playWAVFile(@file5) '''you are 5 feet away waitcnt(clkfreq*4+cnt) range >60 and range <84 : wav.playWAVFile(@file6) '''you are 6 feet away waitcnt(clkfreq*4+cnt) range >72 and range <96 : wav.playWAVFile(@file7) '''you are 7 feet away waitcnt(clkfreq*4+cnt) range >84 and range <108 : wav.playWAVFile(@file8) '''you are 8 feet away
Comments
Have it print which section of the code you are in at that time and have it print when it is outside of any cases and just in the general loop
I'm not 100% on this, but I believe I also vaguely remember having issues with waitcnt when I coded like this
waitcnt(clkfreq*4+cnt)
instead I always write it this way now
waitcnt((clkfreq*4)+cnt)
I also write it as follows
(range >0) and (range <24) : wav.playWAVFile(@file1) '''''you are 1 foot away
I'lll give you an example from your first few conditions.
Edit: This is junk code. See kuroneko's explanation below.
As Cluso99 mentioned, you have overlapping ranges. This really doesn't matter much (it just looks bad) since your code and the code I just posted should behave the same.
You could move the first condition to the end and use "other". I think the code would have a more logical flow (to humans reading it) if you started testing for near items and moved the test distance away with each comparison. If you do start with the out of range test, then I'd keep move inward by tesing to see it the range is greater than a certain value.
I agree with turbo's suggestion of adding debug statements. I fill my code with debug statements whenever I'm trying to figure out what it's doing.
In other words all you have are -1/0 (TRUE/FALSE) target expressions which - I assume - is not what you want.
Use the "Go Advanced" button and then "Manage Attachements". You should then be able to upload files.
If you've never used the serial terminal for problem solving, you are in for a treat as it should really make your life easier if you start to.
Kuroneko is like Hawkeye, he can spot a comparison error from a mile away, I can almost guarantee that it is doing whatever it shouldn't be doing because of what he commented about
There are also debuggers such as
PASD by Ariba
Zero-Footprint debugger by Cluso99 (me) - its a bit complex to setup but can trace spin and pasm code.
jazzed has done a debugger too (sorry, forget the name of it atm)
Hanno has a commercial product (sorry, a real senior moment here - forget what he calls it to)
And there are a couple of emulators as well.
Thanks for the second attempt. Now it stuck in my brain too.
I always use case the way you just describe but I didn't catch the problem in the original code or my changed version.
Thanks again for helping me us see the light.