2016-05-27 109 views
0

所以我需要幫助改變if else語句到switch case語句中,因爲我是在arduino中編碼的新手,似乎無法使其工作。該程序應該使LED閃爍,發聲揚聲器,點亮燈泡,旋轉風扇,並在if if語句中的條件滿足時將選定消息顯示在16x2 lCD上。需要幫助改變if else語句到switch case

#include <LiquidCrystal.h> 

    //Pins of LCD  RS E D4 D5 D6 D7 
    LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // Pins of Arduino 
    int tempPin = A0; //LM35 Pin 
    int ldrPin = A1; //LDR Pin 
    int led = 6; //LED Pin 
    int tempMax = 100; 
    int buzzer = 13; 
    float temp; 
    float tempC; 
    int lightReading; 
    int numTones = 2; 
    int tones[] = {440, 349}; 
    int TIP120pin = 5; 
    int bulb = A2; 

    void setup() 
    { 
     lcd.begin(16, 2); 

     Serial.begin(9600); 

     pinMode(led, OUTPUT); 

     pinMode(tempPin, INPUT); 

     pinMode(ldrPin, INPUT); 

     pinMode(buzzer,OUTPUT); 

     pinMode(TIP120pin, OUTPUT); 

     pinMode(bulb, OUTPUT); 

     lcd.clear(); 
     beep(100); 
    } 
    void loop() 
    { 
     tempC = (5.0 * analogRead(tempPin) * 100.0)/1024; 
     temp = tempC; 
     lightReading = analogRead(ldrPin); 

     lcd.print("TEMP:"); 
     lcd.setCursor(5, 0); 
     lcd.print(temp);  // display the temperature 
     lcd.print("C"); 

     // Display Light on second row 
     lcd.setCursor(0, 1); 
     lcd.print("Light   "); 
     lcd.setCursor(6, 1); 
     lcd.print(lightReading); 
     delay(500); 
     analogWrite(TIP120pin, 0); 
     analogWrite(bulb, 0); 

     if (temp >=35) // if temp is higher than max temp 
     { 
     lcd.clear(); 
     analogWrite(TIP120pin, 255); 
     lcd.setCursor(3, 0); 
     lcd.print("Room Hot!!"); 
     digitalWrite(led, HIGH); 
     delay(200); 
     beep(50); 
     digitalWrite(led, LOW); 
     delay(200); 
     } 

     else if (temp >=30 && temp<35) 
     { 
     lcd.clear(); 
     analogWrite(TIP120pin, 255); 
     digitalWrite(led, HIGH); 
     delay(200); 
     beep(50); 
     digitalWrite(led, LOW); 
     delay(200); 
     lcd.setCursor(3, 0); 
     lcd.print("Room Warm!"); 
     delay(200); 

     } 
    else if (lightReading >100 && lightReading <= 200) 
     { 
     lcd.clear(); 
     analogWrite(bulb, 150); 
     digitalWrite(led, HIGH); 
     delay(200); 
     beep(50); 
     digitalWrite(led, LOW); 
     delay(200); 
     // Display Light on second row 
     lcd.setCursor(3, 0); 
     lcd.print("Room Dim"); 
     delay(200); 
     analogWrite(TIP120pin, 0); 
     } 
     else if (lightReading <=100) 
     { 
     lcd.clear(); 
     analogWrite(bulb, 255); 
     digitalWrite(led, HIGH); 
     delay(200); 
     beep(50); 
     digitalWrite(led, LOW); 
     delay(200); 
     lcd.setCursor(3, 0); 
     lcd.print("Room Dark"); 
     delay(200); 
     analogWrite(TIP120pin, 0); 
     }  

     else if(temp>=35 && lightReading>100 && lightReading<=200) 
     { 
     lcd.clear(); 
     analogWrite(bulb, 150); 
     analogWrite(TIP120pin, 255); 
     digitalWrite(led, HIGH); 
     delay(200); 
     beep(50); 
     digitalWrite(led, LOW); 
     delay(200); 
     lcd.setCursor(3, 0); 
     lcd.print("Room Hot!"); 
     lcd.setCursor(3, 1); 
     lcd.print("Room Dim!"); 
     delay(200); 
     } 

     else if(temp>=35 && lightReading<=100) 
     { 
     lcd.clear(); 
     analogWrite(bulb, 255); 
     analogWrite(TIP120pin, 255); 
     digitalWrite(led, HIGH); 
     delay(200); 
     beep(50); 
     digitalWrite(led, LOW); 
     delay(200); 
     lcd.setCursor(3, 0); 
     lcd.print("Room Hot!"); 
     lcd.setCursor(3, 1); 
     lcd.print("Room Dark!"); 
     delay(200); 
     } 

     else if(temp>=30 && temp<35 && lightReading>=100 && lightReading <=200) 
     { 
     lcd.clear(); 
     analogWrite(bulb, 150); 
     analogWrite(TIP120pin, 150); 
     digitalWrite(led, HIGH); 
     delay(200); 
     beep(50); 
     digitalWrite(led, LOW); 
     delay(200); 
     lcd.setCursor(3, 0); 
     lcd.print("Room Warm!"); 
     lcd.setCursor(3, 1); 
     lcd.print("Room Dim!"); 
     delay(200); 
     } 

     else if (temp>=30 && temp<35 && lightReading<100) 
     { 
     lcd.clear(); 
     analogWrite(bulb, 255); 
     analogWrite(TIP120pin, 255); 
     digitalWrite(led, HIGH); 
     delay(200); 
     beep(50); 
     digitalWrite(led, LOW); 
     delay(200); 
     lcd.setCursor(3, 0); 
     lcd.print("Room Hot!!"); 
     lcd.setCursor(3, 1); 
     lcd.print("Room Dark!"); 
     delay(200); 
     } 

     else 
     { 
     digitalWrite(led, LOW); 
     digitalWrite(buzzer, LOW); 
     analogWrite(bulb, LOW); 
     lcd.print("TEMP:"); 
     lcd.setCursor(5, 0); 
     lcd.print(temp);  
     lcd.print("C"); 

     // Display Light on second row 
     lcd.setCursor(0, 1); 
     lcd.print("Light   "); 
     lcd.setCursor(6, 1); 
     lcd.print(lightReading); 
     } 
     delay(500); 
     lcd.clear(); 
     Serial.print("Light, "); Serial.print(lightReading); Serial.print('\n'); 
    } 
    void beep(unsigned char delayms){ 
     for(int x=0; x<numTones; x++) 
     { 
     tone(buzzer, tones[x]); 
     delay(delayms); 
     } 
     noTone(buzzer); 
     } 
