请问如何让这个输出自动补零-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1507|回复: 1

[未解决] 请问如何让这个输出自动补零

[复制链接]
回帖奖励 25 金币 回复本帖可获得 5 金币奖励! 每人限 1 次(中奖概率 80%)
发表于 2021-2-26 17:21 | 显示全部楼层 |阅读模式
在时间是个位的时候会有显示bug的情况

#include <DHT.h>
#include <DHT_U.h>
#include <LiquidCrystal.h>
#include <ThreeWire.h>  
#include <RtcDS1302.h>
#include <SPI.h>
#include <SD.h>

#define DHTTYPE DHT11
#define DHTPIN 40
#define DBS1302CLK 25
#define DBS1302DAT 26
#define DBS1302RST 27

float Temp,RH,RTemp;
String WriteData;
byte CelsiusDat[8] =
  {
  B01100,
  B10010,
  B10010,
  B01100,
  B00000,
  B00000,
  B00000,
  B00000
  };

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
DHT dht1(DHTPIN,DHTTYPE);
ThreeWire DBS1302(DBS1302DAT,DBS1302CLK,DBS1302RST);
RtcDS1302<ThreeWire> Rtc(DBS1302);
File Data;

void setup()
{
        delay(500);
        Serial.begin(115200);  

  lcd.createChar(1,CelsiusDat);
  lcd.begin(16,2);  
  lcd.setCursor(9,1);
  lcd.print("bySheng");
  lcd.setCursor(5,0);
  lcd.print('%');
  lcd.setCursor(14,0);
  lcd.write(1);
  lcd.setCursor(15,0);
  lcd.print('C');



  dht1.begin();

  SD.begin(4);
  Data = SD.open("Data.txt",FILE_WRITE);

  Rtc.Begin();

}

void loop()
{
  delay(80);
  RtcDateTime now = Rtc.GetDateTime();
  RH = dht1.readHumidity();
  Temp = dht1.readTemperature();
  RTemp = dht1.computeHeatIndex(Temp,RH,false);

  Data = SD.open("Data.txt",FILE_WRITE);
  delay(200);

  lcd.setCursor(0,0);
  lcd.print(RH);
  lcd.setCursor(9,0);
  lcd.print(Temp);
  lcd.setCursor(0,1);
  lcd.print(now.Hour());
  lcd.setCursor(2,1);
  lcd.print(':');
  lcd.setCursor(4,1);
  lcd.print(now.Minute());
  lcd.setCursor(5,1);
  lcd.print(':');
  lcd.setCursor(6,1);
  lcd.print(now.Second());

  Data.print(now.Year());
  Data.print('/');
  Data.print(now.Month());
  Data.print('/');
  Data.print(now.Day());
  Data.print(' ');
  Data.print(now.Hour());
  Data.print(':');
  Data.print(now.Minute());
  Data.print(':');
  Data.print(now.Second());
  Data.print(',');

  Data.print(Temp);
  Data.print(',');
  Data.print(RH);
  Data.print(',');
  Data.println(RTemp);
  Data.close();
}

 楼主| 发表于 2021-2-26 17:52 | 显示全部楼层
已解决

void FormatDisplay(int num){   
    if(num < 10){
        lcd.print("0");
    }   
    lcd.print(num);   
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-29 13:42 , Processed in 0.069125 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表