Running 2 code independently

cdstang1986

New Member
I'm not sure if I'm understanding this right or not. The 08M runs code from top to bottom for say... Lets say we are working on a blinking led.

What I'm looking to day is, say I press the button for the led to blink and its looping till I let go of that button.. At say time I'm looking for start a 2nd led to blinking with out stopping the first one.

So I might just be over saying this but I want the 2 buttons to work independently from one to the other... but be able to turn either on if the even if the other one is in the middle of running the loop code..

Thanks
 

marcos.placona

Senior Member
It really depends on what you're gonna do. Lets say you wanna say :

Pseudo code:

if button 1 pressed then
high 1

if button 2 pressed then
high 2

This will not be on an infinite loop

Any time you press one of the buttons, you will get the led to go high until you tell it to go low

I think I answered your question here, but if I'm missing something, please do say it.
 

cdstang1986

New Member
ok for say.

If button 1 = 1 then goto led 1

led 1

high 1
pause 100
low 1

And have that one looped till I let the button go or = 0. But that the same time i want to press a 2nd button to turn on another led at the same time the first one is running.. And from my understanding is that the way the code runs, it cant do anything till it goes back to main. waiting for a input from one of the two buttons..

Maybe Im thinkin to much into this.
 

marcos.placona

Senior Member
Well, I'm gonna wait till the others come by, but it's pretty much multi-tasking isn't it? I know picaxe can't do that natively, but you might be able to do it another way.
 

kevrus

New Member
Maybe having an 'interrupt' command in the first loop which is activated by the second button would then jump to the 'interrupt' routine. It would then be in this routine that you could code for both LEDs...or this is how I understand the question
 

BeanieBots

Moderator
The bottom line is that the PICAXE cannot run more than one program at the same time but as pointed out by Marcos, it can be made to appear as if it is running several at the same time.
How it's done depends greatlly on exactly what you want to do and there is no real right or wrong method.
Interrupts are an option worth investigating. Look up the command "setint".
A common method I use to make different things happen such as several LEDs flashing at different rates is to use a variable to define the state of what has happened with the inputs. This is known as a "state engine".
Something like the "Branch" statement can then be used to jump to a routine which does whatever is required.
 

papaof2

Senior Member
Consider using the SETINT command. The interrupt returns to the command that was running when the interrupt occurs.

Use a diode OR to bring both buttons to a common pin as the interrupt pin. Keep the status of each pin in variables (button a in b0, button b in b1, etc).

When the interrupt occurs, read the status of each button and compare to the previous status. If the status of button A has changed, change the output pin status (high->low or low-> high) and update the associated variable.

Instead of having a long pause, use a loop of short pauses to allow for more frequent checks for the interrupt:
for b2 = 1 to 100
pause 1
next b2

John
 

hippy

Ex-Staff (retired)
SETINT is okay for detecting a button push but it over-complicates things, messing up timing if waiting for the button to be released within interrupt, or having to change interrupt polarities to detect push and release and I'm not sure mixed-polarity dual interrupts are at all easy to handle.

Here's one way to do it assuming two buttons in Inputs 0 and 1, two LED's+R on Output 2 and 3 ...

Code:
Do
  ' ==== Button on Input 0, LED on Output 2
  b0 = b0 + 1
  If pin0 = 0 Or b0 > 20 Then
    b0 = 0
    Low 2
  Else
    If b0 < 10 Then
      High 2
    Else
      Low 2
    End If
  End If
  ' ==== Button on Input 1, LED on Output 3
  b1 = b1 + 1
  If pin1 = 0 Or b1 > 30 Then
    b1 = 0
    Low 3
  Else
    If b1 < 15 Then
      High 3
    Else
      Low 3
    End If
  End If
  ' ==== Loop timing
  Pause 50
Loop
 
Last edited:

boriz

Senior Member
Would something like this work:
Code:
do
	for fl=0 to 100
		if pin0=1 or pin1=1 then exit else pause 5
	next fl
	if pin0=1 then toggle 2
	if pin1=1 then toggle 3
loop
Note:Untested!
 

boriz

Senior Member
OOPS!

That won't work. But this should:

Code:
do
	if pin0=1 then
		if b2=0 then:toggle 2:endif
		inc b2
	else
		low 2
	endif
	if pin1=1 then
		if b3=0 then:toggle 3:endif
		inc b3
	else
		low 3
	endif
	pause 2
loop
 
Last edited:

moxhamj

New Member
Picaxe can do this easily. It is a matter of understanding the software concepts.

I usually start my programs with
main:
goto main

Then flesh some things out. For a button, you need a variable to act as a flag to indicate the switch position. Say b10
Then for the led you want it to flash every so often, so pick another variable, say b11
For button 2, use b12
for led 2, b13.

Now, in psuedo code:
main:
test button 1
if pressed and b10 =0 then b10=1 else b10=0' a toggle switch in effect
test button2
if pressed and b12=0 then b12=1 else b12=0' toggle switch for button 2
' now we have values 1 or 0 which indicate if a led is supposed to be flashing or not.
' now increment a counter
b11=b11+1
b13=b13+1
'put in a pause
pause 100
' now see if the counter is over a specific value. if it is, then reset.
if b11>20 then b11=0 - and do the same for b13
' and now turn the led on for half the time - eg if the counter is to 20, make it to 10
if b10=1 and b11<10 then led on else led off - and repeat for the second led
' and go back to main
goto main

I appreciate this is not a working program, but I think it is more helpful to understand the structure than to have someone give you a working program. Not in a "eat your greens" sense, but rather because the above program structure is something you will use again and again. Use a flag to indicate if something is on or off. And use a counter to time things (but don't forget to reset the counter).
 
Last edited:
Top