how can I run multiple programs at once?

tesladude

New Member
I have read that m2 parts can do multiple programs at once but I cannot find out how to do this.
So is it possible (for example) to have 2 seperate programs running at once like haveing 2 LEDs blinking on b.0 and b.1, bothe at different speeds?

Main:
High b.0
Pause 1000
Low b.0
Pause 1000
Goto main

Sec:
High b.1
Pause 500
Low b.1
Pause 500
Goto sec


How can I have bothe of these short programs running simultaneously?
 

Paix

Senior Member
Alternatively you could choose to do it easily in one program:
Written for 08M2 which has port C only, but the ports can be
changed as you wish, along with everything else. :) An early
introduction to a state machine.
Code:
b0 = 0
Main:
do
  b1 = b0 % 10 ' first and every tenth loop
  b2 = b0 % 6  ' first and every sixth loop

  if b1 = 0 then
    toggle C.1 ' change state
  endif

  if b2 = 0 then
    toggle C.2 ' change state
  endif

  b0 = b0 + 1  ' this will roll over from 254 to 0
  pause 50    ' shorten this number to change the overall rate
               ' change the modulus above to change the ratio
loop
 

g6ejd

Senior Member
It will still be a sequential programme, but pseudo parallel tasking with a short interval between the two tasks :)
 

tesladude

New Member
The start0 and start1 works perfectly thanks all of you!

Loke I said, I am pretty new and am not understanding the rf in and out comand. I had two chips programmed one to recieve b0-7 and turn on an led , then I had one programed to send b0-7 and it worked perfectly.
But then just to test I changed one of them to b7-0 and it still worked.

How can I have two different signals to controll two different things, such as leds.
 

premelec

Senior Member
I'm not sure just what you want but usually you would send an identifier with the code as to where it's to be used... some unique code that indicates you want it to be used in a particular way. So you take it in in b0-b7 and use the b0 to indicate what the other bytes are for for instance... then you transfer them and take in the next bunch... so IF b0 = 123 THEN GOSUB A ELSIF b0 = 321 GOSUB B sort of thing...
 

tesladude

New Member
Why did changing the b#s still work though?
So wait are the b0-7 additive or somthing?
I dont understand what the bs even are
 

premelec

Senior Member
b0...b7 are distinct 1 byte variables - It doesn't matter what order you write to them. In regard to Manchester coding RFIN 8 bytes you can even load b0 b0 ...b0 all the same - overwriting what ever came before into b0 - if you want. I am not sure what your confusion is... What you are trying to do is what will matter... in short you need to know what a variable is being used for and what it indicates - by what you have done in giving it a value. Perhaps you can state your question differently if this doesn't make it clear...

Second thoughts - I'm not sure if you _didn't_ send b0 b0...b0 for your 8 byte Manchester send that what I've said above would work - it depends on where the check sums are done... perhaps someone else can clarify....
 
Last edited:

tesladude

New Member
I think it is starting to click for me. I wasnt thinking of them as litterally variables.
So does this translate to the variables that you can use like:
Main: let b0=1
If pinb.1=1 then
Inc b0
Endif

Those variables?
If this is what the b0-7 are then when recieved by a chip what happens to them? What is the effect of putting your b0-7 the way you put them?
 

Rick100

Senior Member
The variables like b0 or b1 are locations in the picaxe ram that hold values. What is received by the picaxe are the values. As an example the rfin command gets the values from a radio receiver and stores those values in the variables (ram locations) you specify.
 

tesladude

New Member
How can I translate this to different controlls, such as haveing two picaxe chips on a board with a single common data line so then I can send data with one to turn on my choice of an led on the other, my goal is get the basics of making a logical communication link
 

lbenson

Senior Member
The M2 chips have 2 characters worth of background receive buffer, so you can send one or two characters at any time, the receiver can pick them up when it is convenient.

So ... sender:

serout somePin, N2400_4, (b4) ' note: set baudrate to what you want, with appropriate program speed

Receiver:

Code:
hsersetup B2400_4, %00001100 ' no hserout, inverted input signal
main:
  do
   ' [do whatever you want]
    b4 = $ff  ' set to some value you won't receive
    hserin b4
    if b4 <> $ff then ' we received a character
      ' do stuff
    endif
    ' do other stuff
  loop
For safety's sake, put a 1K resistor between the output pin on one picaxe and the input pin on the other.
 

tesladude

New Member
Like I said I am truelly a beginner, the modt i've done was program a small car to drive around and when it gets neer somthing turn around. So could you kinda run me through it fromthe beginning? Im here not only to know but to learn.
 

lbenson

Senior Member
The sender sends a character (which can be interpreted by the receiver as a command): serout somePin, N2400_4, (b4)

Most interactive programs (like the receiver) are in the form of a big loop which runs forever, doing stuff, or not, with each cycle of the loop and looking for input in one form or another to tell it to do other stuff.

The "bones" of the program I posted make a typical "do forever" loop:
Code:
main: 
  do
  loop
Within the "do ... loop", you put your basic commands to do what you want the program to do. This can include checking input pins for on/off and lighting LEDs, and can also include checking to see if a command character has been received.

The "hserin" command for the M2 chips retreives a character from the hardware serial input buffer, and puts it somewhere that you designate, like into the variable b4. There may be nothing in the input buffer, in which case the previous value in b4 is unchanged. Therefor, to detect whether you have actually gotten a character from the buffer or not, you start by putting an invalid character in b4: "b4 = $ff". "$ff" is the representation, in hexadecimal, of the highest value a byte can hold (b4 is a byte-sized register)--255 (so you could also say "b4=255"). The value 255 does not represent an ascii character, like "0"-"9" or "A"-"Z", so it would not typically be a character which would be sent as a command.

If after you perform the "hserin b4" command, the value of b4 is still 255, then there was no character in the input buffer (no command had been sent by the other picaxe), so you continue processing your "do ... forever" loop. If the value is different, then you do other processing (like turning on/off an LED) based on the value of the command you received.

For instance, the first picaxe sending a "1" could mean you want to turn on an LED, and sending a "0" could mean you want to turn it off. Sending "A" could mean you want to position a servo at a specific spot, and "B" could mean a different spot (and so on). "Z" could mean you want to turn off all outputs and await further commands.
 

premelec

Senior Member
Yes... and for instance putting a received number in b0 would give you 8 bits of 0 & 1 information bit0...bit7 - which you could use to turn on/off LEDs etc.
I hope you have downloaded and read those fascinating manuals #1 & #2 - so many possible commands and situations!
 

lbenson

Senior Member
>So the reciever recieves a value and sets it to a specific variable?

A more precise way to say this might be: the receiver receives a value and sets a specific variable to have that value. (You determine which specific variable that it, for instance, b4.)
 

westaust55

Moderator
So can I not have one transmit signals that the other recieves and interprets? Like a remote controll car?
Yes you can.
The receiver on receipt of a value (with SERIN command) stores the value in a variable.
Then you can test the value with the IF THEN statements and when a test is true/valid perform the set task/action.
 

inglewoodpete

Senior Member
Have you read the Command manual's description of hSerSetup? Someone could describe it all again here but the command description is pretty good.
 
Top