// Pin assignments
const int joystickXPin = A0; // Analog pin for X-axis of the joystick
const int joystickYPin = A1; // Analog pin for Y-axis of the joystick
const int joystickButtonPin = 2; // Digital pin for the joystick button
const int buzzerPin = 4; // Digital pin for the buzzer
// RGB LED pins
const int ledPins[3] = {3, 5, 6}; // Digital pins for Red, Green, and Blue colors of the RGB LED
// Game configuration
const int maxSequence = 10; // Maximum length of the sequence to be memorized in the game
int gameSequence[maxSequence]; // Array to store the random sequence for the game
int sequenceLength = 0; // Tracks the current length of the sequence
bool gameStarted = false; // Flag to check if the game has started
// Joystick configuration
const int deadzone = 100; // Deadzone for joystick sensitivity adjustment
// Arduino's setup function
void setup() {
// Initialize joystick and button pins
pinMode(joystickXPin, INPUT);
pinMode(joystickYPin, INPUT);
pinMode(joystickButtonPin, INPUT_PULLUP);
// Initialize RGB LED pins
for (int i = 0; i < 3; i++) {
pinMode(ledPins[i], OUTPUT);
}
// Initialize buzzer pin
pinMode(buzzerPin, OUTPUT);
// Begin Serial communication for debugging and instructions
Serial.begin(9600);
Serial.println("Welcome to the Memory Game!");
Serial.println("Press the joystick button to start.");
startFlickering();
}
// Arduino's main loop function
void loop() {
// Flicker LEDs until the game starts
// Continuously flicker LEDs until the game starts
while (!gameStarted) {
startFlickering(); // Flicker LEDs with random colors
// Check for the joystick button press
if (digitalRead(joystickButtonPin) == LOW) {
while (digitalRead(joystickButtonPin) == LOW); // Wait for button release
delay(200); // Debounce delay
// Stop flickering and turn off all LEDs
turnOffAllLeds();
// Wait for 2 seconds before starting the game
delay(2000);
// Start the game
startGame();
}
}
// Check for game start
if (!gameStarted && digitalRead(joystickButtonPin) == LOW) {
while (digitalRead(joystickButtonPin) == LOW); // Wait for button release
delay(200); // Debounce delay
startGame(); // Start the game
}
// Game logic when started
if (gameStarted) {
playSequence(); // Play the current sequence using LEDs
for (int currentStep = 0; currentStep < sequenceLength; currentStep++) {
int playerInput = getPlayerInput(); // Get player's input
if (playerInput != gameSequence[currentStep]) {
playErrorSound(); // Play error sound on wrong sequence
Serial.println("Wrong sequence! Game over.");
gameStarted = false; // End the game
return;
}
playSuccessSound(); // Play success sound on correct sequence
}
// Check for game completion
if (sequenceLength < maxSequence) {
Serial.println("Correct! Next sequence:");
sequenceLength++;
delay(1000); // Wait before the next sequence
} else {
Serial.println("Congratulations! You've won the game!");
gameStarted = false; // End the game after winning
}
}
}
void startFlickering() {
int randomLED = random(0, 3); // Pick a random LED (0 to 2)
int randomColor = random(0, 3); // Pick a random color (0 to 2)
digitalWrite(ledPins[randomColor], HIGH); // Turn on the LED with the random color
delay(500); // Flicker faster (every 500ms)
digitalWrite(ledPins[randomColor], LOW); // Turn off the LED
}
void startGame() {
gameStarted = true;
sequenceLength = 1;
randomSeed(millis());
for (int i = 0; i < maxSequence; i++) {
gameSequence[i] = random(0, 3); // Generate values between 0-8 representing LED and color combinations
}
Serial.println("Memorize the sequence:");
playSequence();
}
void playSequence() {
for (int currentStep = 0; currentStep < sequenceLength; currentStep++) {
int ledIndex = gameSequence[currentStep] / 3;
int color = gameSequence[currentStep] % 3;
lightUpLed(ledIndex, color);
delay(800);
turnOffAllLeds();
delay(200);
}
}
int getPlayerInput() {
Serial.println("Waiting for player input...");
int currentPlayerLED = 0;
int currentColor = 0;
int lastLEDValue = -1;
int lastColorValue = -1;
while (true) {
int ledJoystickValue = analogRead(joystickXPin);
int colorJoystickValue = analogRead(joystickYPin);
bool ledChanged = ledJoystickValue != lastLEDValue;
bool colorChanged = colorJoystickValue != lastColorValue;
if (ledChanged) {
if (abs(ledJoystickValue - 512) > deadzone) {
if (ledJoystickValue < 512) {
currentPlayerLED = currentPlayerLED <= 0 ? 2 : currentPlayerLED - 1;
} else {
currentPlayerLED = currentPlayerLED >= 2 ? 0 : currentPlayerLED + 1;
}
}
lastLEDValue = ledJoystickValue;
}
if (colorChanged) {
if (abs(colorJoystickValue - 512) > deadzone) {
if (colorJoystickValue < 512) {
currentColor = currentColor <= 0 ? 2 : currentColor - 1;
} else {
currentColor = currentColor >= 2 ? 0 : currentColor + 1;
}
}
lastColorValue = colorJoystickValue;
}
lightUpLed(currentPlayerLED, currentColor);
if (digitalRead(joystickButtonPin) == LOW) {
Serial.println("Selection confirmed");
while (digitalRead(joystickButtonPin) == LOW);
return currentPlayerLED * 3 + currentColor;
}
}
}
// Function to calculate joystick position for LED and color selection
int getJoystickPosition(int pin, int currentValue, int joystickValue, int max) {
// Check if the joystick has moved significantly (outside deadzone)
if (abs(joystickValue - 512) > deadzone) {
// Adjust the current value based on joystick position
if (joystickValue < 512) {
currentValue = max(1, currentValue - 1); // Move left or down
} else {
currentValue = min(max, currentValue + 1); // Move right or up
}
}
return currentValue; // Return the new position
}
// Function to light up a specific LED with a specific color
void lightUpLed(int ledIndex, int color) {
turnOffAllLeds(); // Ensure all LEDs are turned off first
digitalWrite(ledPins[color], LOW); // Turn on the selected LED with the chosen color (LOW for common anode)
}
// Function to turn off all LEDs
void turnOffAllLeds() {
for (int i = 0; i < 3; i++) {
digitalWrite(ledPins[i], HIGH); // Turn off each LED (HIGH for common anode)
}
}
// Function to play a success sound using the buzzer
void playSuccessSound() {
for (int i = 0; i < 100; i++) {
digitalWrite(buzzerPin, HIGH);
delayMicroseconds(1000); // Tone frequency for success
digitalWrite(buzzerPin, LOW);
delayMicroseconds(1000);
}
}
// Function to play an error sound using the buzzer
void playErrorSound() {
for (int i = 0; i < 50; i++) {
digitalWrite(buzzerPin, HIGH);
delayMicroseconds(500); // Tone frequency for error
digitalWrite(buzzerPin, LOW);
delayMicroseconds(500);
}
}