# newbie trying to shift some bits

Hello, I am trying to send serial data to the arduino and I am wanting to send large whole numbers in the fastest possible way.

I’m using some python and pyserial as my data input for now. Essentially, here is what I would like to be able to type as the python input command:

``````motor.move(2543)
``````

I have started to look at bit sifting as a way to reconstruct things to 2 or more bytes. Example:
`value = (userInput[0] << 8) + userInput[1];`

Wondering if anyone has a working example of some Arduino code for this sort of thing?

Also when I run:

``````Serial.println(1,BIN);
``````

I get 110001

I thought a byte was 8 numbers. This is only 6? It seems to leave out the zeros at the start if it’s not needed. Some ascii tables such as this one http://www.neurophys.wisc.edu/comp/docs/ascii/ list the char 1 as Binary 00110001?

Is this just a serial.print abbreviation or is it actually discarding the first zeros?

Not even sure if this is the right approach for what I am doing? Any suggestions would be most helpful.

Dan.

`value = (userInput[0] << 8) + userInput[1];`

That seems to be correct. You could use the WordCast

``````Serial.println(1,BIN);
``````

I get 110001

Is this just a serial.print abbreviation or is it actually discarding the first zeros?

It simply does not print the leading zeros.

Thanks for clarifying AlphaBeta. So… Before I try to understand word cast…

I feel I need to be clearer about my problem. Lets assume I know very little about all this.

This is my final goal:
I want to be able to write an arbitrary number to serial using pySerial that can be read 1 for 1 in the Arduino.

I have managed to get this to work with this kind of python command:
`ser.write(chr(255))`
Apparently this (chr) wraps any decimal value from 0-255, and you get back its ASCII equivalent. Example (chr65) you get back ‘A’ which the Arduino sees as 65 in Decimal value when you do Serial.read()

This is all great but it only lets me go so high. What is the best approach to take a very long value say, 12345678 over serial to the Arduino so I can use the exact same value in the Aduino code.

Sorry for being so obvious. But I feel I must to cover all bases.

I’m sure this is a very common task. But I am struggling to find any entry level info on this. So far I’ve found these links:
Principialabs.com I found this really usefull if not going over 255
Antipasto Hardware Blog: Passing large values between the Arduino to the TouchShield This kinda made sense but was not fully explaining the syntax.
http://www.arduino.cc/playground/Code/BitMath Interesting but don’t know if this applies to my problem.

I know there are things like firmata and simple message system. But I would really like to learn the leanest approach to this.

Even if that means sending raw binary and reconstructing it(I don’t know how). Can somebody please help me? Suggestions, Links, keywords even! All welcome

Thanks.

Serial.println(1,BIN); should print 110001, while Serial.println(1,BIN); should print 1.

When you say “very large values”, how large is the largest value? Are they always whole numbers? Positive? Up to fifteen or fifty digits?

It's for driving stepper motors and they could be gear reduced by a up to 60:1. So what if I said, 7 or 8 digits?...

The Serial.print command has some cool ways of translating the data into different data types for display, but not sure how this can help me get over 255 hump?

If you send the number as a string then you can use the atoi() function to convert the string to an integer.

I can't remember if there is a size limit to the integer you can get this way.

atoi() is a standard c function that is not documented in the Arduino reference. Google it to see the syntax.

atoi() will add quite some bytes to the size of your sketch though.

If you send the number as a string then you can use the atoi() function to convert the string to an integer.

I can't remember if there is a size limit to the integer you can get this way.

atoi() is a standard c function that is not documented in the Arduino reference. Google it to see the syntax.

atoi() will add quite some bytes to the size of your sketch though.

Thanks for this but I'm not sure why I would need to convert a string to and integer in this instance since I am not sending a string(that i am aware of) from pyserial. I am sending ASCII(which is represented in characters) but it seems that when it is read into the arduino it comes in as the same number I sent.

Example: pySerial command in a python shell for sending

``````ser.write(chr(255))
``````

Arduino command for receiving Data

``````startbyte = Serial.read();
``````

The startbyte variable in the Arduino code, behaves as though it's holding the value I sent from python (255). So the relationship is 1 for 1 (or in this case 255 for 255).

This is good and what I want to happen. Only sending the values as ASCII means I can only send a number no higher than 255.

This is the limit for ASCII which is also the maximum number you can reconstruct from one byte(apparently).

Here is where it all gets a bit blurry for me... Say I wanted to send a value from python which is 2555. I'm sure there are all sorts of ways to do this with the current ASCII encoded approach.

Like sending the ASCII Character for 2 then 5 then 5 then 5. Or, send 25 then 55. Then rebuild these numbers back to the whole number in the Arduino code. But this feels like a cumbersome approach correct?

I'm thinking that there must be some way to get the whole binary number for 2555, then maybe split that into the minimum amount of bytes needed to send it over serial (one byte at a time) then rebuilt the bytes back into one whole binary number, then convert that into the original 2555 number that was sent from pySerial.

Can anyone tell me if I'm on the right track? Sorry for rambling, I am just thinking out aloud. :-/

a group of ascii characters IS a string. (more or less)

Edit: The problem is that Arduino really just recieves one byte at a time, and has no idea what that byte is a part of.

Well the weekend is here. So I will try out some of those suggestions and let you know how it gos. Thanks everyone.

Well it took a couple of weekends actually. But I have learned a lot in the process.

Here's a link to the video. http://www.vimeo.com/4101788

I didn't need to use atoi() in the end, but I'm sure it will come in handy so thanks. I will post the code when I get time to clean it up.

Thanks again for the help,

Dan.