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