help on a complex text numeric string and how to program the ardunio to parse.

i am wanting to control steppers and lights and switches and encoders on a mega. i want to input a text/number string from serial to determine motion. the text format is "g1234x1234…8y1234…8z1234…8f1234. the gxyzf are the delimiters and the location to the correct if statement. the numbers are from current position the number of step cycles the axis stays on.

i have burnt up the internet and downloaded books to no profit on how to program this in a ardunio. I have not found a way to get the ardunio to recognize a variable text delimiter to 1.) parse text + (cutting text off number) number into separate registers and 2.) use text to point the number to a specific place in this prg. each loop if x <> 0 and xn < 0 then xn = xn - 1 untill xn=0 , x is delimiter and xn is the number.

i am undecided on using a defined buffer i am looking right now to input line at a time and parsing that line into statements to be processed.

yes i can download grbl or marlin and Im not a c programmer and apparently have a profound misunderstanding somewhere. Where can i find the help info i am looking for. The fun of his is the actual learning and step by step making this work. from the documentation i have found the text string handling is scanty.

Take a look at the strtok and atoi functions.

You can probably simplify the problem by just sending a series of numbers to the Arduino. If the numbers always appear in the same order you don't need the identifying letters. And if you have a separator between the numbers (a comma is usual) it will make it easy to split the whole into its parts.

Assuming the Arduino has received the characters (say 123,4567,89,23) and stored them in a character array all you need to do is loop through the array looking for the comma character to mark the end of each number.

If you are not a complete beginner and if you take a bit more trouble to get your PC to send integers (assuming the max number of steps for any movement is +/- 32767) then for (say) 4 axes you need to send 8 bytes (2 per integer) and the Arduino doesn't have to do any conversion.


You need to structure the way the data is sent to the arduino make parsing the data easier. Below is some servo code that is set up for easy parsing. A comma is used to delimit the end of a data packet. A servo identifying character is used prior to the comma delimiter to identify which servo the data applies to, and the leading characters represent the numeric control value for the servo to be commanded to.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c =;  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
          Serial.print("writing Microseconds: ");
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
          Serial.print("writing Angle: ");
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
         readString=""; //clears variable for new input
    else {     
      readString += c; //makes the string readString