#include <LiquidCrystal.h> // 定义LCD引脚 LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // 定义按键引脚 const int K1 = 6; const int K2 = 7; const int K3 = 8; // 定义状态变量 bool isRunning = false; bool isPaused = false; int currentNumber = 0; // 定义时间间隔 const unsigned long interval = 500; unsigned long previousMillis = 0; void setup() { // 初始化LCD lcd.begin(16, 2); lcd.print("Press K1 to start"); // 初始化按键引脚为输入模式 pinMode(K1, INPUT_PULLUP); pinMode(K2, INPUT_PULLUP); pinMode(K3, INPUT_PULLUP); } void loop() { // 检测按键状态 checkButtons(); if (isRunning && !isPaused) { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; // 更新显示数字 updateDisplay(); } } } void checkButtons() { if (digitalRead(K1) == LOW) { delay(20); // 消抖 if (digitalRead(K1) == LOW) { isRunning = true; isPaused = false; while (digitalRead(K1) == LOW); // 等待按键释放 } } if (digitalRead(K2) == LOW) { delay(20); // 消抖 if (digitalRead(K2) == LOW) { isPaused = !isPaused; while (digitalRead(K2) == LOW); // 等待按键释放 } } if (digitalRead(K3) == LOW) { delay(20); // 消抖 if (digitalRead(K3) == LOW) { isRunning = false; isPaused = false; currentNumber = 0; updateDisplay(); while (digitalRead(K3) == LOW); // 等待按键释放 } } } void updateDisplay() { lcd.clear(); lcd.print(currentNumber); currentNumber = (currentNumber + 1) % 13; } |