所以我需要幫助改變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);
}
@ MikeJRamsey56我試圖與第一個if else語句2實現開關的情況下,它只是顯示的lightReading和temp不刷新顯示消息whe條件得到滿足。 –