PDA

View Full Version : Starting and stopping a cog with a push button



mattbutsko
10-12-2011, 03:12 AM
Alright, so I've read over the Methods and Cogs chapter in PE Kit Labs but I can't seem to find a solid sample code to work with to make what i want to happen. basically, can someone give me a basic sample code for having a cog run, then when you push a button/flip a switch, the cog disables until the switch is let go of again.

I just can't seem to be able to use cogstop() properly. sorry about the grammar, i'm typing on my phone. thanks for taking a look.

Mike G
10-12-2011, 04:48 AM
How about posting your code? That way folks can get an idea of what you're trying to do.

Mike Green
10-12-2011, 05:19 AM
You don't actually want the cog to disable (stop) itself. Typically, when a cog is started, it continues to run indefinitely. It may wait for an I/O pin to reach a specific state or it may wait for a particular time to arrive (up to about 50 seconds in the future). The power consumption of the cog will drop markedly when it's waiting using any of the WAITxxx instructions / statements. Remember that it takes on the order of 100us to start up a cog to begin with. If you use COGSTOP to stop the cog, that resets it. It would have to be started up all over again from scratch using COGNEW or COGINIT.

http://forums.parallax.com/attachment.php?attachmentid=78421&d=1297987572 (http://forums.parallax.com/showthread.php?129690&p=978076&viewfull=1#post9780 76)

To pause until a switch changes state, you'd want to use WAITPEQ or WAITPNE. Look up the explanations in the Propeller Manual.

Ariba
10-12-2011, 06:16 AM
Here is short code that makes what you want:

CON
LED = 16 'pin numbers
button = 0

VAR long stack[20]

PUB ATest : run | cog
repeat
if ina[button]==0 and run==0 'if not button and not running
run := 1 'start blink cog
cog := cognew(Blink,@stack)
delay(50) 'debounce
elseif ina[button]==1 and run==1 'if button and running
run := 0 'stop blink cog
cogstop(cog)
delay(50) 'debounce

PRI Blink 'cog routine
dira[LED] := 1
repeat
!outa[LED] 'blink a LED
delay(250)

PUB delay(ms)
waitcnt(clkfreq/1000*ms + cnt)


Andy

mattbutsko
10-12-2011, 04:27 PM
Here is short code that makes what you want:

CON
LED = 16 'pin numbers
button = 0

VAR long stack[20]

PUB ATest : run | cog
repeat
if ina[button]==0 and run==0 'if not button and not running
run := 1 'start blink cog
cog := cognew(Blink,@stack)
delay(50) 'debounce
elseif ina[button]==1 and run==1 'if button and running
run := 0 'stop blink cog
cogstop(cog)
delay(50) 'debounce

PRI Blink 'cog routine
dira[LED] := 1
repeat
!outa[LED] 'blink a LED
delay(250)

PUB delay(ms)
waitcnt(clkfreq/1000*ms + cnt)


Andy


Thanks everyone, but specifically Andy for the example. I can definately build what I need out of this.

Ray0665
10-12-2011, 05:53 PM
You can also stop and start cogs explicitly


CON
_CLKMODE = XTAL1 + PLL16X
_XINFREQ = 5_000_000

clock = 80_000_000
ms1 = clock / 1000

Led1 = 16 'pin assignments
led2 = 23
button = 0

VAR long stack[10]

PUB ATest | state
state := 0 ' Initial state for normally open button
' state := 1 ' initial state for normally closed button
repeat
case ina[button]
0: if state==0 ' if in run state 0
cogstop(6) ' Stop running cog
coginit(2,Blink(Led1),@stack) ' Start Blink in cog A
state:=1 ' indicate run state 1
1: if state==1 ' if in run state 1
cogstop(2) ' Stop Blink in running cog
coginit(6,Blink(Led2),@stack) ' Start Blink in cog B
state := 0 ' indicate state 0
delay(50) ' for switch debounce

PRI Blink(Pin) ' cog routine
dira[Pin]~~
repeat
!outa[Pin] ' blink a LED
delay(250)

PUB delay(ms)
waitcnt(clkfreq/1000*ms + cnt)