Shop OBEX P1 Docs P2 Docs Learn Events
Starting and stopping a cog with a push button — Parallax Forums

Starting and stopping a cog with a push button

mattbutskomattbutsko Posts: 8
edited 2011-10-12 09:53 in Propeller 1
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.

Comments

  • Mike GMike G Posts: 2,702
    edited 2011-10-11 20:48
    How about posting your code? That way folks can get an idea of what you're trying to do.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-10-11 21:19
    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.

    attachment.php?attachmentid=78421&d=1297987572

    To pause until a switch changes state, you'd want to use WAITPEQ or WAITPNE. Look up the explanations in the Propeller Manual.
  • AribaAriba Posts: 2,690
    edited 2011-10-11 22:16
    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
  • mattbutskomattbutsko Posts: 8
    edited 2011-10-12 08:27
    Ariba wrote: »
    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.
  • Ray0665Ray0665 Posts: 231
    edited 2011-10-12 09:53
    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)
    
    
Sign In or Register to comment.