Sending data to Arduino over serial and passing it to variables

What I'm trying to make is a simple routine for receiving data through the serial and, where the format of the data is a "tag"+"a number" (either float or int), and after passing a simple validation check assigning the numerical data to specified variables.

As far as I understand it receives the data correctly, but the validation routine doesn't work.

/*
 * Test code for receiving data through serial and passing the data to variables after simple check for valid data.
 * 
 */

float kp = 1.0;
float ki = 1.0;
float kd = 1.0;



void setup() {
  Serial.begin(115200);
}

void loop() {
  serialInput();
}

String serialData;
void serialInput() {
  while (Serial.available()) {
    char incommingByte = Serial.read();
    if (incommingByte == '\n') { // end of serial data
      parseCommand(serialData);
      serialData = "";
    }
    else {
      serialData += incommingByte;
    }
  }

}

void parseCommand(String com) {
  // part1 = {kp,ki,kd}
  // part2 = int or float
  // eg. kp15 or ki1.0
  String part1, part2;

  part1 = com.substring(0, 2);
  if (!(part1 == "kp" || part1 == "ki" || part1 == "kd")) return ;
  part2 = com.substring(2);

  // Check if part2 is an int or float
  boolean isFloat = 0;
  for (int i = 0; i < part2.length(); i++) {
    char c = part2[i];
    if (!isDigit(c)) {
      if (isPunct(c)) {
        if (isFloat == 1) return; // return if more than one punctiation
        isFloat = 1;
      }
      else return;
    }
  }
  // Set parameters
  if (part1 == "kp") {
    if (isFloat) kp = (float)part2.toInt();
    else kp = part2.toFloat();
  }
  if (part1 == "ki") {
    if (isFloat) ki = (float)part2.toInt();
    else ki = part2.toFloat();
  }
  if (part1 == "kd") {
    if (isFloat) kd = (float)part2.toInt();
    else kd = part2.toFloat();
  }

}

but the validation routine doesn't work.

It does something. You need to tell us what it actually does. You need to tell us what you expect it to do.

    if (isFloat) kp = (float)part2.toInt();

Casting an int to a float is just plain wrong. And unnecessary.

Converting a float to an int, or an int to a float is wrong, too. What were you thinking?

This post shows how to do reliable, non-blocking serial input and parsing without Strings.

PaulS:
It does something. You need to tell us what it actually does. You need to tell us what you expect it to do.

    if (isFloat) kp = (float)part2.toInt();

Casting an int to a float is just plain wrong. And unnecessary.

Converting a float to an int, or an int to a float is wrong, too. What were you thinking?

The idea is to update the variables (kp, ki, kd) via serial as part of a PID controller. The code is an attempt at doing so by having the arduino receiving serial data and then determine which variable to change and what it should be changed to. The format it should accept is "kp1.0" or "ki2", etc. The name of the variable followed by the value which is either an int or a float.

Lars81:
by having the arduino receiving serial data

That's what the link to Serial Input Basics that @groundfungus gave you is all about.

...R

The format it should accept is "kp1.0" or "ki2", etc. The name of the variable followed by the value which is either an int or a float.

Suppose that you send "kp1.0". You scan the characters, '1', '.', and '0', and determine that there is a decimal point, and set isFloat to 1. Why you don't set it to true is a mystery for another time.

If the value is a float, you use toInt() to extract the value. Otherwise, you use toFloat() to extract the value. Completely ass-backwards, in my opinion.

Casting an int to a float makes no sense. There is NO way that the 2 bytes of an int (or 4 bytes of a long, which toInt() actually returns) can be meaningfully understood as a float.

The cast is useless in any case. The long returned by toInt() or the float returned by toFloat() will be stored in the double kp properly, without your "advice".