+0

@ MikeJRamsey56我試圖與第一個if else語句2實現開關的情況下,它只是顯示的lightReading和temp不刷新顯示消息whe條件得到滿足。 –

回答

0

如果要將if語句轉換爲Switch,即我假設您想要提高代碼的可讀性,則可以在循環中使用if語句並將開關放入函數中..檢查以下代碼

void loop() 
{ 
    tempC = (5.0 * analogRead(tempPin) * 100.0)/1024; 
    temp = tempC; 
    lightReading = analogRead(ldrPin); 

    lcd.print("TEMP:"); 
    lcd.setCursor(5, 0); 
    lcd.print(temp);  // display the temperature 
    lcd.print("C"); 

    // Display Light on second row 
    lcd.setCursor(0, 1); 
    lcd.print("Light   "); 
    lcd.setCursor(6, 1); 
    lcd.print(lightReading); 
    delay(500); 
    analogWrite(TIP120pin, 0); 
    analogWrite(bulb, 0); 

    if (temp >=35) // if temp is higher than max temp 
    { 
    doSomething('a'); 
    } 

    else if (temp >=30 && temp<35) 
    { 
    doSomething('b'); 
    } 
else if (lightReading >100 && lightReading <= 200) 
    { 
    doSomething('c'); 
    } 
    else if (lightReading <=100) 
    { 
    doSomething('d'); 
    }  

    else if(temp>=35 && lightReading>100 && lightReading<=200) 
    { 
    doSomething('e'); 
    } 

    else if(temp>=35 && lightReading<=100) 
    { 
    doSomething('f'); 
    } 

    else if(temp>=30 && temp<35 && lightReading>=100 && lightReading <=200) 
    { 
    doSomething('g'); 
    } 

    else if (temp>=30 && temp<35 && lightReading<100) 
    { 
    doSomething('h'); 
    } 

    else 
    { 
     doSomething('i'); 
    } 

,並把你的不同狀況在switch語句中的doSomething()函數這樣

void doSomething(condition){ 
switch(condition){  
case 'a' : // if temp is higher than max temp 

lcd.clear(); 
analogWrite(TIP120pin, 255); 
lcd.setCursor(3, 0); 
    lcd.print("Room Hot!!"); 
    digitalWrite(led, HIGH); 
    delay(200); 
    beep(50); 
    digitalWrite(led, LOW); 
    delay(200); 
break; 

case 'b' : 

    lcd.clear(); 
    analogWrite(TIP120pin, 255); 
    digitalWrite(led, HIGH); 
    delay(200); 
    beep(50); 
    digitalWrite(led, LOW); 
    delay(200); 
    lcd.setCursor(3, 0); 
    lcd.print("Room Warm!"); 
    delay(200); 
break; 

case 'c' : 
    lcd.clear(); 
    analogWrite(bulb, 150); 
    digitalWrite(led, HIGH); 
    delay(200); 
    beep(50); 
    digitalWrite(led, LOW); 
    delay(200); 
    // Display Light on second row 
    lcd.setCursor(3, 0); 
    lcd.print("Room Dim"); 
    delay(200); 
    analogWrite(TIP120pin, 0); 
break; 

case 'd': 

    lcd.clear(); 
    analogWrite(bulb, 255); 
    digitalWrite(led, HIGH); 
    delay(200); 
    beep(50); 
    digitalWrite(led, LOW); 
    delay(200); 
    lcd.setCursor(3, 0); 
    lcd.print("Room Dark"); 
    delay(200); 
    analogWrite(TIP120pin, 0); 
break;  

case 'e': 

    lcd.clear(); 
    analogWrite(bulb, 150); 
    analogWrite(TIP120pin, 255); 
    digitalWrite(led, HIGH); 
    delay(200); 
    beep(50); 
    digitalWrite(led, LOW); 
    delay(200); 
    lcd.setCursor(3, 0); 
    lcd.print("Room Hot!"); 
    lcd.setCursor(3, 1); 
    lcd.print("Room Dim!"); 
    delay(200); 
break; 

case 'f': 
    lcd.clear(); 
    analogWrite(bulb, 255); 
    analogWrite(TIP120pin, 255); 
    digitalWrite(led, HIGH); 
    delay(200); 
    beep(50); 
    digitalWrite(led, LOW); 
    delay(200); 
    lcd.setCursor(3, 0); 
    lcd.print("Room Hot!"); 
    lcd.setCursor(3, 1); 
    lcd.print("Room Dark!"); 
    delay(200); 
break; 

case 'g': 
    lcd.clear(); 
    analogWrite(bulb, 150); 
    analogWrite(TIP120pin, 150); 
    digitalWrite(led, HIGH); 
    delay(200); 
    beep(50); 
    digitalWrite(led, LOW); 
    delay(200); 
    lcd.setCursor(3, 0); 
    lcd.print("Room Warm!"); 
    lcd.setCursor(3, 1); 
    lcd.print("Room Dim!"); 
    delay(200); 
break; 

case 'h': 
    lcd.clear(); 
    analogWrite(bulb, 255); 
    analogWrite(TIP120pin, 255); 
    digitalWrite(led, HIGH); 
    delay(200); 
    beep(50); 
    digitalWrite(led, LOW); 
    delay(200); 
    lcd.setCursor(3, 0); 
    lcd.print("Room Hot!!"); 
    lcd.setCursor(3, 1); 
    lcd.print("Room Dark!"); 
    delay(200); 
break; 


default: 
    digitalWrite(led, LOW); 
    digitalWrite(buzzer, LOW); 
    analogWrite(bulb, LOW); 
    lcd.print("TEMP:"); 
    lcd.setCursor(5, 0); 
    lcd.print(temp);  
    lcd.print("C"); 

    // Display Light on second row 
    lcd.setCursor(0, 1); 
    lcd.print("Light   "); 
    lcd.setCursor(6, 1); 
    lcd.print(lightReading); 
break; 
} 
} 
+0

我將這段代碼上傳到了arduino中,並且在滿足亮度和溫度的條件下,它只向我顯示了LCD上的任何一個,或者只是向我展示「房間溫暖」和「房間黑暗」之間循環,儘管我已經設置了它在液晶顯示器的兩個不同的行上顯示..任何想法爲什麼會發生這種情況? –

+0

做一件事情..使用Serial.println()在所有情況下..並檢查你是否在這種情況下着陸或.. –

0

在Java中,switch語句採用這種形式:

switch (expression) 
{ 
    case constant: 
     statements; 
     break; 
    [ case constant-2: 
     statements; 
     break; ] ... 
    [ default: 
     statements; 
     break; ] ... 
} 

switch語句的工作原理與字節,短,燒焦,和int原始數據類型。它也適用於枚舉類型,String類和幾個包裝特定基本類型的特殊類:Character,Byte,Short和Integer。

您嵌套的ifs與他們的布爾表達式不適合切換。