如何让数字输出引脚正常工作
how to get digital out pins working correctly
此代码按预期工作,除了它没有改变导致它根据颜色打开。它始终是亮着的中间橙色 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);
}
}
从我一直在阅读和查找的内容来看,它似乎应该可以工作,但事实并非如此。对此的任何帮助将不胜感激。我将温度传感器放在我的 raspberry pi 外壳内,以确保它不会变得太热。
我认为你的错误是在第二次测试中
else if (t>35)
你必须改成
else if (t>35 && t < 45)
如果没有此修复,温度为 50,lamp 将是橙色而不是红色
但是按照你程序中的逻辑,你忘记了30到35之间的测试,或者你出错了把30改成35或者35改成30
像这样尝试你的 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);
}
希望对您有所帮助!
此代码按预期工作,除了它没有改变导致它根据颜色打开。它始终是亮着的中间橙色 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);
}
}
从我一直在阅读和查找的内容来看,它似乎应该可以工作,但事实并非如此。对此的任何帮助将不胜感激。我将温度传感器放在我的 raspberry pi 外壳内,以确保它不会变得太热。
我认为你的错误是在第二次测试中
else if (t>35)
你必须改成
else if (t>35 && t < 45)
如果没有此修复,温度为 50,lamp 将是橙色而不是红色
但是按照你程序中的逻辑,你忘记了30到35之间的测试,或者你出错了把30改成35或者35改成30
像这样尝试你的 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);
}
希望对您有所帮助!