Controlling Picaxe microcontrollers with a computer or let them communicate

On general demand (ok, Fritsl asked me...), I will show and explain you how to let several Picaxe microcontrollers communicate with each other or how to control a picaxe microcontroller with your computer.

This is actually fairly easy with the serrxd and sertxd commands. Serrxd reads the serial in port and sertxd writes to the serial out port. Now, when you check the Picaxe manual for these commands you will see this:

Syntax:
SERRXD (qualifier,qualifier...)
SERRXD (qualifier,qualifier...),{#}variable,{#}variable...
SERRXD {#}variable,{#}variable...

What this means is:

  • Qualifier: when this is used, the Picaxe will wait until it reads the value that has been entered as qualifier and then continue with its operation
  • Variable: b0... you know these ;)


Little example: serrxd ("LMR"),b1 will wait until it reads the letters LMR and then put the first byte after thos three letters in b1.

For sertxd you find:

Syntax:
SERTXD ({#}data,{#}data...)

This means:

  • Data: this can be anything... Sentences (always place text that needs te be read as text by the receiver between ""), variables (b0, w1 and so on) or special orders (more on that later)

 

Now, to control the outputs on a Picaxe 28X1 I used this code:

 

main:
serrxd("dev0"),b0,b1 'Read serial in port and wait for string dev0 then put next two bytes in b0 and b1
let b2=b1-48
if b0 = 49 then
high b2
sertxd("Light ",b1," switched on. ") 'Send a message to serial out port
else if b0 = 48 then
low b2
sertxd("Light ",b1," switched off. ")
endif
sertxd("Waiting for command...",13,10)
goto main

 

(I used this codes to turn on LEDs that's why you see "light switched on/off")

Now, I think some explanation wouldn't be bad...

  • serrxd("dev0"),b0,b1 ==> Reads the serial in port. The qualifier "dev0" makes sure the controller waits until dev0 is read on the serial in port. After it has read the qualifier the command will put the next two bytes the controller receives in variables b0 and b1.
  • sertxd("Light ",b1," switched on. ") ==> Sends 3 messages to the serial out port, when received by the terminal in the programming editor on the computer these 3 messages will be displayed right next to each other and form a sentence
  • sertxd("Waiting for command...",13,10) ==> Sends the message Waiting for command... to the serial out port. 13 and 10 are special commands and will be explained later on.

This program will do the following: when I send dev011 the controller will enable output one, when I send dev001 the controller will disable output one. This can be doen for outputs 0 till 7 by altering the last number in the code that is being sent.

 

Now, before you start programming you controller, read this paragraph very carefully! The serrxd and sertxd don't work with numbers but with ASCII values... This means that when dev012 is sent to the controller running the program above, the controller will put value 49 in b0 and value 50 in b1. This is because the character 1 is value 49 in the ASCII table. This also means that it is possible to send special function: if you look at ASCII value 13 you will see that it corresponds to the character 'carriage return', this makes sure the next characters will be displayed at the beginning of the line. ASCII value 10 corresponds to 'line feed' and places the cursor on a new line. So if you follow the logic, executing the code sertxd(13,10) will start a new line at the beginning (or do the same as if you would hit 'return' when typing in a text editor).

This also implies that you can't use high b1 straight away in the program above, because that would try to enable output 48 or higher (character zero corresponds to ASCII value 48, char. 7 to 55). That's why I've put in a line of code that subtracts 48 out of b1 and puts the result in b2, that way I can use the variable b2 to enable or disable outputs and variable b1 to give feedback to the computer.

 

Now, if you apply the same logic to two Picaxe microcontrollers instead of a Picaxe and a PC, you can let the microcontrollers communicate with each other.

The Picaxe microcontrollers also have the commands serin and serout, these do exactly the same as serrxd and sertxd but let you decide what pin to use.

Any questions will be answered with pleasure.

https://www.youtube.com/watch?v=vWN4hW7eQrM

It should be noted that the

It should be noted that the serrxd/txd function does not work for all of the picaxes while the serin/serout do. You also are limited to a set baud rate where serin/serout can be defined by the user. Last point is that the you can use a timeout feature on certian higher end picaxes which will prevent your axe from sitting and waiting indefinately.

Can you also clarify this point: The serrxd and sertxd don’t work with numbers but with ASCII values…

The manual seems to state otherwise as far as how to send raw vs ascii characters.

apologies if this is a noob

apologies if this is a noob question - but I assume then that you’re interacting with the picaxe via axepad? If not - what software are you using?

I’m looking for a way to let picaxe communicate with my MAC via wireless… yep, mac. (in my defense; it’s not my mac, and getting anything else in this country is too difficult and expensive!).

When you say wireless, are

When you say wireless, are you saying WIFI or any type including bluetooth.

If so, you should look at processing. It works pretty well with the bluetooth library.

I use processing with the BT lib to communicte with a picaxe at the moment…just experimenting, but I have been playing around with an echo terminal (send from comupter picaxe recieves and responds with the same char).

oh right. no I mean wifi.

oh right. no I mean wifi. simply because I have a fistful of tx/rx modules on hand.

Can I just clarify VDB: you’re interfacing linux with your picaxe? I wonder if I can replicate that on my mac?

I haven’t played with wifi

I haven’t played with wifi as of yet, just RF and bluetooth so wouldn’t be of much help in the way of connecting wifi… :confused:

I am running processing on windows. the db is running on a linux box. I have an ssh tunnel to the linux box that forwards the local(windows) db port to the remote(linux) port…it sounds more complicated than it is.

You can replicate it by just running processing on your mac. I have it set up on my mac as well but I got the bt lib running on the mac as well which is one less thing to worry about . I would just need to set up the port forwarding on the mac to connect to the linux box for db interaction. just a simple ssh command.

Ascii vs raw
Actually a better way to read numbers in is to use the optional # before your variable. In your example this will read a 0 into b1 rather than 48, avoiding the arithmatic. So your code would be

serrxd ("dev0"),bo,b1

This is the code I’ve used

serrxd (“dev0”),bo,b1 is the code I used to communicate with the picaxe. It works with ASCII value’s…

 

Now I’ve tried adding the #'s but then code doesn’t work anymore, so if you could clarify how this should be used, that would be great…

You’re absolutely right :slight_smile:
I know the manual says that you can add #'s to work with raw characters, but that doesn’t work with me… So if anyone can tell me how to work with the #'s that’d be great :slight_smile:

Sorry, my bad, I

Sorry, my bad, I meant

serrxd (“dev0”),#bo,#b1

 

Actually I just realised I’m not using that for input, only output. Here’s my code;

serout 4,T4800,(#w1,13)

I’m using this for reading commands;

 

serin 0,T4800,b1

debug

if b1 = “2” then

gosub goforward

elseif b1 = “8” then

gosub goback

elseif b1 = “4” then

gosub turnleft

elseif b1 = “6” then

gosub turnright

elseif b1 = “5” then

gosub stopnow

 

Works for me.

I was actually using the # for reading in values before and I remember I had to send the # to the Picaxe before the actual number for it to work, so for b1 to read 0 I’d have to send #0 .

I tried that too
I tried that too ( sending #0 to receive 0 in b1) but that doesn’t work either with me…

I can test this on a 08m,

I can test this on a 08m, 18x and 14m this eve. I don’t have a 28x though.

Maybe it’s an issue with the proc firmware?

help
hey plz help me i dont know any thing about robotics…

HI
For people like you, who don’t know anything about robotics there is a special post showing how to build your first robot… you can find it under “Start here”

Off topic, but

What’s wrong with a mac?

Anyways, WIFI is fairly difficult to master, what with all the protocols and everything. Instead, I would just use two micros, one connected to the MAC and one transmitting… And if you want cheap, try some of these- http://www.sparkfun.com/commerce/product_info.php?products_id=8945

 

 

I does not work with my 08M

I does not work with my 08M

Transforming duplex to half duplex and visa versa

M2GR I hope that this is appropriate for the site, it’s not exactly robotics. I’m trying to hook a bluetooth module up to a telescope mount (in place of a hand controller)  and think a Picaxe 08M2 (which I have on hand) might be able to do the transform I need but I’m still trying to get up to speed on using the Picaxes.

The Bluetooth module has TX and RX lines, as far as I can tell the telescope mount uses a single data line and a flow control line which is normally high and is pulled low when either side wants to send a message to the other.

I need to be able to listen on for messages coming from the RX line of the bluetooth module (which would be connected back to a computer or android tablet) and a message is received pull the flow control line low then pass the message to the data line. If the flow control line is pulled low by the telescope mount then the message being received from the data line would be sent on to the TX line of the Bluetooth module. Hope that makes sense.

I gather that an interupt on the flow control line would handle the data coming from the telescope mount (I assume that I’d just do some kind of passthrough routine, read a byte, write a bit) but I’m I’m not sure how to handle the pass through of data coming from the RX line of the bluetooth module. I read elsewhere that issuing a serin command leaves the picaxe unable to do anything else (and got the impression that included interupts). Do you know if that’s the case?

Any handy suggestions for a picaxe beginner. I get the impression that what I’m trying to do should be fairly simple but don’t know enough yet to have much confidence.

 

Bob

 

Not very clear

I’m having trouble with understanding what you’r trying to acheive… Are you trying to send back any data that arrives at your BT module?

Some more info

MG2R I’m trying to use the picaxe to passon any data that comes from the bluetooth module to into the data line going to the mount (taking the flow control line low while I do so) and when data comes from the mount (indicated by the mount pulling the flow control line low) I want to send that data to the bluetooth module.

I’ve had a go at the code but there is so much new stuff for me in this that I don’t know if that will make it much clearer. Commands coming via bluetooth should always start with a ‘:’ and end with a char 13. Length will be variable depending on what the command requires.

Normal responses from the mount will start with a ‘;’ and end with a char 13, abnormal responses will start with a ! and end with a char 13.

If this is something you don’t find interesting or is to far off line for the site then please fell free to ignore it, I’m not trying to be a pest.

From what I could tell I had to define the flow_control line differently for different commands, some commands want the C.2 syntax, others pin2 (hope I’ve got that right) then the convention is to use the %00000100 when setting an interupt.

’ First pass at a program to use a Picaxe 08M2 as a converter
’ between a Skywatcher Dob mount and a bluetooth module.

’ The dob appears to use a half duplex protocol with hardware flow control,
’ the bluetooth module uses seperate tx and rx lines
’ Bob Stephens 2012

symbol mount_data = C.1
symbol flow_control = C.2
symbol flow_status = pin2
symbol bt_rx = C.3
symbol bt_tx = C.4

main: setint 0,%00000100
      ’ Commands should start with a ‘:’ and end with a char 13
      serin bt_rx,T9600_8,s_w1 ‘ Receive a character into variable b1
      gosub talk_on_data
      goto main
     

talk_on_data:

    setint off

    do: pause 1: loop until flow_status = 1

    if flow_status = 1 then

        low flow_control

        pause 3

        serout mount_data, T9600_8,(s_w1)

        high flow_control
   endif


    setint 0,%00000100

return

interrupt:

    ’ Read incoming on mount data line when the line is pulled low
    ’ Normal response from the mount will start with a ‘;’. abnormal with a ‘!’
    ’ All commands and responses should end with a char 13

    if flow_status = 0 then
      serin mount_data,T9600_8,(s_w2)
      serout bt_tx, T9600_8, (s_w2)
    endif

return

Long time ago

First, let me start with saying that you’ll find that people on this site aren’t very strict on “only robotics”. General electronics, help with programming or actually just about anything that is remotely connected to technology is welcome, for as far as I can tell.

Now, it has been ages since I’ve last used a PicAxe (I use Arduino now), so I don’t know the language very well anymore. But I can work a little program in pseudo-code to indicate how such a program should be able to function. I could be mistaking, but I’ll try my best!

set interrupt to activate interrupt sequence when flow_controll is pulled low
actively listen to the bluetooth module for data
if BT-data is received then
   disable interrupt
   send BT-data to dob
   activate interrupt

interrupt sequence:
   read dob-data
   send dob-data to BT-TX

That should be about it. Although reading the data can be tricky since the data is of a variable length. I don’t know how you can work around this with a PicAxe since it always reads a fixed amount of data (defined by the variables you put in the serin-instruction).

You could try posting your problem in the forums, I’m sure that there will be far more capable people than me who are able to answer your question fully.

Good luck!