[英]Arduino LCD Timer Display
我一直在嘗試用 Arduino 制作一個計時器,它將在 2 小時后停止並關閉蜂鳴器 go。 我已經成功地增加了計時器的計數秒數,但現在我也嘗試讓它顯示小時和分鍾並關閉蜂鳴器 go,感謝所有幫助,這是項目的代碼和圖片:
#include <LiquidCrystal.h>
LiquidCrystal lcd(1,2,4,5,6,7); // setup the lcd
#define button 3
// setup integers //
int timerMode=0;
int buzzer=8;
int wait=400;
int longWait=5000;
int startTime;
int hours=(millis() - startTime) / 360000;
void setup() {
lcd.begin(16,2); // start lcd //
lcd.clear(); // clear old text //
pinMode(button, INPUT_PULLUP); // make button an input //
pinMode(buzzer, OUTPUT); // make buzzer an output //
lcd.print("Ergo timer"); // print begin text //
}
void loop() {
lcd.setCursor(0,1);
if (digitalRead(button) == LOW){ // if button is pressed//
startTime=millis();
timerMode++;
delay(wait);
}
if (timerMode==1){ // if timermode is 1 after a button press //
lcd.setCursor(0,0);
lcd.print("Over 2 uur pause"); // print top text //
lcd.setCursor(0,5);
lcd.print( (millis() - startTime) / 1000); // print time in seconds //
lcd.setCursor(0,4);
lcd.print(":"); // print a column between //
lcd.setCursor(0,3);
lcd.print( (millis() - startTime) / 60000); // print time in minutes //
lcd.setCursor(0,2);
lcd.print(":");
lcd. setCursor(0,1);
lcd.print( (millis() - startTime) / 360000); // print time in hours //
}
if (hours == 2){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("---Nu pause---");
tone(buzzer,200,1000);
}
if (timerMode > 1){
delay(longWait);
timerMode=0;
lcd.clear();
lcd.print("---Ergo timer---");
}
int hours=(millis() - startTime) / 360000;
是廢話。 C++代碼從上到下執行。 每當某些變量發生變化時,程序不會神奇地重新訪問以前執行的行。
這意味着這條線不會創建一些神奇的公式,只要有什么變化就會更新hours
。 它只是創建一行代碼,在啟動時執行一次,當startTime
未初始化時,如果定時器外圍硬件尚未設置, millis()
可能只會產生無意義。
您需要將此代碼移動到主循環中。 由於這是一個嵌入式系統,您還應該放棄所有天真的“原始數據類型”,例如int
以支持stdint.h
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.