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);
}
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 And even that, float is the wrong data type… A float is NOT just a decimal point
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.
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;
}}}
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;
}