【问题标题】:Counting seconds doesn't work数秒不起作用
【发布时间】:2016-09-04 05:54:22
【问题描述】:

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

【问题讨论】:

  • 如果您为编程语言添加标签可能会有所帮助 (c?)

标签: c counting seconds


【解决方案1】:

改变

if(currentMillis = nextMillis) // here is nextMillis is assigned to currentMillis 

 if(currentMillis == nextMillis) // here is nextMillis is compared with currentMillis on equality

【讨论】:

  • 谢谢!我还忘记了两件事:1)使“打印”为假,2)清除液晶显示器。
猜你喜欢
  • 2015-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-23
  • 2015-02-06
  • 2016-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多