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;
}