计算秒数不起作用

标签 c counting seconds

好吧,所以我写了这段代码,据我所知,它应该输出 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/

相关文章:

Python 计算变量类型列表中的字符串数,如果可能则不循环

python - 将 python 2.7 timedelta.total_seconds() 转换为 python 2.6 中的等效项

c - 如何从C函数中返回字符数组

classification - 我将如何计算数组中每个字母数字的数量? (APL)

c - 在 C 中将字符串分配给 char 时会发生什么

excel - 在 Excel 中使用 Len 函数定义条件时出现问题

jquery计时器剩余写入时间

python - 如何每 4 秒更新一次 tkinter 窗口?

c - 64位除法

c - 如何使用 C 语言的 fork() 并行调用子程序