此代码可以正常工作,只是其不变,导致它根据颜色打开。它总是在中间的橙色LED亮起。有什么线索吗?
#include <LiquidCrystal.h>
#include <DHT.h>
#include "DHT.h"
#define DHTPIN A0 // what pin we're connected to
#define DHTTYPE DHT11 // DHT 11
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
Serial.begin(9600);
for (int DigitalPin = 7; DigitalPin <= 9; DigitalPin++)
{
pinMode(DigitalPin, OUTPUT);
}
lcd.begin(16,2); //16 by 2 character display
dht.begin();
}
void loop()
{
delay(1000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(t);
lcd.print("'C");
lcd.setCursor(0,1);
lcd.print("Humid: ");
lcd.print(h);
lcd.print("%");
if (t<=30)
{
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
digitalWrite(8, LOW);
}
else if (t>35)
{
digitalWrite(8, HIGH);
digitalWrite(7, LOW);
digitalWrite(9, LOW);
}
else if (t>=45)
{
digitalWrite(9, HIGH);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
}
}
我一直在阅读和查找的似乎应该起作用,但事实并非如此。任何帮助,将不胜感激。我将温度传感器放入树莓派外壳中,以确保其不会太热。
答案 0 :(得分:0)
我认为您的错误是在第二次测试中
else if (t>35)
您必须更改为
else if (t>35 && t < 45)
如果没有此修复程序,温度为50,指示灯将为橙色而不是红色
但是按照程序中的逻辑,您忘记了30到35之间的测试,或者您出错了,将30更改为35或将35更改为30
答案 1 :(得分:0)
尝试这样的if-else条件:
if (t<=30)
{
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
else if (t>=45)
{
digitalWrite(9, HIGH);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
}
else
{
digitalWrite(8, HIGH);
digitalWrite(7, LOW);
digitalWrite(9, LOW);
}
希望有帮助!