串行监视器不接受arduino c编程中的用户输入

时间:2014-10-19 21:14:16

标签: c while-loop arduino user-input

这是我在Arduino板上进行C编程的第一年。出于某种原因,我的串口显示器不能接受任何用户输入,我做错了什么?我需要将两个用户输入作为浮点变量,然后在我设置的自定义函数中使用。

float contSurfArea(float x, float y){
  float z;
  z = (3.14159*x*x)+(2*3.14159*x*y);
  return (z);
}




 void setup()
  {
    Serial.begin(9600); //serial communication initialized


}

void loop(){
  float baseRad, contHeight; 

  Serial.println("Open-top Cylindrical Container Program");
  delay(2000);

  Serial.println("Radius of the base(in meters): ");
  while(Serial.available()<=0)
  baseRad= Serial.parseFloat( );
  delay(2000);

  Serial.println("Height of the container(in meters): ");
  while(Serial.available()<=0)
  contHeight= Serial.parseFloat( );
  delay(2000);

  float q;
  q = contSurfArea(baseRad, contHeight);

  Serial.print("The surface area of your container is: ");
  Serial.print(q);
  Serial.print( "meters^2");

}

1 个答案:

答案 0 :(得分:0)

这应该适合你:

float contSurfArea(float x, float y){
  float z;
  z = (3.14159*x*x)+(2*3.14159*x*y);
  return (z);
}




 void setup()
  {
    Serial.begin(9600); //serial communication initialized


}

void loop(){
  float baseRad, contHeight = -1; 
  char junk = ' ';

  Serial.println("Open-top Cylindrical Container Program");
  delay(2000);

  Serial.println("Radius of the base(in meters): ");
  while (Serial.available() == 0); //Wait here until input buffer has a character
  baseRad = Serial.parseFloat();
  Serial.print("baseRad = "); Serial.println(baseRad, DEC);
  while (Serial.available() > 0) { //parseFloat() can leave non-numeric characters
    junk = Serial.read(); //clear the keyboard buffer
  }

  Serial.println("Height of the container(in meters): ");
  while (Serial.available() == 0); //Wait here until input buffer has a character
  contHeight = Serial.parseFloat();
  Serial.print("contHeight = "); Serial.println(contHeight, DEC);
  while (Serial.available() > 0) {
    junk = Serial.read(); //clear the keyboard buffer
  }

  float q;
  q = contSurfArea(baseRad, contHeight);

  Serial.print("The surface area of your container is: ");
  Serial.print(q);
  Serial.print( "meters^2");

}