Datatypes and Serial.write

Hello all - I'm quite new to Arduino programming and am having issues with datatypes, concatenating values and Serial.write. My project will read the values from a joystick and send the values via an xbee to a robot, receiving it via another xbee. My goal is to read the values of the joystick, concatenate the 3 values into a string, and send that value to my robot.

When I send a test string to the robot such as:

Serial.print("600 601 602");
Serial.write("\n");

The receiving code is able to successfully extract the 3 numbers.

My issue is how to use the proper data types and concatenation to read the joystick values and send a similarly formatted value to "Serial.print". The following code seems to convert the numbers into their ascii values

//  Serial.print(analogRead(0)); //Read the position of the joysticks X axis and print it on the serial port
//  Serial.print(" ");
//  Serial.print(analogRead(1)); //Read the position of the joysticks Y axis and print it on the serial port
//  Serial.print(" ");
//  Serial.print(digitalRead(8)); //Read the position of the select button and print it on the serial port
//  Serial.print("\n");

So my question, poorly worded, is how do I read the 3 values and send it in the same format as my testing sample above?

Thanks for any help or direction,

Todd

This should do. It reads the analog pins and then compiles the integer values into a string as you had before.

char buffer[16] = {0}
int x = analogRead(0);
int y = analogRead(1);
int select = analogRead(8);
sprintf(buffer,"%d %d %d\n",x,y,select );
Serial.print(buffer);

(Oh, and just to mention, for your original code, this is a shorter equivalent:

Serial.print("600 601 602\n");

)

Excellent! Thanks for your help. It makes perfect sense now that I look at it :slight_smile:

Todd