Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

enter image description hereUsing LabVIEW I am sending string of data through Xbee like,

  • u123
  • r64
  • r89
  • u34
  • r179

And my arduino is receiving this data. I want my arduino to read this data as a string and I want to extract numbers from these strings and feed it into two different servos. For example from u123 I want to extract 123 and feed it to servo1 and from r64 I want to extract 64 and feed it to servo2 and so on.

Arduino IDE Serial Monitor is showing nothing but Arduino's Xbee is receiving and sending data. Don't know why :(

share|improve this question
    
It's not clear what you're asking. Is "Arduino IDE Serial Monitor is showing nothing..." an additional question or did my answer not address your question? What are you expecting to happen? What did you try to accomplish that? What happened instead? – JRobert Oct 30 '15 at 19:48

First collect all of the characters up to the new-line (`\n' or line-feed) into an array of characters, and put a zero byte after the last one.

Since It looks like all of the strings you need to parse consist of one a single letter followed by the value you want, value = atoi(inputArray[1]; will return the value as a 16-bit int. If it could be larger than 32767 but less than 65536 (probably not if you're driving a servo with it), then cast the value to unsigned.

If there might be an unknown number of characters (non-digits) preceeding the value, the C function size_t strcspn(const char *string, const char *cset) will return the length of the leading sub-string of string that does not contain characters in cset. So value = atoi(inputArray[strcspn(inputArray, "0123456789")]); will do the same thing, but calculating the array index of the first digit in the string.

(For completeness, note there is a companion function, size_t strspn(const char *str, const char *cset);, that returns the leading sub-string of 'string' consisting only of characters in cset).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.