Shop OBEX P1 Docs P2 Docs Learn Events
Passing Variables Question — Parallax Forums

Passing Variables Question

bgw56bgw56 Posts: 6
edited 2008-01-07 00:13 in Propeller 1
My questions is as follows:
I have two objects running , one is sensing push button states, the other generating servo pulse timing in a repeat loop.
Is there a way to pass the button state info from the first object into the servo loop of the second while it running for motor control . I've considered passing control info using a pair of I/O pins. One outputting button states and the other being sensed inside the servo pulse timing loop. That seems like the hard way.What's the easy way?

Thanks !

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-01-06 19:33
    The easy way is just to use a shared variable. You have a single shared variable (byte, word or long as needed) in your main program and you pass the address of this variable to first the push button object (where it's initialized), then the servo object (where it's referenced). The push button object just has to put useful information in it (like the button states) and the servo object can reference it.

    Because it's a single byte / word / long and only one cog changes it (the button one), you don't need to use locks. If you needed more than one storage unit (like an array of words) or if the servo object had to change it, you might have to use locks, but not with the situation you've outlined.
  • bgw56bgw56 Posts: 6
    edited 2008-01-06 21:12
    Hi Mike,
    I've succeeded in passing variables between the two objects (button to servo) but never into the timing loop.

    Your way sounds like just what I'm looking for.
    Can you give me the how-to's of declaring and sharing variables the way you mentioned.
    Also, how they can be changed and read by multiple objects.

    Thanks
    BW
  • bgw56bgw56 Posts: 6
    edited 2008-01-06 21:47
    Mike,
    I think I get it now. Just didn't know where to start.
    Reading/writing Bytes of Main Memory (Syntax3) Manual P.167

    Thanks
    BW
  • Mike GreenMike Green Posts: 23,101
    edited 2008-01-06 22:08
    I really can't make much more suggestions without using your code as a framework. How about posting it as attachments to a reply?
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-06 23:00
    How to dereference a pointer is not too obvious... The syntax is - "funny"
  • bgw56bgw56 Posts: 6
    edited 2008-01-06 23:41
    Hi,
    After a quick read of Byte and Dat,
    Here is some code I tried as a test. I thought it might be possible to share the Dat array between objects.
    It works when run in first object alone.

    What I need to do in a nutshell : read a button in object 1. Pass that data into a running loop in object 2.
    Object 2 could be as simple as stopping a flashing led.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-01-07 00:01
    Here's a simple example:
    VAR
       long myData
       long stack[noparse][[/noparse]30]
    
    PUB main | temp
       startOther(@myData) ' Pretend this is in another object
    
    ' --- the next loop could be in yet another object
    
       repeat
          ' read a button and put some value into temp
          long[noparse][[/noparse]@myData] := temp  ' Transfer the value to the shared variable
    
    ' --- pretend this is now another object
    
    PUB startOther(pointer)
       long[noparse][[/noparse]pointer] := 0   ' Initialize the shared variable
       cognew(otherMethod(pointer),@stack) ' Launch the other cog and return
    
    PRI otherMethod(pointer)
       if long[noparse][[/noparse]pointer] <> 0
          ' do something if non-zero
       else
          ' do something else if zero
    
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-07 00:04
    As your code was a little bit confusing I have simplified it:
    OBJ                         
         PP : "Peek&Poke_B"     
    
    PUB Button
      'Debug.start(31, 30, 0, 57600)
       repeat
            MyData[noparse][[/noparse] 0 ] := INA[noparse][[/noparse] 0 ]
            MyData[noparse][[/noparse] 1 ] := INA[noparse][[/noparse] 1 ]
      
    DAT
       MyData     byte 0,0
    


    VAR
      long offset1,offset2
    
    PUB Main
    
        repeat    
          offset1 := MyData[noparse][[/noparse] 0 ]
          offset2 := MyData[noparse][[/noparse] 1 ]
          Printit
    
    DAT
       MyData     byte 0,0
    



    This will not work of course. You should have done what Mike had suggested above! And a lot more...
    Mike's suggestion would have lead to the following code; I resort the object tree so it makes sense


    VAR
      long offset1,offset2
    OBJ A: "PeekAndPokeA"
    
    PUB Main 
        A.start(@MyData)
        repeat    
          offset1 := MyData[noparse][[/noparse]0]
          offset2 := MyData
          Printit
    
    DAT
      MyData     byte 0,0
    



    VAR
       LONG stack[noparse][[/noparse]20]
    PUB start(thePointer)
       pointer := thePointer
       COGNEW(Button, @stack)
    
    PUB Button 
       repeat
            BYTE[noparse][[/noparse]pointer][noparse][[/noparse] 0 ] := INA[noparse][[/noparse] 0 ]
            BYTE[noparse][[/noparse]pointer][noparse][[/noparse] 1 ] := INA[noparse][[/noparse] 1 ]
    
    



    All right?

    ----
    Edit: Oh, Mike was 3 minutes faster smile.gif Well I am a slow typer...

    Post Edited (deSilva) : 1/7/2008 12:12:26 AM GMT
  • bgw56bgw56 Posts: 6
    edited 2008-01-07 00:13
    Thanks for your answers. I'll let you know how I make out. Could take a while.
    BW
Sign In or Register to comment.