Shop OBEX P1 Docs P2 Docs Learn Events
trouble passing variables — Parallax Forums

trouble passing variables

GrantmcFGrantmcF Posts: 30
edited 2012-10-13 21:29 in Propeller 1
Still having trouble passing variable between cogs. In the attached code I'm trying to start a cog, have it run for 4 min then set a global variable as a flag for a later decision. The problems I'm having is that I can never get the flag set and the 240 sec timer doesn't always work. Any help is appreciated.

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-10-11 20:51
    Change
    Pub Prewet(Pre_wet_done2)
    repeat 240                        'Repeat for 240 times = 4 minutes because of 1 second wait    
      Dira[13..15] :=1             'Sets direction of pins 13-15 to output
      outa[13..15] :=0             'Sets pin 13 high and pins 14 and 15 low
      waitcnt(clkfreq + cnt)          'Wait 1 second
    Pre_wet_done2 := 1                'Set status of Pre_wet_done 
    
     
    

    To:
    Pub Prewet(Pre_wet_done2)
    repeat 240                        'Repeat for 240 times = 4 minutes because of 1 second wait    
      Dira[13..15] :=1             'Sets direction of pins 13-15 to output
      outa[13..15] :=0             'Sets pin 13 high and pins 14 and 15 low
      waitcnt(clkfreq + cnt)          'Wait 1 second
    [COLOR=#ff0000]byte[Pre_wet_done2] [/COLOR]:= 1                'Set status of Pre_wet_done 
    
    

    Alternatively, you can just use:
    Pub Prewet(Pre_wet_done2)
    repeat 240                        'Repeat for 240 times = 4 minutes because of 1 second wait    
      Dira[13..15] :=1             'Sets direction of pins 13-15 to output
      outa[13..15] :=0             'Sets pin 13 high and pins 14 and 15 low
      waitcnt(clkfreq + cnt)          'Wait 1 second
    [COLOR=#ff0000]Pre_wet_done [/COLOR]:= 1                'Set status of Pre_wet_done 
    

    Since all cogs within one object have access to all the global variables.

    I suppose it doesn't hurt to keep setting the direction and output state of the pins but you could do these just once at the beginning of the method and then just loop your waitcnt only. Also the current method of waiting 240 seconds will be little off since it doesn't take into account the loop overhead. I think the waitcnt section of the Propeller manual has instructions on how to have more accurate wait times.
  • JonnyMacJonnyMac Posts: 9,108
    edited 2012-10-11 21:58
    Here's how I'd do it:
    pub pre_wet(seconds, flagpntr) | t
    
      dira[13..15] := %111
      outa[13..15] := %100
    
      long[flagpntr] := false
    
      t := cnt
      repeat seconds
        waitcnt(t += clkfreq)
    
      long[flagpntr] := true
      cogstop(cogid)
    


    Call it like this using the address of a long that is in the global variable declarations:
    cognew(pre_wet(240, @wetdone), @stack)
    


    I think passing the timing allows you to forget about the internals of this method once it works; your main code can adjust timing.
  • StefanL38StefanL38 Posts: 2,292
    edited 2012-10-12 06:39
    Hi GrantmcF,

    I want to point again onto
      DirA[13..15] := %111
    


    instead of
      DirA[13..15] := 1
    

    if you want to set more than one pin to value 1
    you need to assign a value where every bit is set to 1
    easiest way to do this is to use binary numbers through using the "%" in front of the digits

      DirA[13..15] := %111
    

    is the same as
      DirA[13..15] := 7
    


    best regards
    Stefan
  • GrantmcFGrantmcF Posts: 30
    edited 2012-10-13 21:29
    I figured out the problem. It wasn't in the variable passing. It was the repeat loop continually resetting the new cog. The count never finished, so the variable never got a chance to be passed. Another flag and state check before the cog start lets it bypass the cog start if it is already running. Thanks for the help guys.
Sign In or Register to comment.