#include <Arduino.h>
// Pin Definitions
#define RGB_RED_PIN 6
#define RGB_GREEN_PIN 5
#define RGB_BLUE_PIN 8
#define START_STOP_BUTTON_PIN 2
#define DIFFICULTY_BUTTON_PIN 3
// Game Variables
int difficultyLevel = 0; // 0: Easy, 1: Medium, 2: Hard
unsigned long roundStartTime;
int wordsCorrect = 0;
bool gameActive = false;
const char* difficultyMessages[] = {"Easy mode on!", "Medium mode on!", "Hard mode on!"};
const int difficultyDelays[] = {3000, 2000, 1000}; // Delays in ms for Easy, Medium, Hard
unsigned long lastWordTime = 0;
String currentWord = "";
String wordList[] = {"hello", "world", "arduino", "race", "game","play", "code", "learn", "fun", "build"};
int numWords = sizeof(wordList) / sizeof(wordList[0]);
volatile bool buttonPressed = false;
volatile bool difficultyButtonPressed = false;
// Variables for Debouncing
unsigned long lastDebounceTimeStart = 0;
unsigned long lastDebounceTimeDifficulty = 0;
const unsigned long debounceDelay = 50; // 50ms debounce delay
// New variable for user input
String userInput = "";
// Function to Set RGB LED Color
void setRGBColor(int red, int green, int blue) {
analogWrite(RGB_RED_PIN, 255 - red);
analogWrite(RGB_GREEN_PIN, 255 - green);
analogWrite(RGB_BLUE_PIN, 255 - blue);
}
// Function to handle the start/stop button press with debouncing logic
void handleStartStop() {
unsigned long currentTime = millis();
if ((currentTime - lastDebounceTimeStart) > debounceDelay) {
buttonPressed = true;
lastDebounceTimeStart = currentTime;
}
}
// Function to handle the difficulty button press and cycle through difficulty levels
void handleDifficulty() {
unsigned long currentTime = millis();
if ((currentTime - lastDebounceTimeDifficulty) > debounceDelay) {
difficultyButtonPressed = true;
lastDebounceTimeDifficulty = currentTime;
}
}
// Start Countdown Sequence
void startCountdown() {
for (int i = 3; i > 0; i--) {
setRGBColor(255, 255, 255); delay(500); // LED White Blink
setRGBColor(0, 0, 0); delay(500);
Serial.println(i);
}
}
// Generate Random Word
void generateWord() {
int randomIndex = random(numWords);
currentWord = wordList[randomIndex];
Serial.println("Type the word: " + currentWord);
}
// Setup Code
void setup() {
Serial.begin(9600);
pinMode(RGB_RED_PIN, OUTPUT);
pinMode(RGB_GREEN_PIN, OUTPUT);
pinMode(RGB_BLUE_PIN, OUTPUT);
pinMode(START_STOP_BUTTON_PIN, INPUT_PULLUP);
pinMode(DIFFICULTY_BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(START_STOP_BUTTON_PIN), handleStartStop, FALLING);
attachInterrupt(digitalPinToInterrupt(DIFFICULTY_BUTTON_PIN), handleDifficulty, FALLING);
setRGBColor(255, 255, 255); // White for idle
Serial.println("Game in idle mode. Set difficulty and press start to begin.");
}
// Main Loop
void loop() {
// Handle Difficulty Button
if (difficultyButtonPressed && !gameActive) {
difficultyLevel = (difficultyLevel + 1) % 3;
Serial.println(difficultyMessages[difficultyLevel]);
difficultyButtonPressed = false;
}
// Handle Start/Stop Button
if (buttonPressed) {
buttonPressed = false;
if (!gameActive) {
gameActive = true;
startCountdown();
roundStartTime = millis();
setRGBColor(0, 255, 0); // Start with Green LED
generateWord();
} else {
gameActive = false;
Serial.print("Round ended. Words correct: ");
Serial.println(wordsCorrect);
wordsCorrect = 0;
setRGBColor(255, 255, 255); // Reset to idle color
}
}
// Game Active Logic
if (gameActive) {
if (millis() - roundStartTime >= 30000) {
// End Round after 30 seconds
gameActive = false;
Serial.print("Round ended. Words correct: ");
Serial.println(wordsCorrect);
wordsCorrect = 0;
setRGBColor(255, 255, 255); // Reset to idle color
}
// Word Typing Logic with Backspace Handling
if (Serial.available() > 0) {
char incomingChar = Serial.read();
if (incomingChar == '\b') {
// Handle backspace: remove last character if possible
if (userInput.length() > 0) {
userInput.remove(userInput.length() - 1);
Serial.print("\b \b"); // Remove character from display
}
} else if (incomingChar == '\n') {
// Check if input matches the current word
userInput.trim(); // Remove any extraneous whitespace
if (userInput == currentWord) {
setRGBColor(0, 255, 0); // Green for correct word
wordsCorrect++;
generateWord(); // Generate new word immediately
} else {
setRGBColor(255, 0, 0); // Red for incorrect word
}
userInput = ""; // Clear user input for next round
} else {
// Add character to input string if it’s not backspace or newline
userInput += incomingChar;
Serial.print(incomingChar); // Echo the character to the terminal
}
}
// Word Timeout Logic
if (millis() - lastWordTime >= difficultyDelays[difficultyLevel]) {
lastWordTime = millis();
generateWord();
}
}
}