Simple G-code to XY table

I have an XY table i wont to send command to over my Serial connection. For an example I wont to be able to write "X100" and "Y300" and then on the command "run", make them go to that position 100,300 . I am farely ok using the AccelStepper.H library (great library!), so what i need help to is how to store the serial data, and how to split them up into an axis and a int value.

So far i have gotten this code to read and store the serial data in string form (maybe going down the wrong path here!):

String serDataStr;

void setup() {
  Serial.begin(9600);
  Serial.println("Serial conection started, waiting for instructions...");
}

void loop() {

  while(Serial.available()) { 
    char recievedChar = Serial.read();
    serDataStr += recievedChar;  
    delay(5);
  }

  if (serDataStr != "") {
    Serial.print(serDataStr);
  } 
}

But how do i convert string to int? I would like to be able to use comperator, say asking on the first space in the array:

if(serDataStr[0] == X){
then take the rest of the digits and make that the x position

}

I have tried a couple of different thing for the last couple of hours, and it dosen't seem to work for me, so if anyone have any idea they are welcome :slight_smile:

PS. I am using a chipkit and the serial comes from the Mpide serial monitor (not that i think this matters!).

Man page references Linux but it's a stdlib function -- available on any platform.

You could easily send the data to the Arduino as integers (or longs) and then you don't have to bother with parsing them on the Arduino. You don't need X or Y either - the order of the numbers will be enough.

...R

I am going to use this as a test code, so i can move my XY table “manually”, by using simple commands typed directly in my serial prompt. How would i then pass them on as integers?

Anyway i did use the atoi command, but then had to first convert my String to char, and then char to int. Then i found the string.toInt() command and decided to cut out char array completely.

Here are my code so far, comments are welcome, since i am still not so good at programming.

(The code will evolve further. So far the code can only tell the value. Capital letter X / Y is going to be used for setting coordinates, and regular x / y for increments on position. When set I will type RUN for going to the coordinates and HOME for zeroing)

String serDataStr;
int xPos = 0;
int yPos = 0;
int posValue = 0;



void setup() {
  Serial.begin(9600);
  Serial.println("Serial conection started, waiting for instructions...");
}

void loop() {

  while(Serial.available()) {   // Check to see if there are any availeble communication
    char recievedChar = Serial.read();
    serDataStr += recievedChar;  // Puttes the serial communication together to one string.
    delay(5);    // Neccesary on fast boards as chipkit, else it will put one serial com into two.
  }

  if (serDataStr != "") {  // If the datastring are not empty, then proceed.

    if(serDataStr[0] =='X' || serDataStr[0] =='x' || serDataStr[0] =='Y' ||  serDataStr[0]=='y'){  // If the first letter correspond to axis names
      String valueString;
      int strLen = serDataStr.length();
      
      for(int i=1; i< strLen; i++){      //Making a new string with value, typed after the X,x,Y or y;
        valueString += serDataStr[i];
      }
      
      posValue = valueString.toInt();            // Konverting the String to an integer
      Serial.println(posValue);
    }

    serDataStr = "";    //Empty the data string from the Serial

  } 
}

You can only send bytes (and generally only those that represent printable characters) from the serial monitor. You would need a PC program to generate and send integers.

You could, perhaps, simulate sending integers from the serial monitor by getting the Arduino to treat every pair of bytes as an integer. But you won’t find it easy to generate all the possible integer values that way.

…R