所以我需要帮助将if else语句更改为switch case语句,因为我是arduino编码的新手,似乎无法让它工作。该程序应该闪烁LED,发出扬声器,点亮灯泡,旋转风扇,并在if else语句中的条件满足时,将选定的信息显示在16x2 lCD上。
#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 :(得分:0)
在Java中,switch语句采用以下形式:
switch (expression)
{
case constant:
statements;
break;
[ case constant-2:
statements;
break; ] ...
[ default:
statements;
break; ] ...
}
switch语句适用于byte,short,char和int原语数据类型。它还适用于枚举类型,String类和一些包含某些基本类型的特殊类:Character,Byte,Short和Integer。
你的嵌套ifs及其布尔表达式不适合切换。
答案 1 :(得分:0)
如果你想将if语句转换为Switch,即我假设你想提高代码的可读性,可以在循环中使用if语句并将switch放入函数中..检查下面的代码
<div id=rdTest>
<div class="inputField">
<label>Focal Image:</label>
<input name="FocalImage" type="text" id="field_2" class="textbox" value="">
</div>
<div class="radioGroup">
<label>Test Page:</label>
<input type="radio" name="toggle" value="Yes" id="tglyes">Yes
<input type="radio" name="toggle" value='No' id="tglno">No
</div>
<div id="thgtLdr"></div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script> // place code here </script>
并将你的不同条件放在doSomething()函数的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');
}