问题描述
我正在我的大学项目中,我必须测量温度并在16x2 LCD上显示温度,并根据温度切换冷却设备,还必须使用pH传感器并在其上显示其值16x2。目前,我正在使用以下代码进行温度和切换,但无法正常工作。它会在屏幕上显示温度,还会显示一些垃圾值,并且切换不正确。
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <dht.h>
dht DHT;
#define DHT11_PIN 7
LiquidCrystal_I2C lcd(0x27,2,1,4,5,6,7);
const int ledPin = 6;
void setup()
{
lcd.setBacklightPin(3,POSITIVE);
lcd.setBacklight(HIGH);
lcd.begin(16,2);
lcd.clear();
Serial.begin(9600);
for (int DigitalPin = 8; DigitalPin <= 8; DigitalPin++) {
pinMode(DigitalPin,OUTPUT);
}
}
void loop()
{
int chk = DHT.read11(DHT11_PIN);
float temp=(DHT.temperature);
float Hum=(DHT.humidity);
lcd.print("Temp C ");
lcd.setCursor(6,0);
lcd.print(temp);
lcd.setCursor(0,1);
lcd.println("Humid % ");
lcd.setCursor(6,1);
lcd.print(Hum);
delay(1000);
if (temp <= 29)
digitalWrite(8,LOW);
else if (temp>30)
digitalWrite(8,HIGH);
}
解决方法
我将答案分为两部分:
- 硬件:
请描述您的硬件配置,以缩小问题范围:如何切换继电器?它是光耦合的吗?您正在使用晶体管吗?如果是这样,是什么类型?您的继电器有反向二极管保护吗? (不要不将继电器直接连接到Arduino引脚)
- 代码
2.1。您的setup()中的此for循环不正确:
for (int DigitalPin = 8; DigitalPin <= 8; DigitalPin++) {
pinMode(DigitalPin,OUTPUT);
}
您的索引从8到8,因此不需要for循环即可将单个引脚声明为输出。
2.2。通常,您可以将输出引脚分配给变量,也可以像对DHT一样将其分配为宏。保持一致。此外,您的代码还需要整理一下:
a。用所有库调用开始代码
#include <Wire.h> // The Wire.h library is already called by the LiquidCrystal_I2C library,so you don't need to call it if you use the I2C one
#include <LCD.h> // You are using the LCD library...
#include <LiquidCrystal_I2C.h> // but also the I2C lcd library. You don't need both.
#include <dht.h> // Is this the latest version? Use Adafruit's
b。尝试在开始时对所有宏进行分组。在此处声明大头针或常量:
// Using macros
#define DHT11_PIN 7
#define DigitalPin 8
#define DHT11_PIN 7
或对引脚使用常量。选择一个,而不是全部选择:
// Group all your const int together,for the sake of clarity
const int DHT_PIN = 7;
const int DigitalPin = 8;
const int ledPin = 6;
c。我相信这个构造函数对于您选择的库是不正确的。检查this库中的示例(假设它是相同的,我相信是这样)
LiquidCrystal_I2C lcd(0x27,2,1,4,5,6,7);
尝试使用此构造函数,而不是:
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27,16,2);
d。 setup()
函数也需要进行一些更改:
void setup()
{
// You don't seem to be using the serial library. However,if you use it,it is
// preferable to start it first.
Serial.begin(9600);
// LCD configuration
lcd.begin(16,2); // Always call the begin() method first
lcd.setBacklightPin(3,POSITIVE); // Then,use additional configuration methods.
lcd.setBacklight(HIGH);
lcd.clear();
// Once again,please check if you are using the right library.
// It might be the cause of the garbage you see.
// Initialize the type of pin
pinMode(DigitalPin,OUTPUT);
}
e。根据Adafruit的library(请参见示例),您缺少用于声明所用DHT传感器类型的宏:
#define DHTTYPE DHT11
f。在您的main loop()中,您正在读取带有一些奇怪括号的值:
int chk = DHT.read11(DHT11_PIN);
float temp=(DHT.temperature);
float Hum=(DHT.humidity);
根据最新的库,它应该是:
// The latest library does not have this prototype: int chk = DHT.read11(DHT11_PIN); What is it for in your dht lib?
float temp = DHT.readTemperature(); // instead of: float temp=(DHT.temperature); --> which should be DHT.temperature(); in any case
float hum = DHT.readHumidity(); // instead of: float Hum=(DHT.humidity);
TL; DR:共享硬件以为您提供帮助。更新您的库,清理代码,并使用最新库中的正确方法。
,非常感谢您的指导,以下是我的工作代码:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define DHTPIN 7 //digital pin for sensor
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN,DHTTYPE);
LiquidCrystal_I2C lcd(0x27,7,3,POSITIVE);
void setup()
{
pinMode(8,OUTPUT); //for configuring relay output
lcd.begin(16,2);
dht.begin();
}
void loop() {
delay(1000);
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f))
{
lcd.print("NO CONNECTION");
return;
}
//Hum code
lcd.setCursor(0,0);
lcd.print("Hum:");
lcd.print(h);
lcd.print(" % ");
//Temp code
lcd.setCursor(0,1);
lcd.print("Temp:");
lcd.print(t);
lcd.print(" C");
//To operate relay
{
if (t <= 26)
digitalWrite(8,LOW);
else if (t>27)
digitalWrite(8,HIGH);
}
}