Simple Spin Question
Kirk Fraser
Posts: 364
Why does this code execute only once and not repeat?
Thanks.
''Turn on LED at pin PUB LedOn dira[4] := 1 dira[5] := 1 dira[27] := 1 repeat outa[4] := 1 outa[5] := 1 ' outa[27] := 1
Thanks.
Comments
To get code to repeat you'll need instruction for a loop. Spin has a built in command 'repeat' that works nicely. I'll find you a link to the synopsis.
Doc
EDIT: I missed the REPEAT in the middle of your code. Doh! Let me come up with a better answer.
Hope this helps.
This toggles your bits and inserts a 1/2s delay in between (so you can see it).
BTW... try to avoid the bad habit of using pin numbers in your code like this; used named constants instead. Easier to read, update, and troubleshoot.
This works on all 3 pins when executed separately:
But this appeared to work once or twice but more often fails to repeat and seems to revert to an old program stored in EEPROM after it runs once. I must have a short somewhere, sorry. Any idea what short would cause that behavior?? Thanks.
Or possibly another problem. When I try to run the combined 3 pin program it appears to do nothing for a while then pin 4 flashes for a while with the others off. Then when I run the individual program they all work fine. Then when I run the 3 pin program same story.
So it can't be a simple short since it's on a table and nothing wiggles during or between runs. Is it a bug in my Prop? How could it choose to switch between working and not working repeatedly for these two programs with no hardware changes?
If the program in EEPROM is started then you had a Reset. The reason may be that the supply voltage goes too low when all 3 LEDs are on. (low Battery?).
Andy
You can NOT have repeat (forever) inside PUB Subroutines.
Only the master routine can repeat forever.
Doc
Or if the program runs out of code.
You'll want a repeat somewhere in the code to keep it alive (all of the programs Kirk posted so far include a repeat so this shouldn't be the problem).
Kirk, you might want to load the programs to EEPROM (F11) while testing. Sometimes a PC can cause a Prop attached via USB to reset without a good reason.
Do you have resistors on the LEDs ?
Bean
Now I have to keep tracing as nothing is showing on my transistor LED's which have resistors. Could be i need to put them on 24 volts to see anything or keep debugging.
Thank you all.
Kirk, It would probably be a good idea to post a schematic (or at least a good description) of how your transistors are connected.
As Tony mentioned, you want to use resistors when powering LEDs from a Prop I/O pin. I generally use 100 ohm since that's what the tutorial I read suggested. A single Prop pin shouldn't source or sink more than 40ma. There's also a limit for the overall current (which I don't recall).
Is this really true? Seems like a weird limitation.
If it is, I can't find a reference to it in the Manual (I'd expect to see it on P189)
Tony should have said only one repeat forever per cog.
Each cog can only execute one branch of code at a time so it can't be running two continuous loops at once. A single cog can pause one loop and execute a second for a while and then continue with the first but the loops have to occur one at a time inside a single cog.
You can have up to eight continuous loops at once by using other cogs.
Could you imagine how crippling it would be for a microcontroller to only be allowed one continuous loop? How limiting.
For example you might have a method that sets up a load of stuff and then for whatever reason calls another method that contains the repeat loop ("forever loop") of your application. Or you might have the initialization in its own method and you end up with something looking like: