Reading two characters from Serial


I am building a two way tiltable table for something like a webcam. I have made it to work with two potentiometers, and it works fine. See this Youtube video for the current status.

There is an armaturePositionMatrix with values 650, 1500, 2350 to be sent to the servos as microseconds, and that works well.

I then built a Python interface with buttons eight ways, as well as center both axis, and am now trying to send data from Python to Arduino, so that 00 is top left, 01 top, 02 top right, 10 left etc.

I got it to workl with one character sent to the Arduino, ie. 0 for left, 1 for center and 2 for right. So far so good.

I then tried to declare a buffer as char *buffer, then read data into it this:

if (Serial.available() >= 2) {
for (i = 0; i < 2; i++) {
buffer [ i ] =; (the i of course hasn’t got the spaces IRL)

since I know the button will always send two characters, one for horizontal attitude and one for vertical.

But using this as

i = 0;
receivedCommand = buffer[ i ];
switch (receivedCommand)
case ‘0’ :

etc… doesn’t work.

So my question is: how to read the two characters and separate them for horizontal and vertical values?

Many thanks!

With a standard Arduino board the hardware UART is connected via USB to the PC. With you are reading inputs from the Serial Monitor. But you want to read your button. How is it connected to the controller pins?

use code tags if you don't want the funny italic font when typing "[ i ] without the space" and for readability by all...

what do you mean by tried to declare a buffer as char *buffer do you mean a char (no star) buffer? likechar buffer[2]; // and not char * buffer[2]

can you post all your code? how do you deal with the fact that probably most of the time the loop does not have anything waiting for you so just zoom past the if (serial.available() >= 2) statement?

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

This Python - Arduino demo may also be useful.


etc... doesn't work.

That is the lamest phrase used on the forum. You read something from the serial port, but not bother to echo it, to see what you read. Why not? How can you hope to use data that you have read, when you don't KNOW what you read?

Posting ALL of your code helps, too, so we can see what all the data types are.

Thanks for the link to the Serial instructions. I am sure I can manage with the information in that post.

Sorry to have irritated some of the respondents. I will phrase my question better next time.

usenetfan: Sorry to have irritated some of the respondents. I will phrase my question better next time.

Take a look at the number of posts Paul has made. Probably 50% of those posts more or less tell the poster almost the same thing. That it, newcomers here tend to make the same mistakes, yet Paul takes it upon himself to answer that same question...again...for the 20,001th time. He's really not irritated. Trust me, if he was really irritated, he wouldn't have answered at all. It's just that all of us get a little bummed out saying the same thing over and over.

Stick around and you'll get the hang of what's expected in a clearly-stated question and the value of posting all of your code. It's worth the effort cuz there are some really smart people here.

if (Serial.available() >= 2) {

is unreliable way to check Serial buffer.


while(!Serial.available()); wait for data or use if(() ,,,,

if(!Serial.available()) skip buffer processing if non blocking is desired { while(Serial.available()) check buffer until empty { ... ..... buffer [i++ ] =; collect data from buffer .... }

if (Serial.available() >= 2) {

is unreliable way to check Serial buffer.

No, it isn't. If there are 2 or more characters, process them. Otherwise, go do something else. That's a perfectly reasonable thing to do.

and ...

while(Serial.available())           check  buffer until  empty
..... buffer [i++ ] =; collect data from buffer

is not sufficient.

Look at Serial Input Basics