需要帮助将 if else 语句更改为 switch case
Need help changing if else statements into switch case
所以我需要帮助将 if else 语句更改为 switch case 语句,因为我是 arduino 编码的新手,似乎无法让它工作。当满足 if else 语句中的条件时,该程序应该使 LED 灯闪烁、扬声器发出声音、灯泡点亮、风扇旋转并在 16x2 液晶显示器上显示所选消息。
#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);
}
在 Java 中,switch 语句采用以下形式:
switch (expression)
{
case constant:
statements;
break;
[ case constant-2:
statements;
break; ] ...
[ default:
statements;
break; ] ...
}
switch 语句适用于 byte、short、char 和 int 原始数据类型。它还适用于枚举类型、字符串 class 和一些包装某些基本类型的特殊 classes:Character、Byte、Short 和 Integer。
您的带有布尔表达式的嵌套 if 不适合 switch。
如果你想将 if 语句转换为 Switch,即我假设你想提高代码的可读性,你可以在循环中使用 if 语句并将 switch 放在函数中..检查下面的代码
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');
}
然后将您的不同条件放入 doSomething() 函数的 switch 语句中,如下所示
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;
}
}
所以我需要帮助将 if else 语句更改为 switch case 语句,因为我是 arduino 编码的新手,似乎无法让它工作。当满足 if else 语句中的条件时,该程序应该使 LED 灯闪烁、扬声器发出声音、灯泡点亮、风扇旋转并在 16x2 液晶显示器上显示所选消息。
#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);
}
在 Java 中,switch 语句采用以下形式:
switch (expression)
{
case constant:
statements;
break;
[ case constant-2:
statements;
break; ] ...
[ default:
statements;
break; ] ...
}
switch 语句适用于 byte、short、char 和 int 原始数据类型。它还适用于枚举类型、字符串 class 和一些包装某些基本类型的特殊 classes:Character、Byte、Short 和 Integer。
您的带有布尔表达式的嵌套 if 不适合 switch。
如果你想将 if 语句转换为 Switch,即我假设你想提高代码的可读性,你可以在循环中使用 if 语句并将 switch 放在函数中..检查下面的代码
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');
}
然后将您的不同条件放入 doSomething() 函数的 switch 语句中,如下所示
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;
}
}