View Full Version : Help Please. Loop doing strange things

10-19-2011, 09:24 PM
I have been troubleshooting this code for some time now but to now avail. I hate to bother the forum with this but I'm at a standstill.

The code scans 4 push-buttons and starts a new method on a specified cog hen the conditions are met. The code works only when I comment out any of the cognew, coginit, and cogstop commands. The code works if I do everything in the same cog but I as soon as I try a new cog the loop will spit out a weird symbol and stop working altogether.

PUB Main
{{Senses pins XPlus, XMinus, YPlus, YMinus logic state to control
outputs on pins XDir, XStep, YDir, YStep}}


waitcnt(clkfreq/8 + cnt)

Debug.Str(String("Program is running..."))

XCount := 50 'Set XCount variable to 50
YCount := 50 'Set YCount variable to 50

dira[XDir..XStep]~~ 'Set stepper control pins as inputs
dira[YDir..YStep]~~ 'Set stepper control pins as inputs
dira[0..3]~~ '-----Set test LED pins as inputs

!outa[0..3] '-----Test the LED outputs by cycling once at one Hz
waitcnt(clkfreq/2 + cnt)

'End of initialization

repeat 'Loop scans input pins for current state
if (ina[XPlus] == 1) and (ina[XMinus] <> 1) and (XCount < XMAX) 'If button is depressed, without the other button being pressed and XCount is not at its max value, activate the corresponding method
debug.str(String("Step X CW"))
debug.newline 'Corresponding method started in a new cog
'coginit(1, motorStep(CW, XStep), @Stack1)
'cognew(motorStep(CW, XStep), @Stack1)
'waitcnt(clkfreq/5 + cnt)

elseif (ina[XMinus] == 1) and (ina[XPlus] <> 1) and (XCount > XMIN)
debug.str(String("Step X CCW"))
'coginit(1, motorStep(CCW, XStep), @Stack1)
'cognew(motorStep(CCW, XStep), @Stack1)
'waitcnt(clkfreq/5 + cnt)

if (ina[YPlus] == 1) and (ina[YMinus] <> 1) and (YCount < YMAX)
debug.str(String("Step Y CW"))
'coginit(2, motorStep(CW, YStep), @Stack2)
'cognew(motorStep(CW, YStep), @Stack2)
'waitcnt(clkfreq/5 + cnt)

elseif (ina[YMinus] == 1) and (ina[YPlus] <> 1) and (YCount > YMIN)
debug.str(String("Step Y CCW"))
'coginit(2, motorStep(CCW, YStep), @Stack2)
'cognew(motorStep(CCW, YStep), @Stack2)
'waitcnt(clkfreq/5 + cnt)

I have tried every way I could think of to start the "motorStep" method on another cog but I just can't get it to work. Thanks in advance for your help

10-19-2011, 11:42 PM
From the looks of it you're running the Parallax Serial Terminal object. This object - when active - consumes a cog (PASM driver). Which simply means one of your cogstop commands is killing the terminal driver. If you absolutely must stop running cogs then record the ID returned from cognew and stay away from everything else. Also, your output pins are set in the context of (I'm guessing here) cog 0 which means the step method - if it's driving those pins - won't work in a different cog (every cog has its own dira/outa register pair).

10-19-2011, 11:53 PM
Hard to tell from the code you posted but it appears that you set the direction of the pins in one cog and then try to run the motor from another. Do you set the pins to be outputs in the cog that's actually using the pin? Each cog has it's own dira register.

10-20-2011, 08:16 AM
Firstly, welcome to the forum.

I hope you realise that it does take some time to start a cog. It has to load the code from hub and then begin executing.

Since you have not posted all the code, it is hard to tell what is wrong. Likely it is what kuroneko has said.

Peter Jakacki
10-20-2011, 08:19 AM
Please post all of your code, it could be as simple as maybe not allocating enough stack space (or too much) for Stack1 etc

10-20-2011, 12:42 PM
Thanks everyone for the help. Kuroneko hit the nail right on the head. I was stopping the Cog that was running the serial terminal and I didn't realize that I had to change the directory from the current cog. It works now but I still have a lot of refining to do. Thanks again.