漂流心得,一块板接二个LCD1602
注意跳线https://mc.dfrobot.com.cn/forum.php?mod=image&aid=34847&size=300x300&key=6dcb37975f441d24&nocache=yes&type=fixnonehttps://mc.dfrobot.com.cn/forum.php?mod=image&aid=34848&size=300x300&key=0018db91d71bd1ad&nocache=yes&type=fixnone#include <dht11.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20,16,2);//设置LCD的地址为0x20,可以设置2行,每行16个字符
LiquidCrystal_I2C mylcd(0x27,16,2);
dht11 DHT;
#define DHT11_PIN 3
float dist;
float checkdistance_4_7() {
digitalWrite(4, LOW);
delayMicroseconds(2);
digitalWrite(4, HIGH);
delayMicroseconds(10);
digitalWrite(4, LOW);
float distance = pulseIn(7, HIGH) / 58.00;
delay(10);
return distance;
}
void setup(){
dist = 0;
pinMode(4, OUTPUT);
pinMode(7, INPUT);
lcd.init(); // LCD初始化设置
lcd.backlight(); // 打开LCD背光
mylcd.init(); // LCD初始化设置
mylcd.backlight(); // 打开LCD背光
}
void loop(){
dist = checkdistance_4_7();
if (dist < 1000) {
mylcd.clear();
mylcd.setCursor(0, 0);
mylcd.print("Distance(cm):");
mylcd.setCursor(0, 1);
mylcd.print(dist);
delay(1000);
}
lcd.clear();
lcd.setCursor(0, 0);
Serial.println("Type,\tstatus,\tHumidity(%),\tTemperature(C)");
lcd.print("Humidity(%): "); //LCD屏显示” Humidity(%):”
lcd.setCursor(0, 1); //光标移到第2行,第一个字符
lcd.print("Temp(C): "); //LCD屏显示”Temp(C):”
int chk; //chk用于存储DHT11传感器的数据
Serial.print("DHT11, \t");
//读取DHT11传感器的数据
chk = DHT.read(DHT11_PIN);
switch (chk){
case DHTLIB_OK:
Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("Time out error,\t");
break;
default:
Serial.print("Unknown error,\t");
break;
}
//串口显示温湿度值
Serial.print(DHT.humidity,1);
Serial.print(",\t");
Serial.println(DHT.temperature,1);
//LCD显示温湿度值
lcd.setCursor(12, 0);
lcd.print(DHT.humidity,1);
lcd.setCursor(12, 1);
lcd.print(DHT.temperature,1);
delay(1000);
}
学习了!
页:
[1]