Input float from serial monitor

sorry for this dumb question, im new to arduino. i want to calculate how much user shld drink based on their weight. currently the weight is in string. is it possible for me to convert to float/ integer.
"
String weight;
float goal;
void setup() {
Serial.begin(9600);

delay(2000);

Serial.println("What is your weight?");

}

void loop() {
if(Serial.available()){
weight = Serial.readStringUntil('\n');

    Serial.println("your weight is " + weight + "!");
}

}"

String.toFloat()

set that

You could change that to:
float weight = Serial.parseFloat();

Try the following codes placing in your sketch with Newline option in the Line ending tab of SM.

void loop()
{
  if (Serial.available())
  {
    byte m = Serial.readBytesUntil('\n', weight, 10);
    weight[m] = '\0'; //insert null-charcater
    goal = atof(weight);   //converting string/ASCII to float
    Serial.print("your weight is: ");
    Serial.println(goal, 2);
  }
}

Output:

What is your weight?
your weight is: 69.75

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.