A_Nerd
November 2, 2021, 9:49pm
1
I am writing some code that is communicating motor positions from Unity to Arduino over the serial port. I am suck on writing a function to delimnate the incoming string.
`string incomingString = "motorPos1,motorPos2,motorPos3,motorPos4,motorPos5,endCommand";`
`string incomingString = "28.5,42.9,12.0,68.65,0.12,0x0F";`
I want to convert this string into a float array containing the motor positons and one string containg the endCommand like such:
float motorPositions[] = {mPos1, mPos2, mPos3, mPos4, mPos5};
string endCommand = endCommand;
I am fairly flexible on what the result would be. Thanks for the help.
Maybe something like this:
float motorPositions[5];
String endCommand;
void ParseInput ()
{
motorPositions[0] = Serial.parseFloat();
motorPositions[1] = Serial.parseFloat();
motorPositions[2] = Serial.parseFloat();
motorPositions[3] = Serial.parseFloat();
motorPositions[4] = Serial.parseFloat();
endCommand = Serial.readString(); // Ends at timeout
}
void setup()
{
Serial.begin(115200);
}
void loop()
{
if (Serial.available())
{
ParseInput();
for (int i = 0; i < 5; i++)
{
Serial.print("motorPos");
Serial.print(i + 1);
Serial.print(" = ");
Serial.println(motorPositions[i]);
}
Serial.print("endCommand = ");
Serial.println(endCommand);
}
}
system
Closed
May 1, 2022, 10:01pm
3
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.