Loading...
Pages: [1]   Go Down
Author Topic: Datatypes and Serial.write  (Read 278 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
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

Code:
//  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
Logged

Leeds, UK
Offline Offline
God Member
*****
Karma: 35
Posts: 983
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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:
Code:
Serial.print("600 601 602\n");
)
« Last Edit: July 15, 2012, 06:22:39 pm by TCWORLD » Logged

~Tom~

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Todd
Logged

Pages: [1]   Go Up
Print
 
Jump to: