2014-10-19 104 views
0

這是我在Arduino板上編程的第一年。出於某種原因,我的串口監視器不會接受任何用戶輸入,我做錯了什麼?我需要兩個用戶輸入作爲浮點變量,然後在我設置的自定義函數中使用。串行監視器不接受用戶輸入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"); 

} 
+0

Pleasefixyourindentatiionelseyourcodeistoodifficulttoread – 2014-10-19 22:46:54

回答

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"); 

} 
+0

謝謝你,我缺的是我,而循環語句後分號 – 2014-10-20 05:57:47