Arduino应用开发:第十章代码及练习答案

该代码示例展示了如何使用Arduino与LCD1602显示器及DHT11温湿度传感器进行交互。程序初始化后,LCD1602上会显示温度和湿度信息,并滚动显示。同时,根据计算出的热指数,程序会通过LED和蜂鸣器发出不同警报,提示环境是否处于安全、不适或危险范围。
摘要由CSDN通过智能技术生成

编号:simple_LCD1602

[code]
#include <LiquidCrystal.h>
const int rs = 12, en = 10, d4 = 6, d5 = 5, d6 = 4, d7 = 3;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  // put your setup code here, to run once:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, world!");
  delay(3000);  
}

void loop() {
  // put your main code here, to run repeatedly:
  for(int a=1; a<=9; a++){
      for(int b=1; b<=9; b++){
        lcd.setCursor(0,1);
        lcd.print(a);
        lcd.print('*');
        lcd.print(b);
        lcd.print('=');
        lcd.print(a*b);
        delay(1000);
      }
      lcd.clear();
  }    
}
[/code]

=========================================================================

编号:tmp_hm_LCD1602

[code]
#include "DHT.h"
#define DHTPIN 7       //DHT11传感器的数据管脚与UNO7号管脚相连
#define DHTTYPE DHT11  //定义传感器类型为DHT11

#include <LiquidCrystal.h>
const int rs = 12, en = 10, d4 = 6, d5 = 5, d6 = 4, d7 = 3; //定义液晶屏各个管脚

#define ALARM_LED 8  //定义LED的连接管脚为8号
#define BEEP      9  //定义蜂鸣器的连接管脚为9号

DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

float temp, humd;   //定义温度和湿度变量
float htIndex;      //热指数

void setup() {
  // put your setup code here, to run once:
  pinMode(ALARM_LED, OUTPUT);
  pinMode(BEEP, OUTPUT);
  dht.begin();
  lcd.begin(16, 2);
  lcd.setCursor(2,0);
  lcd.print("TEMPERAUTRE & ");
  lcd.setCursor(0,1);
  lcd.print("HUMIDITY MONITOR");
  delay(3000);
}

void loop() {
  // put your main code here, to run repeatedly:
  lcd.setCursor(0,0);
  lcd.clear();
  temp = dht.readTemperature();  //读取温度值
  humd = dht.readHumidity();     //读取湿度值
  lcd.print("     Temperature=");
  lcd.print(temp, 1);
  lcd.write(0B11011111);  
  lcd.write(0B01000011);         //显示摄氏度符号
  lcd.print(" ");
  lcd.print("Humidity=");
  lcd.print(humd, 1);  
  lcd.print('%');
  for(int i=0; i<24; i++){
      lcd.scrollDisplayLeft();
      delay(500);
  }
  
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("T:");
  lcd.print(temp,1);
  lcd.write(0B11011111);  
  lcd.write(0B01000011);         //显示摄氏度符号
  lcd.print(" ");
  lcd.print("H:");
  lcd.print(humd, 1);  
  lcd.print('%');
  delay(3000);
  
  htIndex = dht.computeHeatIndex();
  if(htIndex <=80){   //安全范围内
      digitalWrite(ALARM_LED, LOW);
      digitalWrite(BEEP, LOW);
      lcd.setCursor(0,1);
      lcd.print("HtIndx=");
      lcd.print(htIndex,0);
      lcd.print(" :SAFE");
  }
  else if(htIndex >80 && htIndex <= 101){   //不适范围内
      digitalWrite(ALARM_LED, HIGH);
      digitalWrite(BEEP, LOW);
      lcd.setCursor(0,1);
      lcd.print("HtIndx=");
      lcd.print(htIndex,0);
      lcd.print(" :WARN");
  }
  else if(htIndex > 101){    //危险范围内
      digitalWrite(ALARM_LED, HIGH);
      digitalWrite(BEEP, HIGH);
      lcd.setCursor(0,1);
      lcd.print("HtIndx=");
      lcd.print(htIndex,0);
      lcd.print(":DANGER");
  }
  delay(3000);
}
[/code]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值