簡體   English   中英

Arduino,顯示時間,溫度和濕度

[英]Arduino, displaying time, temperature and the humidity

我正在Arduino Uno上進行數字時鍾項目。 我被要求做的是制作一個不使用RTC模塊就可以在LCD上顯示的數字時鍾。 但是我也想添加一些東西,例如DHT11溫度和濕度傳感器。 我想在第一行顯示TIME:HH:MM:SS,在第二行顯示TEMP(* C):XX.YY,等待片刻,清除第二行(我使用空白空格),然后顯示HUM(%):XX.YY。 等等。 我只是想將第二行延遲2秒,同時從溫度更改為濕度以清楚地看到溫度和濕度值。 但是它延遲了整個系統,時間值也浪費了很多延遲時間。 我只希望時鍾一直顯示,並分別顯示溫度和濕度。 這是我的代碼如下:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <dht.h>
#define dht_apin   2
LiquidCrystal_I2C lcd(0x27, 16, 2);
dht DHT;  
int h=14; //hour
int m=50; //minute
int s=20; //second
int flag; 
int TIME; 
const int hs=8; 
const int ms=9; 
void setup()
{
lcd.begin();
lcd.backlight();
}

void loop() 
{ 
delay(1000);
lcd.setCursor(0,0); 
s=s+1;  
lcd.print("TIME:");
lcd.print(h); 
lcd.print(":"); 
lcd.print(m); 
lcd.print(":"); 
lcd.print(s);     
delay(400);
if(flag==24)flag=0; 
delay(1000); 
lcd.clear(); 
if(s==60){ 
 s=0; 
 m=m+1; 
} 
if(m==60) 
{ 
 m=0; 
 h=h+1; 
 flag=flag+1; 
}        

//Temperature and humidity
DHT.read11(dht_apin);
lcd.setCursor(0,1);
lcd.print("TEMP(*C):");  
lcd.print(DHT.temperature);
delay(2000);
lcd.setCursor(0,1);
lcd.print(' '); 
lcd.setCursor(0,1);
lcd.print("HUM(%):"); 
lcd.print(DHT.humidity); 
delay(1000); 
} 

謝謝!

正如DigitalNinja所說, delay阻礙了一切。 如果要保持運行狀態,則必須使用中斷。

中斷停止執行代碼以跳轉到循環外的特定功能。 它不會像delay一樣阻塞時鍾。 一切都在運行,但您基本上只是在需要時根據外部事件(引腳上升,經過的時間...)插入函數。

您可以使用Timer1庫每X秒執行一次操作。 或使用attachInterrupt創建自己的系統。

我在TimerOne參考頁中采用了此示例,並對其進行了評論

void setup()
{
  // Your variables here
  // ...

  Timer1.initialize(500000);         // initialize timer1, and set a 1/2 second period
  Timer1.attachInterrupt(callback);  // attaches callback() as the function to call when Timer is done
}

void callback()
{
  // Do your thing when Timer is elapsed
  // The timer goes automatically to zero. You don't need to reset it.
}

void loop()
{
  // your program here...
  // You never need to call callback() function, it is called WHEN Timer is done
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM