Variable help please :(

sr

New Member
Right hello everyone :D

Basically I want to read a three digit number using the serin command, i assume this is done like so:

Code:
serin 2,N4800,b0,b1,b2
I have managed to get it working with one character, but I cannot get the three numbers into one variable, as it is, if I were to send "123", "1" would be in variable b0, "2" would be in b1, etc. I have tried the following but it doesn't seen to work.

Code:
let b3 = b0 & b1 & b2
Any ideas how to get all three digits into one variable? :)
 

ylp88

Senior Member
Code:
let b3 = b0 & b1 & b2
Indeed this will not perform the concatenation as you may have been expecting (we can't deal with strings...).

To elaborate, the process involves converting the ASCII (number) characters into decimal numbers, compensating for their place values and then adding them all up and saving the result.

ylp88
 

moxhamj

New Member
Hi,

There are some simple solutions here. Are you sending numbers as binary numbers or as ascii numbers? If they are binary (eg 0 is binary 00000000) then you can do some simple maths eg w5=b0:w6=w5*100+b1*10+b2

But I suspect you would be sending the numbers as ascii characters. In which case have a quick look at the ascii character set. 0 is ascii 48, 1 is ascii 49 etc. So you need to subtract 48 to get the binary value, then do the multiplication to convert to a single number.

And just watch the binary values which cannot go over 255. So if b0 contains the "hundreds" you need to convert it to a word first before multiplying by 100.

Have a look at the bintoascii command. And maybe look at sending the number another way - eg w0=123, serout w0, and serin w0.

This is a bit complex I know. Can you tell us where the serin is coming from - another picaxe or a computer?
 
Last edited:

hippy

Technical Support
Staff member
w5=b0:w6=w5*100+b1*10+b2
You fell for believing 'operators have precedence' which is something hard to shake off in this particular case - I often catch myself doing that, this is a particular trap people do fall into, and it's hard to see the mistake even when staring one in the face. So by way of explanation for everyone rather than specific criticism ...

Because PICAXE expressions evaluate solely left to right that would need to be -

w6 = w5 * 10 + b1 * 10 + b2

The second "*10" applies to the first resulting in that being an effective "*100".

So it's really : ( ( w5 * 10 ) + b1 ) * 10 ==> ( w5 * 10 * 10 ) + ( b1 * 10 )

Also, no need to promote b0 ( byte ) to w5 (word ) as that will happen automatically. All maths evaluations are done as 16-bit ( bytes are zero extended, MSB set to zero ) and truncated if necessary when stored in a byte.
 

sr

New Member
So I can just accept the three digits at once by using a word variable in my serin command?

BTW the input is from my PC, its a webcam pan/tilt thing im making. The digits are the servo position.
 

hippy

Technical Support
Staff member
You can accept three digits at once using a word variable ( byte variable even if les sthan 256 ) using #var is SerIn, but you'll need to send a terminating character as well or the PICAXE doesn't know if the number has finished or there are more digits to come later.
 

sr

New Member
I am still working the program, thanks for the help guys, there is another thing I wanted to ask though.

Is there a way to have the picaxe continue a command and execute another part of the program simultaneously?

I have two servos, and currently I am adding a pause command after each servo command to allow the servos time to actually move. Is there a way to tell the servos to move, then immediately go back to the main program to listen for more serial data, while the servos are still moving into position?
 

hippy

Technical Support
Staff member
SERVO commands always execute in parallel with the main program. Don't add the pause and you can go back to your SERIN while the SERVO is on the move to its requested postion.

It will also be worthwhile taking a look at the SERVOPOS command if you're using serial to control servos.
 

sr

New Member
Okay I just tried it with this program

Code:
main:
servo 0,75
servo 0,150
servo 0,225
goto main
And the servo is not moving at all

but this works:

Code:
main:
servo 0,75
pause 1000
servo 0,150
pause 1000
servo 0,225
pause 1000
goto main
:(
 

hippy

Technical Support
Staff member
If you send consecutive SERVO commands nothing much is likely to happen, the servo doesn't have time to do any movement before being told to go to somewhere else, plus issuing a SERVO command resets the timing so nothing gets through. This is why I suggested looking at the SERVOPOS command.

A servo will move to where it should be while your program does something else ( be that a PAUSE or some other commands ) but you have to let it get there before telling it to go somewhere else. You also have to be wary of just entering SERIN as I believe that will also stop servo movement. There are discussions about this which can be found using Forum Search.
 
Top