Last active
February 2, 2023 22:28
-
-
Save mattfelsen/9467420 to your computer and use it in GitHub Desktop.
Splitting strings by a delimiter for Arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// This is tested and works! | |
// | |
String input = "123,456"; | |
int firstVal, secondVal; | |
for (int i = 0; i < input.length(); i++) { | |
if (input.substring(i, i+1) == ",") { | |
firstVal = input.substring(0, i).toInt(); | |
secondVal = input.substring(i+1).toInt(); | |
break; | |
} | |
} | |
// | |
// I tested a similar version in JS and it worked | |
// No guarantees!! | |
// | |
// Define number of pieces | |
const int numberOfPieces = 4; | |
String pieces[numberOfPieces]; | |
// This will be the buffered string from Serial.read() | |
// up until you hit a \n | |
// Should look something like "123,456,789,0" | |
String input = ""; | |
// Keep track of current position in array | |
int counter = 0; | |
// Keep track of the last comma so we know where to start the substring | |
int lastIndex = 0; | |
void setup(){ | |
Serial.begin(9600); | |
} | |
void loop() { | |
// Check for data coming in from serial | |
if (Serial.available() > 0) { | |
// Read the first byte and store it as a char | |
char ch = Serial.read(); | |
// Do all the processing here since this is the end of a line | |
if (ch == '\n') { | |
for (int i = 0; i < input.length(); i++) { | |
// Loop through each character and check if it's a comma | |
if (input.substring(i, i+1) == ",") { | |
// Grab the piece from the last index up to the current position and store it | |
pieces[counter] = input.substring(lastIndex, i); | |
// Update the last position and add 1, so it starts from the next character | |
lastIndex = i + 1; | |
// Increase the position in the array that we store into | |
counter++; | |
} | |
// If we're at the end of the string (no more commas to stop us) | |
if (i == input.length() - 1) { | |
// Grab the last part of the string from the lastIndex to the end | |
pieces[counter] = input.substring(lastIndex, i); | |
} | |
} | |
// Clear out string and counters to get ready for the next incoming string | |
input = ""; | |
counter = 0; | |
lastIndex = 0; | |
} | |
else { | |
//if we havent reached a newline character yet, add the current character to the string | |
input += ch; | |
} | |
} | |
// Data is now available in pieces array | |
// pieces[0] is first item | |
// pieces[1] is second item, and so on | |
// You can call toInt() on the data to convert it to an int | |
// ex. int value = pieces[0].toInt(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The new SafeString Arduino library, available from the Arduino Library Manager and from
https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html
provides a number of tokenizing methods and accurate string to number conversions.
SafeStrings are easy to debug. SafeStrings provide detailed error messages, including the name of SafeString in question, to help you get your program running correctly.
SafeStrings are safe and robust. SafeStrings never cause reboots and are always in a valid usable state, even if your code passes null pointers or '\0' arguments or exceeds the available capacity of the SafeString.
SafeString programs run forever. SafeStrings completely avoid memory fragmentation which will eventually cause your program to fail and never makes extra copies when passed as arguments.
SafeStrings are faster. SafeStrings do not create multiple copies or short lived objects nor do they do unnecessary copying of the data.
There are a number of examples included with the library. Here is the trivial one for parsing user input to extract keywords and ignore the rest without blocking the rest of the sketch from running.
The SafeString string to number conversion methods fix a number of problems. The usual Arduino conversions will returns 5 for the string "5a" while SafeString returns false since 5a is not a valid number. See the included library example sketch.