const int segments[] = {2, 3, 4, 5, 6, 7, 8, 9}; // 七段顯示器的 a~dot 接腳
const int digits[] = {A0, A1, A2, A3, 10, 11, 12, 13}; // 顯示器的共陰極 COM1~COM8
const int pauseButtonPin = A4; // 暫停按鈕接腳
const int resetButtonPin = A5; // 重置按鈕接腳
const int modeButtonPin = 9; // 模式切換按鈕接腳
// 定義數字對應的編碼 (共陰極)
const byte numbers[10] = {
0x3F, // 0 - 0111111
0x06, // 1 - 0000110
0x5B, // 2 - 1011011
0x4F, // 3 - 1001111
0x66, // 4 - 1100110
0x6D, // 5 - 1101101
0x7D, // 6 - 1111101
0x07, // 7 - 0000111
0x7F, // 8 - 1111111
0x6F // 9 - 1101111
};
unsigned long displayValue = 0; // 要顯示的數值
unsigned long previousMillis = 0; // 上次更新的時間
unsigned long flashPreviousMillis = 0; // 閃爍的上次更新時間
const long interval = 300; // 更新間隔 (0.3秒)
const long flashInterval = 500; // 閃爍間隔 (0.5秒)
bool isPaused = true; // 初始化為暫停狀態
bool isFlashOn = true; // 初始化閃爍狀態
bool isCountingUp = true; // 初始化為升數模式
int pauseButtonState = 0; // 暫停按鈕狀態
int lastPauseButtonState = 0; // 上一次的暫停按鈕狀態
int resetButtonState = 0; // 重置按鈕狀態
int lastResetButtonState = 0; // 上一次的重置按鈕狀態
int modeButtonState = 0; // 模式切換按鈕狀態
int lastModeButtonState = 0; // 上一次的模式切換按鈕狀態
void setup() {
for (int i = 0; i < 8; i++) {
pinMode(segments[i], OUTPUT);
}
for (int i = 0; i < 8; i++) {
pinMode(digits[i], OUTPUT);
digitalWrite(digits[i], HIGH); // 初始化為高電平
}
pinMode(pauseButtonPin, INPUT_PULLUP); // 設置暫停按鈕為輸入並啟用內建上拉電阻
pinMode(resetButtonPin, INPUT_PULLUP); // 設置重置按鈕為輸入並啟用內建上拉電阻
pinMode(modeButtonPin, INPUT_PULLUP); // 設置模式切換按鈕為輸入並啟用內建上拉電阻
}
// 顯示數字的函數
void displayNumber(int number, int position, bool flash) {
if (flash) {
for (int i = 0; i < 8; i++) {
digitalWrite(segments[i], (numbers[number] >> i) & 0x01 ? 0 : 1);
}
} else {
for (int i = 0; i < 8; i++) {
digitalWrite(segments[i], HIGH); // 關閉所有段
}
}
digitalWrite(digits[position], 1); // 選擇位置
delay(5); // 短暫延遲
digitalWrite(digits[position], 0); // 關閉位置
}
void loop() {
unsigned long currentMillis = millis();
// 檢查暫停按鈕狀態
pauseButtonState = digitalRead(pauseButtonPin);
if (pauseButtonState != lastPauseButtonState) {
if (pauseButtonState == LOW) { // 內建上拉電阻,所以按鈕按下時為 LOW
isPaused = !isPaused; // 切換暫停狀態
}
delay(50); // 消除按鈕抖動
}
lastPauseButtonState = pauseButtonState;
// 檢查重置按鈕狀態
resetButtonState = digitalRead(resetButtonPin);
if (resetButtonState != lastResetButtonState) {
if (resetButtonState == LOW) { // 內建上拉電阻,所以按鈕按下時為 LOW
if (isCountingUp) {
displayValue = 0; // 升數模式時重置數值為 0
} else {
displayValue = 99999999; // 倒數模式時重置數值為 99999999
}
isPaused = true; // 暫停計數
}
delay(50); // 消除按鈕抖動
}
lastResetButtonState = resetButtonState;
// 檢查模式切換按鈕狀態
modeButtonState = digitalRead(modeButtonPin);
if (modeButtonState != lastModeButtonState) {
if (modeButtonState == LOW) { // 內建上拉電阻,所以按鈕按下時為 LOW
isCountingUp = !isCountingUp; // 切換升數/倒數模式
if (!isCountingUp) {
displayValue = 99999999; // 切換到倒數模式時,從 99999999 開始
} else {
displayValue = 0; // 切換到升數模式時,從 0 開始
}
isPaused = true; // 暫停計數
}
delay(50); // 消除按鈕抖動
}
lastModeButtonState = modeButtonState;
// 如果未暫停,繼續計數
if (!isPaused) {
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (isCountingUp) {
displayValue++;
if (displayValue == 100000000) { // 8位數顯示的最大值
displayValue = 0;
}
} else {
if (displayValue > 0) {
displayValue--;
} else {
displayValue = 0; // 保持顯示為0
isPaused = true; // 當倒數到0時暫停計數
}
}
}
}
// 取得數字的每一位
int digitsArray[8];
unsigned long tempValue = displayValue;
for (int i = 7; i >= 0; i--) {
digitsArray[i] = tempValue % 10;
tempValue /= 10;
}
// 閃爍功能
if (isPaused) {
if (currentMillis - flashPreviousMillis >= flashInterval) {
flashPreviousMillis = currentMillis;
isFlashOn = !isFlashOn;
}
} else {
isFlashOn = true;
}
// 顯示八位數字(閃爍效果)
for (int i = 0; i < 8; i++) {
displayNumber(digitsArray[i], i, isFlashOn);
}
}