在这记录minecraft的点点滴滴。

作者:linfengfeiwu2018-4-25 21:08分类: 笔记 标签: Arduino

又是一个版本改进了,这次添加了DHT11温湿度传感器,来显示实时温湿度。到此,这个时钟基本已经比较完美了。

DHT11接法和库文件见:https://blog.readgroup.cn/post/55

其他的库文件见:https://blog.readgroup.cn/post/70

直接上代码:

#include <Wire.h>
#include <ds3231.h>
#include <LiquidCrystal_I2C_DS3231.h>
#define BUFF_MAX 128

//DHT11 Sensor:
#include "DHT.h"
#define DHTPIN 12     // what digital pin we're connected to
#define DHTTYPE DHT11   // DHT 11

DHT dht(DHTPIN, DHTTYPE);

//****************************************Define I2C LCD Display *********************************
#define I2C_ADDR    0x27  // Define I2C Address for the PCF8574T 
//---(Following are the PCF8574 pin assignments to LCD connections )----
// This are different than earlier/different I2C LCD displays

#define Rs_pin  0
#define Rw_pin  1
#define En_pin  2
#define BACKLIGHT_PIN  3
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

#define  LED_OFF  0
#define  LED_ON  1

/*-----( Declare objects )-----*/  
LiquidCrystal_I2C  lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
//************************************ END LCD DISPLAY *******************************************
//摄氏度
byte Nian[8] = {0b11000,0b11011,0b00100, 0b00100, 0b00100, 0b00100, 0b00011, 0b00000};
//月
byte Yue[8] = {0b01111, 0b01001, 0b01111, 0b01001, 0b01111, 0b01001, 0b10011, 0b00001};
//日
byte Ri[8] = {0b01111, 0b01001, 0b01001, 0b01111, 0b01001, 0b01001, 0b01111, 0b00000};

uint8_t time[8];
char recv[BUFF_MAX];
unsigned int recv_size = 0;
unsigned long prev, interval = 1000;

void setup()
{
    Serial.begin(9600);
    Wire.begin();
    DS3231_init(DS3231_INTCN);
    memset(recv, 0, BUFF_MAX);
    Serial.println("GET time");

  //**************************LCD Setup********************************
   lcd.begin (16,2);  // initialize the lcd 
   // Switch on the backlight
   lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
   lcd.setBacklight(LED_ON);
  //***************************END LCD Setup********************************
    
    Serial.println("Setting time");
   // setTheTime("304022129022016");     // ssmmhhWDDMMYYYY set time once in the given format
    lcd.createChar(0, Nian);
    lcd.createChar(1, Yue);
    lcd.createChar(2, Ri);
    dht.begin();
}

void loop()
{
    int hhhh = dht.readHumidity();
  int tttt = dht.readTemperature();
    char tempF[6]; 
    float temperature;
    char buff[BUFF_MAX];
    unsigned long now = millis();
    struct ts t;
    // show time once in a while
    if (now - prev > interval){
        DS3231_get(&t); //Get time
        temperature = DS3231_get_treg(); //Get temperature
        dtostrf(temperature, 5, 1, tempF);

        lcd.clear();
        lcd.setCursor(0,0);
        
        if(t.mon<10)
        {
          lcd.print("0");
        }
        lcd.print(t.mon);
         lcd.write(byte(1));
        if(t.mday<10)
        {
          lcd.print("0");
        }
        lcd.print(t.mday);
         lcd.write(byte(2));
        lcd.print(" ");
        lcd.print(t.wday);  
        lcd.print(" ");   
        lcd.print(tttt);
        lcd.write(byte(0));
        lcd.print(" "); 
        lcd.print(hhhh);
        lcd.print("%");
        
        lcd.setCursor(0,1); //Go to second line of the LCD Screen
         if(t.hour<10)
        {
          lcd.print("0");
        }
        lcd.print(t.hour);
        lcd.print(":");
        if(t.min<10)
        {
          lcd.print("0");
        }
        lcd.print(t.min);
        lcd.print(":");
        if(t.sec<10)
        {
          lcd.print("0");
        }
        lcd.print(t.sec);
        
        lcd.print(" LOVE ");//这里用来显示自定义字符,这行不要可以显示DS3231的温度
        lcd.print(tempF);
        lcd.print((char)223);
        lcd.print("C");
        prev = now;
    }

}

void setTheTime(char *cmd)
{
    struct ts t;

    // ssmmhhWDDMMYYYY  set time

        t.sec = inp2toi(cmd, 0);
        t.min = inp2toi(cmd, 2);
        t.hour = inp2toi(cmd, 4);
        t.wday = inp2toi(cmd, 6);
        t.mday = inp2toi(cmd, 7);
        t.mon = inp2toi(cmd, 9);
        t.year = inp2toi(cmd, 11) * 100 + inp2toi(cmd, 13);
        DS3231_set(t);
        Serial.println("OK");
}


void printWeek(int week)
{
  switch(week)
  {
    case 1: lcd.print("Mon");break;
    case 2: lcd.print("Tue");break;
    case 3: lcd.print("Wed");break;
    case 4: lcd.print("Thu");break;
    case 5: lcd.print("Fri");break;
    case 6: lcd.print("Sat");break;
    case 7: lcd.print("Sun");break;
    default: lcd.print(" E ");break;
  } 
}
效果图:

QQ截图20180425210830.png

温馨提示如有转载或引用以上内容之必要,敬请将本文链接作为出处标注,谢谢合作!
et_highlighter51

已有 0/2673 人参与

发表评论:

欢迎关注本站微信公众号~