倒數計時:預設倒數時間為 10 秒,可隨時根據需求調整時間。
時間調整模式:有「增加」與「減少」兩種模式,自由切換以快速設置所需時間。
暫停與恢復:倒數過程中,您可以按下按鈕暫停倒數,方便臨時處理其他事務。
快速重置:只需長按按鈕,即可重置計時器,快速回到初始狀態
結束 : 亮燈提醒
它我原本想說長按5秒能叫出小遊戲-小恐龍使在無聊的倒計時中有能玩的,很可惜的最後沒成功。
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// 初始化 LCD 顯示屏(地址為 0x27,16x2)
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int button1 = 2; // 按鈕 1 引腳
const int button2 = 3; // 按鈕 2 引腳
const int ledPin = 9; // LED 引腳
int countdownTime = 10; // 初始倒數時間
int remainingTime = 0; // 剩餘時間(支持調整和暫停)
bool countdownActive = false; // 倒數是否啟動
bool countdownPaused = false; // 是否暫停
bool addMode = true; // true = "增加時間", false = "減少時間"
// 長按相關變數
unsigned long button1PressStart = 0;
unsigned long button2PressStart = 0;
const unsigned long longPressTime = 2000; // 長按的閾值時間 (毫秒)
bool button1LongPressHandled = false;
bool button2LongPressHandled = false;
void setup() {
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("SmartCountdown"); // 顯示名稱
delay(2000); // 等待 2 秒
updateLCD();
}
void loop() {
// 處理按鈕 1(啟動/暫停或重置)
handleButtonPress(button1, button1PressStart, button1LongPressHandled, [] {
// 短按功能:暫停或恢復倒數
if (countdownActive) {
countdownPaused = !countdownPaused;
lcd.setCursor(0, 0);
lcd.print(countdownPaused ? "Paused " : "Resumed ");
delay(1000);
updateLCD();
} else {
startCountdown(); // 開始倒數
}
}, [] {
// 長按功能:重置計時器
countdownActive = false;
countdownPaused = false;
countdownTime = 10;
remainingTime = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Timer Reset!");
delay(1000);
updateLCD();
});
// 處理按鈕 2(調整時間或切換模式)
handleButtonPress(button2, button2PressStart, button2LongPressHandled, [] {
// 短按功能:增加或減少時間
if (addMode) {
countdownTime++;
} else {
countdownTime--;
if (countdownTime < 0) countdownTime = 0; // 防止負數
}
if (!countdownActive) {
updateLCD();
}
}, [] {
// 長按功能:切換模式
addMode = !addMode;
lcd.setCursor(0, 0);
lcd.print(addMode ? "Mode: Add Time " : "Mode: Sub Time ");
delay(1000);
updateLCD();
});
if (countdownActive && !countdownPaused) {
runCountdown();
}
}
// 按鈕處理函數
void handleButtonPress(int buttonPin, unsigned long &pressStart, bool &longPressHandled, void (*shortPressAction)(), void (*longPressAction)()) {
if (digitalRead(buttonPin) == LOW) {
if (pressStart == 0) {
pressStart = millis();
}
if (millis() - pressStart > longPressTime && !longPressHandled) {
longPressHandled = true;
longPressAction();
}
} else {
if (pressStart > 0 && !longPressHandled) {
shortPressAction();
}
pressStart = 0;
longPressHandled = false;
}
}
// 更新 LCD 顯示
void updateLCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set time:");
lcd.setCursor(0, 1);
lcd.print(countdownTime);
lcd.print(" sec ");
lcd.print(addMode ? "(+)" : "(-)");
}
// 倒數計時邏輯
void runCountdown() {
static unsigned long previousMillis = millis();
if (millis() - previousMillis >= 1000) {
previousMillis = millis();
if (remainingTime > 0) {
remainingTime--;
lcd.setCursor(0, 0);
lcd.print("Time left:");
lcd.setCursor(0, 1);
lcd.print(remainingTime);
lcd.print(" sec ");
} else {
countdownActive = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time's up!");
for (int i = 0; i < 5; i++) {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
updateLCD();
}
}
}
// 啟動倒數
void startCountdown() {
countdownActive = true;
countdownPaused = false;
remainingTime = countdownTime;
lcd.setCursor(0, 0);
lcd.print("Countdown...");
}
小恐龍死掉了