sharing value between methods
shmow
Posts: 109
Any advice on how to share a value between two methods (which are running on separate cogs)?
When I press the button the LED turns on but it won't flash - my code is below.
Thanks,
Shmow
When I press the button the LED turns on but it won't flash - my code is below.
Thanks,
Shmow
'' File: Sharing Variable Content.spin {{One method manipulates variable's value and another method annunciates it to a LED on pin17 - only when a pushbutton (pin 23) is pressed}} CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 DAT Value_Led byte 0 VAR long stack[noparse][[/noparse]20] byte LED_One PUB Main 'with pushbutton pressed, second cog and method are started 'when button is released, second cog is stopped and P17 goes low dira[noparse][[/noparse]17] := %1 repeat if ina[noparse][[/noparse]23] coginit(7, Second_Flash, @stack[noparse][[/noparse]10]) 'method starts in separate cog Led_One := Value_Led outa[noparse][[/noparse]17] := Led_One else cogstop(7) outa[noparse][[/noparse]17] := 0 PUB Second_Flash 'value repeatedly toggles between one and zero at one second intervals 'then assigned to memory location repeat byte[noparse][[/noparse]@Value_Led] := 1 waitcnt(clkfreq + cnt) byte[noparse][[/noparse]@Value_Led] := 0 waitcnt(clkfreq + cnt)
Comments
It takes a while for COGINIT or COGNEW to finish. They both set up the other cog, but finish and go on to the next statement a long time before the other cog actually finishes loading and starts (over 100us). Your program loops back and does a COGINIT again long before the 2nd cog would finish loading starting the process over again. Even when the button is released, the new cog is STOPped before it could ever start running.
when the input 23 (pushbutton) goes high...
-the 8th cog is started
-which runs Second_Flash method - toggling a value between 1 and 0 every two seconds
-the Second_Flash method also assigns this changing value to a memory location (byte[noparse][[/noparse]@Value_Led])
-the Main method (running in the default and separate cog) reads the contents of this memory location
-and assigns this value to the output pin (17).
when the input 23 (pushbutton) goes low...
-the 8th cog stops
-the output pin (17) also goes low
How I've interpreted your reply: If I hold down the button then the 8th cog is re-initiated constantly,
or this cog is stopped before anything can happen (if I release the button too soon.)·
To test that idea, I edited out the code in the ELSE portion of that method but still no flashing output.
Looks like I haven't grasped the timing·of cogs yet.·
Perhaps you can steer me in the right direction.
Repeatedly re-initialising the cog doesn't give it time to do anything. A bit like dragging a runner back to the start line while the race is under way.
Note that the second cog doesn't need to use the byte notation to access the variable.
Anything in a VAR or DAT section is global to the object.
This still won't do what you say you want, but it will do what the original program
was written to do. You need to only start the blinking cog once, when the pushbutton
is initially depressed. If the button is still on and the cog is already started, you just
want to copy Value_Led to the output pin.
Normally with the Propeller, if you want a cog to do blinking like this, you give control
of the I/O pin over to that cog. The direction register bit in the cog is set to output
and the cog does the blinking. You could even dispense with the variable since the
input registers of all the other cogs give the actual output state of the I/O pin. You
wouldn't need to turn off the LED since, by stopping the blink cog, the direction
register of the blink cog would be cleared to zero making the I/O pin an input.
-Phil
Post Edited (hippy) : 3/17/2008 11:51:55 AM GMT