好吧,所以我写了这段代码,据我所知,它应该输出 HMS 时间,但它只输出 00:00:00 比一秒长得多。我没有尝试等待超过几个小时来查看它是否发生变化。 我知道这不是硬件问题,因为我的液晶显示屏工作正常并且 我没有任何其他东西连接到我的arduino。 如果你想知道,我尝试过将“_time”函数“over”循环。
这是代码:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int hour;
int minute;
int seconds = -1;
boolean printed = false;
boolean secCounted = false;
unsigned long nextMillis = 1000;
void setup() {
lcd.begin(16, 2);
}
void loop() {
unsigned long currentMillis = millis();
if(currentMillis = nextMillis) _time(), nextMillis += 1000;
}
void _time(){
seconds += 1;
if(seconds == 60) seconds = 0, minute += 1;
if(minute == 60 && hour <= 12) minute = 0, hour += 1;
if(printed == false){
if(hour == 0) lcd.print("00");
if(hour != 0 && hour < 10) {lcd.print("0");
lcd.print(hour);}
if(hour > 9) lcd.print(hour);
lcd.print(":");
if(minute == 0) lcd.print("00");
if(minute != 0 && minute < 10) {lcd.print("0");
lcd.print(minute);}
if(minute > 9) lcd.print(minute);
lcd.print(":");
if(seconds == 0) lcd.print("00");
if(seconds != 0 && seconds < 10) {lcd.print("0");
lcd.print(seconds);}
if(seconds > 9) lcd.print(seconds);
printed = true;
}
}
最佳答案
改变
if(currentMillis = nextMillis) // here is nextMillis is assigned to currentMillis
上
if(currentMillis == nextMillis) // here is nextMillis is compared with currentMillis on equality
关于计算秒数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37118442/