Run 2 Processes at SAME time?

ChedderCheeser

Senior Member
Can my Picaxe 08M run 2 pieces of code at the SAME time? Ex. Blink an led AND run an alarm system? Also, I spell chedder (my "name") with an E not an A. :D:D Anyway, Let me know if its possible and if so, how to do it. THANKS!!!
 

papaof2

Senior Member
How much the chip can do at (more or less) the same time is limited by program memory and programmer creativity ;-)

Air code:

'=================
MyStart:
'turn LED on
High LEDpin

'check door switch
If DOORpin = 1 then
'do alarm action
end if

'turn LED off
Low LEDpin

'check window switch
If WINDOWpin = 1 then
'do alarm action
end if

Goto MyStart

'===================

Obviously, there's a lot of wiring to be done before this code actually works.

John
 

eclectic

Moderator
Can my Picaxe 08M run 2 pieces of code at the SAME time? Ex. Blink an led AND run an alarm system? Also, I spell chedder (my "name") with an E not an A. :D:D Anyway, Let me know if its possible and if so, how to do it. THANKS!!!
What sort of alarm?
It is possible to run two processes
by quickly switching between two tasks.


Please tell us more about your project(s).

e
 

BeanieBots

Moderator
Sort of.
but it's up to YOU (the programmer) to decide how it's implemented.
HOW to do it will be largely based on what EXACTLY you want to do. There is IDEAL method.
For your example, I would write a simple routine to falsh the LED and then use interrupts to monitor the alarm inputs.

Have a look at the 18M2 which has four threads which are time sliced giving you the abilty to write four routines which will appear to run simultaneously.
 

ChedderCheeser

Senior Member
What sort of alarm?
It is possible to run two processes
by quickly switching between two tasks.


Please tell us more about your project(s).

e
Well, that was just an example... but idk... I was just wondering.. it would be useful... like... something like this:

Code:
main:
	gosub checkalarm
	gosub flashled
	gosub checkalarm
	gosub flashled
goto main

flashled:
	high 4
	pause 500
	low 4
	pause 500
	return

checkalarm:
	if in0 = 1 then
		'alarm code
	endif
	return
Idk... just would be nice to know. Thats not really the SAME time... but... idk...
 
Last edited:

MartinM57

Moderator
No, not AT THE SAME TIME

More advanced program designs and coding methods will give the illusion of *at the same time* as they switch between different tasks very quickly.

So no - the processor can only run one "instruction" at once. Up to you to decide how and when to switch between tasks quickly.
 

ChedderCheeser

Senior Member
If all you want to do is flash an LED then you could use pwmout which will work in the background, albeit fast. I would find it a lot easier to just have a 'flashing LED' installed ( http://www.maplin.co.uk/5mm-flashing-leds-35804 ).
Well.. thats no fun lol. How does this look:

Code:
init:
	symbol sensorpin = 1
	output 4
	input 1
goto main

main:
	gosub checkalarm
	gosub flashled
	gosub checkalarm
	gosub flashled
goto main

flashled:
	high 4
	pause 500
	low 4
	pause 500
return

checkalarm:
	If sensorpin = 1 then
		'do alarm action
	end if
return
I get this though:
Code:
 If sensorpin = 1 then
             ^

Error: Syntax error in this line!
HMMM???...
 

BeanieBots

Moderator
Just get another 08M.
Very cheap, no worries about how to thread the code and they really will run at the SAME time.
 

hippy

Ex-Staff (retired)
How does this look:

Code:
flashled:
	high 4
	pause 500
	low 4
	pause 500
return
The problem with that code is that while flashing the LED the program is completely tied up doing that for a whole second. During that time a nifty burglar could have opened the door, snuck in, and closed it again.

You have to get a bit more complicated in slicing the program and interleaving each task ... or take a look at the 18M2 which better handles that sort of thing automatically, supports multi-tasking.
 

inglewoodpete

Senior Member
Code:
 If sensorpin = 1 then
             ^

Error: Syntax error in this line!
HMMM???...
Using the substitution that you have in your code your command is:

If 1 = 1 Then

You need to use:

If pin1 = 1 Then

...so change the symbol statement:

symbol sensorpin = pin1

In order to run 2 or more tasks, you need to get rid of large pause statements. The bigger PICAXEs make it easier because they have independent timers and timer interrupts.

On a small PICAXE like an 08M, do some thing like the following. Note that the program flow is kept open, with only a small pause statement.

Code:
#PICAXE 08M
Symbol SensorPin = pin1
Symbol cFlashPeriod = 200   'you will need to fiddle with this value
Symbol wTimer = w3
'
Do
	If SensorPin = 1 Then
		GoSub CheckAlarm
	Else
		Pause 5
	EndIf
	Dec wTimer
	If wTimer = 0 Then
		GoSub FlashLED
	EndIf
Loop
FlashLED:
	toggle 4
	wTimer = cFlashPeriod  'Reset timer
	return

CheckAlarm:
	'alarm code
	return
Note: syntax not checked!
 

westaust55

Moderator
@Chedder,

PICAXE chips can only perform one command at a time.

But, if you are crafty with your programming bytes you can simulate time slice methods which makes it appear as if multiple actions are occurring at the same time.
This is pretty much what the newest PICAXE, the 18M2 does with its 4 separate start points.

Programming can be a dry matter, but you can set up blocks of program code that go a round in a loop with multiple tests and actions within. Gradually programming should become natural but some folks find cut and paste from other folks code an easy way to learn so don’t be put off.

If you keep in mind that on average, each simple BASIC command takes 0.25 milliseconds, compared with PAUSE 1 which takes approx 1 millisecond.

Then inter-twining actions in the manner of the example IWP has given helps to make several tasks appear to occur at once. Using the TOGGLE command in a loop will alternately turn the LED on and off and saves needing separate HIGH and LOW commands and two PAUSE commands.
All that said, it may generally be easier for folks to help with a specific project or example than abstract ideas – hence eclectic asking for details of your project.

and . . try not to worry about folks making the odd spellin' mistooke. ;)
 

Technical

Technical Support
Staff member
The new 08M2, released today (1 July 2011), can run up to 4 tasks in parallel. Take a look at the 'parallel tasks' section in part 1 of the PICAXE manual.
 
Top