parsing string

In case you get readings like “33, 58, 92″ , and need to parse it into individual numbers, here is some psudeocode.

//———————————————-
// declare variable names at the beginning of the code
int a, b, c;

// read and parse string in void loop()
//%d stands for decimal integer, and correspond to the type of the pointer that follows
scanf (Serial.read(0),”%d,%d,%d”,&a, &b, &c);

//do whatever you want with the numbers
Serial.print (a); // will print 33
//———————————————-

Note that if your reading is bigger than integer, the number will be inaccurate . You’ll need to use unsigned integer, or long integer. You might also be reading float and charecters. For the corresponding format specifier, refer to http://www.cplusplus.com/reference/clibrary/cstdio/scanf/

Advertisements

~ by cheng on April 23, 2010.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: