Shop OBEX P1 Docs P2 Docs Learn Events
Cognew? How to start another cog to do a task for me. — Parallax Forums

Cognew? How to start another cog to do a task for me.

Brian CarpenterBrian Carpenter Posts: 728
edited 2010-06-13 00:19 in Propeller 1
right now i have a loop that does x number of things and writes them to variables.
  REPEAT
  
    
    XB.RxFlush
    Celc := (((ADC.in(0))*3300)/4096-500)     'the MCP9700 will be connected to possition 0 on the MCP3808
    PC.str(string("we are sampling the temp now -- "))
    PC.dec(Celc)
    PC.tx(13)
    BatteryVolts := (ADC.average(1,16))                 'the battery will be connected to possition 1 on the MCP3808 and will have a divider circuit to monitor a 12v battery.'
    PC.str(string("and now the battery voltage is -- "))
    PC.dec(BatteryVolts)
    PC.tx(13)
    IF WaterLevelFlag == 1
      XB.str(string("EEEEE"))
      XB.delay(500)
      Emergency             ' motor needs to close immediately




Now i want some other code to work simultaneously to this that will not interfere with timing of this loop
PUB  LEDSTATUS | Flag
  Case Flag
    0:
      REPEAT 20
        !outa[noparse][[/noparse]SLed]
        XB.delay(100)
        !outa[noparse][[/noparse]SLed]
        XB.delay(200) 
    1: 
      REPEAT 20
        !outa[noparse][[/noparse]SLed]
        XB.delay(250)
    2:
      REPEAT 20
        !outa[noparse][[/noparse]SLed]
        XB.delay(50)
    3:
      REPEAT 20
        !outa[noparse][[/noparse]SLed]
        XB.delay(500)



I want this to run all the time and the LED will change how it blinks based on the variable.
How do i do this? Should i be using Cognew?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


It's Only A Stupid Question If You Have Not Googled It First!!

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2010-06-13 00:04
    Brian Carpenter said...
    I want this to run all the time and the LED will change how it blinks based on the variable.
    How do i do this? Should i be using Cognew?
    As you already have a method which encapsulates your visual action(s) simply do this:

    VAR
      long  stack[noparse][[/noparse]20]
    
    PUB main
      ...
      cognew(LEDSTATUS, @stack)
    


    The remaining issue is that your LED status method looks at a local variable (Flag) which isn't very useful (for starters it's undefined). I assume the flag is to be updated by another part of your program, in this case make Flag a global variable (in the VAR section) or pass its address as a function parameter. Make sure that if you use a local variable it stays alive, global is probably best.

    VAR
      long  stack[noparse][[/noparse]20]
    
    PUB main | flag
      ...
      flag := 0
      cognew(LEDSTATUS(@flag), @stack)
    


    Also, could you please reformat your comment in the code section, it makes the page too wide and the posting hard to read.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-06-13 00:19
    Some things you have to talk about first:
    1) How does Flag get set?

    2) How does XB.delay work? If it uses any global (VAR) variables, you may have problems with two cogs potentially changing the same variable at the same time.

    Each cog has its own I/O registers. You'd have to initialize DIRA in the 2nd cog for SLed.

    The code in the 2nd cog has to not exit so you need some kind of REPEAT statement around the code. If it exits from the method, the cog stops.
Sign In or Register to comment.