Go Down

Topic: Parsing serial string into variables (Read 10361 times) previous topic - next topic


I have a simple robot project I am trying t do.
The section I am stuck on is getting the drive control data from the remote into the robot controller.

The remote is a "Nunchuck". I read it's joystick values and 2 of it's button states via I2C with a Arduino Pro Micro. I then build that data into a string and send all of it over an HC-05 Bluetooth pair. That piece I have working well. The data string is looks like this:


The first value is X joystick, second is Y joystick and the last two are the button states and can be 0 or 1.

I send these with a Serial1.println(nunchuckValues);

My question is, how can I parse these values out of the received string on th robot controller (Samd21).
I know how to convert the individual "substrings" into integers/bytes etc. once I have them broken out. I just am having a heck of a time finding a simple way to split this string on the delimiter of ",". I have done this multiple times in Python, but as we know, Arduino isn't running python. :(


strtok() (and strchr()) can be used to find a delimiter in a nul terminated character array (c-string); see the updated Serial Input Basics thread for an example.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.


May 21, 2018, 06:55 am Last Edit: May 21, 2018, 06:56 am by horace
a simple example using strtok() to parse tokens
Code: [Select]
// strtok example - parse tokens

void setup() {
  // put your setup code here, to run once:
    char str[] ="RL3]\[Test 1}|{3}|{S]\[Test 2}|{41}|{L]\[Test 3}|{1234}|{L";    // text to tokenise
    char * pch;                     // pointer to tokens
    Serial.print("Splitting string ");
    Serial.println(" into tokens:");
    pch = strtok (str,"}]\[|{");         // get first token
    while (pch != NULL)
      int x=999;
      Serial.print("string found ");
      Serial.println(pch);          // print it
      pch = strtok (NULL, "}]\[|{");     // get next token

void loop() {}

Code: [Select]
Splitting string RL3][Test 1}|{3}|{S][Test 2}|{41}|{L][Test 3}|{1234}|{L into tokens:
string found RL3
string found Test 1
string found 3
string found S
string found Test 2
string found 41
string found L
string found Test 3
string found 1234
string found L


May 21, 2018, 10:00 am Last Edit: May 21, 2018, 10:08 am by outsider
Not the best way, but if you're only entering numbers, try this. In the top of serial monitor, enter 127,128,0,1[ENTER].
Code: [Select]
void setup()
  Serial.begin(9600); // set line end to Newline at
                      // bottom of serial monitor

void loop()
  int x, y, bs1, bs2;
  x = y = bs1 = bs2 = 0;
  while(Serial.available() > 0)
    x = Serial.parseInt();
    y = Serial.parseInt();
    bs1= Serial.parseInt();
    bs2 = Serial.parseInt();
    char r = Serial.read();
    if(r == '\n'){}
    Serial.print("x =  ");
    Serial.print("y =  ");
    Serial.print("bs1 =  ");
    Serial.print("bs2 =  ");

Go Up