Can't escape repeat loop
Ozzie Neil
Posts: 16
I am new to propeller having been a Stamp user for a few years.· I am finding SPIN a bit clunky and i am having a problem i can't seem to overcome.
I have a prototype board wired up with 5 inputs to pins 15 to 19.· When anyone of them goes high the value is summed, if the sum is greater than zero then the 3 coginit commands are supposed to execute.· The summing part works well, no problem there.
It all works but the repeat command won't release, i can't stop it and it then appears to hang.· I added a debug string to the end and it won't execute.· The cogint and fire() command does work, it lights up a LED (it will do more than that eventually) but won't escape the repeat command.· I added a second cogint to see if it would release - my thinking was if it·executed the second coginit then the fault was not coginit related.· It executes all 3 coginits, i have tried some repeat whiles etc but to no avail.·
Below is the code i am using, this is my first time on this forum and certainly would appreciate some help.· I am sure it i something simple that i am overlooking.· [noparse]:)[/noparse]· I included the fire() method in case that helps.
· Debug.str(String("Enter delay in seconds: ",9))
· pause := Debug.getdec
···
· repeat
··· value := (ina[noparse][[/noparse]15..19])
··· if value > 0
····· coginit (1,fire(camera,1,pause), @stack[noparse][[/noparse]0])
····· coginit (2,fire(misc1,2,pause*2), @stack[noparse][[/noparse]20])
····· coginit (3,fire(camera,3,pause*4), @stack[noparse][[/noparse]30])
Pub fire (output,cog,delay)
{{
Fires output.· Parameters are output pin, cpu, delay in seconds
}}
· dira[noparse][[/noparse]0..4]· := %11111································ 'set pins to outputs
·
· waitcnt(clkfreq*delay + cnt)························· 'start delay
· outa[noparse][[/noparse]output] := 1···································· 'turn on
· waitcnt(clkfreq/2 + cnt)····························· 'stay on for...
· outa[noparse][[/noparse]output] := 0···································· 'turn off
· cogstop(cog)········································· 'stop cpu
······
I have a prototype board wired up with 5 inputs to pins 15 to 19.· When anyone of them goes high the value is summed, if the sum is greater than zero then the 3 coginit commands are supposed to execute.· The summing part works well, no problem there.
It all works but the repeat command won't release, i can't stop it and it then appears to hang.· I added a debug string to the end and it won't execute.· The cogint and fire() command does work, it lights up a LED (it will do more than that eventually) but won't escape the repeat command.· I added a second cogint to see if it would release - my thinking was if it·executed the second coginit then the fault was not coginit related.· It executes all 3 coginits, i have tried some repeat whiles etc but to no avail.·
Below is the code i am using, this is my first time on this forum and certainly would appreciate some help.· I am sure it i something simple that i am overlooking.· [noparse]:)[/noparse]· I included the fire() method in case that helps.
· Debug.str(String("Enter delay in seconds: ",9))
· pause := Debug.getdec
···
· repeat
··· value := (ina[noparse][[/noparse]15..19])
··· if value > 0
····· coginit (1,fire(camera,1,pause), @stack[noparse][[/noparse]0])
····· coginit (2,fire(misc1,2,pause*2), @stack[noparse][[/noparse]20])
····· coginit (3,fire(camera,3,pause*4), @stack[noparse][[/noparse]30])
Pub fire (output,cog,delay)
{{
Fires output.· Parameters are output pin, cpu, delay in seconds
}}
· dira[noparse][[/noparse]0..4]· := %11111································ 'set pins to outputs
·
· waitcnt(clkfreq*delay + cnt)························· 'start delay
· outa[noparse][[/noparse]output] := 1···································· 'turn on
· waitcnt(clkfreq/2 + cnt)····························· 'stay on for...
· outa[noparse][[/noparse]output] := 0···································· 'turn off
· cogstop(cog)········································· 'stop cpu
······
Comments
So it will repeatidly check ina and when it is greater then 0 it will leave the loop and run your coginit commands.
You can also repeat while which is handy. Page 188 of the manual onwards has quite a few examples.
Graham
Post Edited (Graham Stabler) : 1/1/2010 3:16:11 PM GMT
I have added the print test string in the code since once it finishes the last cogint it should run the next command (print test string) and it doesn't, it prints the first string (enter delay) so setup of debug is correct.
If i use either the until or the while it works up to the last cogint and won't print the test string - ( this is only for debugging purposes). Since it still isn't leaving the loop it won't print the the "Program finished" string which is the last program instruction.
( i have also used repeat while value == 0 )
Debug.str(String("Enter delay in seconds: ",9))
pause := Debug.getdec
repeat 'until value > 0
value := (ina[noparse][[/noparse]15..19])
if value > 0
coginit (1,fire(camera,1,pause), @stack[noparse][[/noparse]0]) 'allow 20 for each stack
coginit (2,fire(misc1,2,pause*2), @stack[noparse][[/noparse]20])
coginit (3,fire(camera,3,pause*4), @stack[noparse][[/noparse]30])
Debug.str(String("Test string"))
'while value == 0
Debug.str(String("Program finished"))
for people to help you with code, you really need to use the CODE buttons (located below the smilies) to contain your code, otherwise the indentations are lost and nobody knows what you're really doing.
Debug.str(String("Enter delay in seconds: ",9))
pause := Debug.getdec
repeat···· 'until value > 0
value := (ina[noparse][[/noparse]15..19])
·· if value > 0
····· coginit (1,fire(camera,1,pause), @stack[noparse][[/noparse]0])········ 'allow 20 for each stack
····· coginit (2,fire(misc1,2,pause*2), @stack[noparse][[/noparse]20])
····· coginit (3,fire(camera,3,pause*4), @stack[noparse][[/noparse]30])
····· Debug.str(String("Test string"))
'while value == 0
Debug.str(String("Program finished"))
On my computer, I'm seeing this:
which has nothing indented under the repeat. Maybe it's showing up on my computer improperly because you aren't using the CODE keys under the smilies to enclose your code, but I think that repeat, by itself, will just hang there forever.
Try this.... I am not sure what logic you are trying to achieve but the Repeat by itself with no indented anything
under it will just sit there for ever and the program will never do anything.
This code might be what you want....... but PLEASE when you post code use the FULL posting Editor not the
Quick Reply one and ALSO use the # key which is one of the editing tools at the top of the Editor IDE.
This # key will create a box that you can put code in and will maintain Indentations.
In SPIN....Indentation is of PARAMOUNT importance and wrongly indented code WILL produce all sorts of
SIDE EFFECTS that you may not want at all.
Anyway...here is my attempt at correcting your code.....pay particular attention to how things are indented.
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Samuel
www.RobotBASIC.com
Post Edited (SamMishal) : 1/3/2010 3:29:58 PM GMT
On a brighter note i replaced coginit with cognew and it works so my query is now more academic rather than a real problem but why would cognew work and not coginit?· Nothing else was changed so the repeat loops i had were fine, troubleshooting 101 tells me the problem must have been coginit since it was the only thing that was changed..
I followed samples in Parallax documentation so i am slightly confised as to why it wouldn't work.· I should have used cognew to begin with, oh well.·
Thanks again for all those who responded, i look forward to an answer on my now academic question.
In coginit YOU have to specify the cog-number.
If the cog with this number is already in use (from some other part of the software that has used cognew)
The coginit command will force this cog to stop its running code and start running the new code specified in the second parameter of coginit.
This means TOTALLY regardless of a cog beeing already in use coginit forces this cog in EVERY CASE to stop working and start the new code.
ALWAYS use cognew. Then you NEVER have to worry about cognumbers. It is all done automatically in the backround for you.
There are only a few special cases where it is an advantage to use coginit. 99.9% it is a disadvantage so always use cognew
best regards
Stefan
I totally agree with Stefan and jazzed on this. Using cognew allows the Propeller to keep track of which cogs it is using and thus the Propeller will prevent conflicts from occurring (so long as you don't try to call up too many cogs). In other words, if you use cognew, you don't have to think so much about what you and your chip are doing. In the past, I thought using coginit would put me more in control of what's happening with my software, but no matter what I tried, I always got burned by it in the end. Stick with cognew and you'll be happy.
have fun,
Mark
I also thought it would give me a greater degree of control but maybe the processor is better at doing that than I am. I think the lesson here is that using coginit is so rarely justified you just don't use it.
Neil