Skip to content

Instantly share code, notes, and snippets.

@mattfelsen
Last active February 2, 2023 22:28
Show Gist options
  • Save mattfelsen/9467420 to your computer and use it in GitHub Desktop.
Save mattfelsen/9467420 to your computer and use it in GitHub Desktop.

Revisions

  1. mattfelsen revised this gist Apr 5, 2014. 1 changed file with 56 additions and 16 deletions.
    72 changes: 56 additions & 16 deletions gistfile1.ino
    Original file line number Diff line number Diff line change
    @@ -19,32 +19,72 @@ for (int i = 0; i < input.length(); i++) {
    //

    // Define number of pieces
    int numberOfPieces = 4;
    const int numberOfPieces = 4;
    String pieces[numberOfPieces];

    // This will be the buffered string from Serial.read()
    // up until you hit a \n
    String input = "123,456,789,0";
    // 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;

    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++;
    }
    void setup(){
    Serial.begin(9600);
    }
    // Check if the last separater occurred before the end of the string
    // If so, get that last bit of the string
    if (input.length != lastIndex) {
    pieces[counter] = input.substring(lastIndex, i);

    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();

    }
  2. mattfelsen revised this gist Apr 4, 2014. 1 changed file with 40 additions and 0 deletions.
    40 changes: 40 additions & 0 deletions gistfile1.ino
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    //
    // This is tested and works!
    //
    String input = "123,456";
    int firstVal, secondVal;

    @@ -7,4 +10,41 @@ for (int i = 0; i < input.length(); i++) {
    secondVal = input.substring(i+1).toInt();
    break;
    }
    }


    //
    // I tested a similar version in JS and it worked
    // No guarantees!!
    //

    // Define number of pieces
    int numberOfPieces = 4;
    String pieces[numberOfPieces];

    // This will be the buffered string from Serial.read()
    // up until you hit a \n
    String input = "123,456,789,0";

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

    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++;
    }
    }
    // Check if the last separater occurred before the end of the string
    // If so, get that last bit of the string
    if (input.length != lastIndex) {
    pieces[counter] = input.substring(lastIndex, i);
    }
  3. mattfelsen created this gist Mar 10, 2014.
    10 changes: 10 additions & 0 deletions gistfile1.ino
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    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;
    }
    }