unexpected println

i am trying to use a serial input as a variable and so far so good. the code basically works but i do not understand why the result prints out twice and the second result is not correct.

i would have expected the if command to only be evaluated once?

Torben

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

void loop() {
    float area;
    char rx_byte;
  
  if (Serial.available() > 0) {    // is a character available?
    rx_byte = Serial.read();       // get the character
  
    // calculate the area of a circle with radius of serial input
    area = CircleArea(rx_byte-48);
    Serial.print("Area of circle is: ");
    Serial.println(area, 4); // print area to 4 decimal places
    delay(1000); 
}
}
// function calculate the area of a circle
float CircleArea(float radius){
    float result;    
    result = 3.141592654 * radius * radius;
    return result;
}

What is the serial monitor line ending set to?

AWOL:
What is the serial monitor line ending set to?

Hi Awol

thanks. it was set to Newline

Torben

...and there's your answer.

Why are you passing a byte to a function that expects a float? You REALLY need to understand what each of the methods in the Serial instance's class do.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R