About Serial.available() and if Commands

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.

The "if" statement is executed a microsecond or so after msg1 is sent off to be printed.

How many characters do you think might be in the serial input buffer, for serialParseFloat() to interpret?

I recommend the Serial Input Basics tutorial.

1 Like

Welcome to the forum

Serial.println(msg2+area);

Either split this into 2 separate print statements or cast the float to a String because you cannot print a String and a float like that

1 Like

@omniscent Welcome to the forum, good job on the post..

think you want to read in radius not area..
Project in a simulator..
made a few changes..

float radius = 1;
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() {
  if (radius > 0)
    Serial.println(msg1);

  while (Serial.available() == 0);
  if (Serial.available() > 0) {
    radius = Serial.parseFloat();
    if (radius > 0) {
      Serial.println(radius);
      area = pi * radius * radius;
      Serial.println(msg2 + area);
    }
  }
}

have fun.. ~q

1 Like

No need for the subsequent "if" statement.

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 .

like this??

float radius = 1;
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() {
  if (radius > 0)
    Serial.println(msg1);


  if (Serial.available() > 0) {
    radius = Serial.parseFloat();
    if (radius > 0) {
      Serial.println(radius);
      area = pi * radius * radius;
      Serial.println(msg2 + area);
    }
  } else radius = 0;
}

~q

1 Like

Why have a wait part at all ?

See Serial input basics - updated

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.

1 Like

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
  }
}
1 Like

Now i know where i do wrong. Placing the msg1 to the loop was the mistake.
I really appreciate the help guys. Thanks and have a nice day.

Why should it be a mistaken?

Look at the following sketch which has put the msg1 in loop() function. (Select No line ending option.)

float radius;
float area;
#define pi 3.14

char msg1[] = "Enter the radius of circle: ";
char msg2[] = "Area of circle is: ";

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

void loop() 
{
  Serial.print(msg1);
  L1: byte n = Serial.available();
  if(n == 0)
  {
    goto L1;
  }
  radius = Serial.parseFloat();
  Serial.println(radius, 2);
  //------------------------
  Serial.print(msg2);
  area = pi*radius*radius;
  Serial.println(area, 2);
  Serial.println("==================================");
}

Output:

Enter the radius of circle: 4.57
Area of circle is: 65.58
==================================
Enter the radius of circle: 
1 Like

I do love a good satire.

I could not find any other option to comply with OP's adamant if()!

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