Parsing serial string into variables

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:

127,128,0,0

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. :frowning:

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.

a simple example using strtok() to parse tokens

// strtok example - parse tokens

void setup() {
  // put your setup code here, to run once:
    Serial.begin(115200);
    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.print(str);
    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() {}

gives

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

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].

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.println(x);
    Serial.print("y =  ");
    Serial.println(y);
    Serial.print("bs1 =  ");
    Serial.println(bs1);
    Serial.print("bs2 =  ");
    Serial.println(bs2);
  }
}