Hi guys, new member here.
So the problem is i want to write an Ask-Wait-Read code with if and serial.available() but when i ask, the message loops and even if i enter the data it doesn't work.
[i do know how to make this with *while(Serial.available()==0)* but i want to try make it with *if* ]
EXP:
int radius;
float area;
float pi= 3.14;
String msg1 = "Enter the radius of circle: ";
String msg2 = "Area of circle is: ";
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(msg1);
if(Serial.available() > 0) {
radius = Serial.parseFloat();
area = pi*radius*radius;
Serial.print(msg2);
Serial.println(area);
}
}
I hope i didin't make any mistake with the community guidelines.
Thanks this works perfectly but i am trying not to use while(Serial.available() ==0) for the Wait part. I am trying to make the Wait part too with if .
Actually i am trying to learn Arduino and i am currently watching Paul McWhorter #18 Reading Numbers from the Serial Monitor and after every video i read wikis about the commands etc. I read Serial.available() command wiki and saw examples about it. I wondered how can i improve the user input mechanism otherway. Thanks for the [Serial Input Basics - updated] i will read it and try to learn.
I very simplistic version of your code. The initial prompt to enter the radius is in setup(), after that you only print a new prompt after the response has been received.
int radius;
float area;
float pi = 3.14;
String msg1 = "Enter the radius of circle: ";
String msg2 = "Area of circle is: ";
void setup() {
Serial.begin(9600);
Serial.println(msg1); //initial prompt
}
void loop() {
if (Serial.available() > 0) {
radius = Serial.parseFloat();
area = pi * radius * radius;
Serial.print(msg2);
Serial.println(area);
Serial.println(msg1);//prompt for new input
}
}