組員:41111420 陳岷劭
一.介紹作品:
反應測試遊戲
這是一個基於 Arduino 的反應時間測試遊戲,通過點亮不同顏色的 LED 燈來測試玩家的反應速度。玩家需要根據 LED 燈的顏色快速按下對應的按鈕。系統會記錄玩家的反應時間並計算平均反應時間。
主要功能
顯示隨機顏色的 LED 燈:紅色、綠色或藍色 LED 燈隨機點亮。
按鈕響應:每個顏色的 LED 燈對應一個按鈕,玩家需要快速按下對應的按鈕。
反應時間記錄:系統會記錄每次的反應時間並在遊戲結束後顯示所有反應時間及平均反應時間。
二.程式碼:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int redPin = 2;
const int greenPin = 3;
const int bluePin = 4;
const int redButtonPin = 5;
const int greenButtonPin = 6;
const int blueButtonPin = 7;
const int startButtonPin = 8; // 啟動按鈕腳位
LiquidCrystal_I2C lcd(0x27, 16, 2); // 設定I2C
int correctColor;
int attempts = 0;
int reactionTimes[5];
const int debounceDelay = 50; // 去抖動延遲
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(redButtonPin, INPUT_PULLUP);
pinMode(greenButtonPin, INPUT_PULLUP);
pinMode(blueButtonPin, INPUT_PULLUP);
pinMode(startButtonPin, INPUT_PULLUP);
Serial.begin(9600);
lcd.init(); // 使用 init 初始化 LCD
lcd.backlight();
randomSeed(analogRead(0)); // 使亂數發生器
lcd.print("Press Start Btn");
}
void loop() {
Serial.println("Press the start button to begin the game...");
lcd.setCursor(0, 0);
lcd.print("Press Start Btn ");
while (digitalRead(startButtonPin) == HIGH) {
// 等待玩家按下啟動按鈕
}
delay(debounceDelay); // 去抖動處理
Serial.println("Game started!");
lcd.clear();
lcd.print("Game started!");
while (attempts < 5) {
delay(1000); // 間隔1秒開始每次遊戲
// 產生隨機等待時間(1-2
int waitTime = random(1000, 2000);
delay(waitTime);
// 產生隨機顏色
correctColor = random(0, 3); // 0:紅色, 1:綠色, 2:藍色
// 顯示隨機顏色
if (correctColor == 0) {
Serial.println("Red LED on");
lcd.setCursor(0, 1);
lcd.print("Red LED on ");
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
} else if (correctColor == 1) {
Serial.println("Green LED on");
lcd.setCursor(0, 1);
lcd.print("Green LED on ");
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, LOW);
} else if (correctColor == 2) {
Serial.println("yellow LED on");
lcd.setCursor(0, 1);
lcd.print("Blue LED on ");
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, HIGH);
}
// 等待玩家按下正確按鈕
int startTime = millis();
bool buttonPressed = false;
while (!buttonPressed) {
if (correctColor == 0 && digitalRead(redButtonPin) == LOW) {
if (digitalRead(redButtonPin) == LOW) {
digitalWrite(redPin, LOW);
buttonPressed = true;
Serial.println("Red button pressed");
}
} else if (correctColor == 1 && digitalRead(greenButtonPin) == LOW) {
if (digitalRead(greenButtonPin) == LOW) {
digitalWrite(greenPin, LOW);
buttonPressed = true;
Serial.println("Green button pressed");
}
} else if (correctColor == 2 && digitalRead(blueButtonPin) == LOW) {
if (digitalRead(blueButtonPin) == LOW) {
digitalWrite(bluePin, LOW);
buttonPressed = true;
Serial.println("Blue button pressed");
}
}
}
int reactionTime = millis() - startTime;
// 顯示反應時間並記錄到陣列中
Serial.println("Attempt " + String(attempts + 1) + " reaction time: " + String(reactionTime) + " milliseconds");
reactionTimes[attempts] = reactionTime;
// 重置LED並增加遊戲次數
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
attempts++;
}
Serial.println("Game Over");
lcd.clear();
lcd.print("Game Over");
// 顯示各次反應時間
Serial.println("Reaction times:");
for (int i = 0; i < 5; i++) {
Serial.print("Attempt " + String(i + 1) + ": ");
Serial.println(reactionTimes[i]);
}
// 計算並顯示平均反應時間
int totalReactionTime = 0;
for (int i = 0; i < 5; i++) {
totalReactionTime += reactionTimes[i];
}
float averageReactionTime = totalReactionTime / 5.0;
Serial.print("Average reaction time: ");
Serial.println(averageReactionTime);
lcd.setCursor(0, 1);
lcd.print("Avg: " + String(averageReactionTime) + " ms");
while(true){
// 遊戲結束
}
}
三.成品照片:
實際線路圖
讀出的秒數(單位厘秒)
三.結果心得:
這個遊戲讓我學會了如何使用 Arduino 控制 LED 和按鈕,並透過程式碼記錄和計算反應時間。在遊戲過程中,我體會到了硬體接線和軟體程式設計的有趣之處,同時也學到了如何排除故障和調試程式碼。這個項目不僅提高了我的編程技能,還增強了我的電子學知識。