connect arduino with unity, convert string to float

Hi, this code converts string values to float but I can not take advantage of converted values. For example, if you say :

if(floatFromPC1>1){
digitalWrite(1,HIGH);
}

Does not respond, What is the problem with your Opinion

#include <Stepper.h>
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;

// initialize the stepper library on pins 8 through 11:

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(" Z coord is: ");
  Serial.println(floatFromPC2);
  

}

What is the serial output you get?

What is the problem with your Opinion

In my opinion, it is operator error.

The code you posted expects some input that you have not shared. It generates (or could) some output that you haven't shared.

So, it is extremely likely that it just that your expectations are wrong.

septillion:
What is the serial output you get?

septillion:
What is the serial output you get?

septillion:
What is the serial output you get?

I want to get variables of type float

The code you posted expects some input that you have not shared. It generates (or could) some output that you haven’t shared.

PaulS:
In my opinion, it is operator error.

The code you posted expects some input that you have not shared. It generates (or could) some output that you haven't shared.

So, it is extremely likely that it just that your expectations are wrong.

This is the code that sends the information from Unity to Arduino :

[code]using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.IO.Ports;


public class Arduinoconnect : MonoBehaviour {
	private SerialPort sp;
	public Vector3 car_Pos;
	void Start() {
		sp = new SerialPort("COM3",9600);
		sp.ReadTimeout = 50;
		sp.Open();

	}
	void Update() {
		car_Pos = transform.position;
		string _x = car_Pos.x.ToString();
		//string _z  = car_Pos.z.ToString();
		char[] s = new char[1];
		s[0] = 'x';
		sp.Write(s,0,1);
		sp.Write(_x);
		s[0] = 'z';
		sp.Write(s,0,1);
		sp.Write(_z);
		s[0] = 'e';     // to understand full package of X,Y is sent
		sp.Write(s,0,1);


	}
}

[/code]

WHY is it so hard to just post what is send to the Arduino and what you get back? I do know it’s hard for us to magically just know it… We’re not here to debug a Python script…

And if you don’t know THAT is a good starting point. Dump the script and just give it a try with the serial monitor.

And I don’t care you want floats, that’s not what I asked :wink: And even that, float is the wrong data type… A float is NOT just a decimal point :wink:

Your code works as expected with input from the serial monitor and turning an led on and off if floatFromPC is >1.

I can also send the float strings from a PC with Python to to the arduino, and can see the arduino respond correctly.

I would focus on your connection with unity, and what you are really sending.

This code sends information from the Unity engine to the Arduino :

Does it?

void Start() {
		sp = new SerialPort("COM3",9600);
		sp.ReadTimeout = 50;
		sp.Open();

	}

I think you need to wait for a bit after you open the Serial port from unity to allow for the Arduino to reset.

I don't know how to create a delay with unity, but in my Python scripts, I need a two second wait after opening the port before sending to the Arduino.

cattledog:

void Start() {

sp = new SerialPort(“COM3”,9600);
sp.ReadTimeout = 50;
sp.Open();

}




I think you need to wait for a bit after you open the Serial port from unity to allow for the Arduino to reset.

I don't know how to create a delay with unity, but in my Python scripts, I need a two second wait after opening the port before sending to the Arduino.

My friend data arrives from untiy, but the problem is in converting data from string to float

mean imagine that code:

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

// variables to hold the parsed data 
boolean newData = false;
//============
void setup()
{ Serial.begin(9600);
}

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

void loop() {
   
  recvWithStartEndMarkers();
  }

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

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;
char * strtokIndx; // this is used by strtok() as an index
  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;
    }}}

Now,How to convert string data to float

data arrives from unity, but the problem is in converting data from string to float

I have no knowledge of unity and how the numbers in the string are encoded, but when the data is sent by the serial monitor or by Python as the bytes of the ascii or utf-8 characters the Arduino reads, parses and converts to floats. You have followed Robin2's serial input code exactly, and atof() in the code you are using converts the data from string to float

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

In my opinion your problem is on the unity side.