receiving x,y Coordinates from Unity

Hi i am sending endlessly x,y coordinates and i am trying to put them in variables which i print in Serial Monitor . The thing is that my code doesn't work . Has someone any ideas ? thanks .

The data that we are sending from Unity is :
x,650.45,1150.789x,780.45,900.4325x,560.549,809.567 etc.

i used the code from : Data Input demo sketch - Programming Questions - Arduino Forum

const byte buffSize = 32;
char inputSeveral[buffSize];
float inputFloat1 = 0.0;
float inputFloat2 = 0.0;
char inputCsvString[12];
boolean coordready =false;

void setup() {
    Serial.begin(9600);
    Serial1.begin(9600);
    Serial.println("Starting DemoDataInput");
}

void loop() {
    readCSV();
}

void readCSV() {
    inputSeveral[0] = 0;
    byte ndx = 0;
    byte xCount=0;

      while (Serial1.available() > 0) {
       inputSeveral[ndx] = Serial1.read();
       if (inputSeveral[ndx] == 'x') {
          coordready = true;
          xCount ++;
       }
       else {
          coordready = false;
       }
       ndx ++;
       if (coordready == true && xCount > 1) {
        
        inputSeveral[ndx] = 0;
        char * partOfString;

        partOfString = strtok(inputSeveral,",");
        strcpy(inputCsvString, partOfString);

        partOfString = strtok(NULL, ",");
        inputFloat1 = atof(partOfString);

        partOfString = strtok(NULL, ",");
        inputFloat2 = atof(partOfString);

        Serial.println(" x coord = ");
        Serial.println(inputFloat1);
        Serial.println(" y coord = ");
        Serial.println(inputFloat2);

        ndx = 1;
       }
      }

}

The serial input basics thread will help to reliably receive your data. You probably should change the format of the data being sent to make it easier and more reliable to receive and parse (see receive with start and end markers and example #5 for parsing).

The data that we are sending from Unity is

Do you have any control over that format? Alljammedtogetherlikethatishardtoread.

The lack of a end of packet marker makes parsing more difficult than it needs to be.

The thing is that my code doesn't work .

It does something. You need to determine what it actually does. Send the same data using the Serial Monitor application, so you can see what the Arduino sends back (or could send back).

      if (inputSeveral[ndx] == 'x') {
          coordready = true;

That is NOT true. You do NOT have a coordinate just because the x arrived.

       ndx = 1;

Why are you not resetting to 0?

PaulS:
Do you have any control over that format? Alljammedtogetherlikethatishardtoread.

The lack of a end of packet marker makes parsing more difficult than it needs to be.
It does something. You need to determine what it actually does. Send the same data using the Serial Monitor application, so you can see what the Arduino sends back (or could send back).

      if (inputSeveral[ndx] == 'x') {

coordready = true;



That is NOT true. You do NOT have a coordinate just because the x arrived.



ndx = 1;



Why are you not resetting to 0?

Yes we can send the coordinates as we like . X,Y are floats converted to strings.
For example we can send <700.45,1100.5678>

For the second comment i think we have the first x,y into the array.

For the third comment i put ndx=1 because if we put ndx=0 we lose the char 'x' which is important for the strtok to work.
( i dont know what exactly this code does)
char * partOfString;

partOfString = strtok(inputSeveral,",");
strcpy(inputCsvString, partOfString);

partOfString = strtok(NULL, ",");
inputFloat1 = atof(partOfString);

partOfString = strtok(NULL, ",");
inputFloat2 = atof(partOfString);

For example we can send <700.45,1100.5678>

So, do that, and use Robin's receive with start and end markers example.

When parsing the data after the > arrives. you will have two calls to strtok(). The first will have the buffer name in the call. The second will have NULL in the call.

You CAN also add a space after the comma, so that a humancanreadthedatawithouttrouble.

And, you WILL reset the index variable to 0.

Thank you very much PaulS , i did it . I appreciate your help .
Here is the code :

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

      // variables to hold the parsed data
float floatFromPC1 = 0.0;
float floatFromPC2 = 0.0;

boolean newData = false;

//============

void setup() {
    Serial.begin(9600);
    Serial.println("This demo expects 2 pieces of data - two floating point values");
    Serial.println("Enter data in this style <65.45, 24.7>  ");
    Serial.println();
}

//============

void loop() {
    recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with \0
        parseData();
        showParsedData();
        newData = false;
    }
}

//============

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//============

void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars, ","); // this continues where the previous call left off
    floatFromPC1 = atof(strtokIndx);     // convert this part to a float

    strtokIndx = strtok(NULL, ",");
    floatFromPC2 = atof(strtokIndx);     // convert this part to a float
    
    strtokIndx = 0;
}

//============

void showParsedData() {
   
    Serial.print(" X coord is: ");
    Serial.println(floatFromPC1);
    Serial.print(" Y coord is: ");
    Serial.println(floatFromPC2);
}

Hi i am receiving data from Unity3d to Arduino and i am having problems printing wrong data to Serial Monitor . Here is the code :

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing
float floatFromPC1 = 0.0;
float floatFromPC2 = 0.0;

boolean newData = false;

void setup() {
    Serial.begin(9600);
    Serial1.begin(9600);
    Serial.println("Receive data from Unity3d like  <650.4578, 1150.724>  ");
    Serial.println();
}

void loop() {
    recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with \0
        parseData();
        showParsedData();
        newData = false;
    }
}

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (Serial1.available() > 0 && newData == false) {
        rc = Serial1.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }
        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars, ","); // this continues where the previous call left off
    floatFromPC1 = atof(strtokIndx);     // convert this part to a float

    strtokIndx = strtok(NULL, ",");
    floatFromPC2 = atof(strtokIndx);     // convert this part to a float
    
    strtokIndx = 0;
}

void showParsedData() {
   
    Serial.print(" X coord is: ");
    Serial.println(floatFromPC1);
    Serial.print(" Y coord is: ");
    Serial.println(floatFromPC2);
}

And here is some of the data we are receiving :

Y coord is: 891.38
 X coord is: 63.00
 Y coord is: 877.96
 X coord is: 921.40
 Y coord is: 859.11
 X coord is: 922.91
 Y coord is: 848.23
 X coord is: 925.33
 Y coord is: 83318.31
 X coord is: 930.00
 Y coord is: 0.00
 X coord is: 936.88
 Y coord is: 772.00
 X coord is: 944.38
 Y coord is: 740.92
 X coord is: 95741.00
 Y coord is: 675.47
 X coord is: 9.00
 Y coord is: 616.63
 X coord is: 981.77
 Y coord is: 602.54
 X coord is: 985.03
 Y coord is: 0.00
 X coord is: 988.51
 Y coord is: 577.37
 X coord is: 7.00
 Y coord is: 0.00
 X coord is: 995.12
 Y coord is: 0.00
 X coord is: 1001.07
 Y coord is: 526.49
 X coord is: 101679.28
 Y coord is: 426.31
 X coord is: 1019.66
 Y coord is: 385.22
 X coord is: 1020.50
 Y coord is: 370.37
 X coord is: 1021.27

Any ideas? the boundaries on the coordinates are [0,2000].

maybe just for debugging purposes try printing out data for Serial1 to Serial every time data is available on Serial1 and see if all is well ie you are received data in the format you expect e.g <650.4578, 1150.724>

maybe are we sending too fast for the serial buffer ? i can't think anything else . maybe a delay in the unity program should fix the problem ? what do you think ?