#include <Arduino.h>
// Pin assignments
const int ledPins[] = {2, 3, 4, 5}; // LED pins
const int buttonPins[] = {6, 7, 8, 9}; // Button pins
const int buzzerPin = 10; // Buzzer pin
// Frequencies for each button
const int tones[] = {262, 330, 392, 494}; // C4, E4, G4, B4
// Game variables
const int sequenceMaxLength = 20; // Maximum sequence length
int sequence[sequenceMaxLength]; // Stores the LED sequence
int sequenceLength = 0; // Current sequence length
int playerIndex = 0; // Tracks player's progress
bool gameActive = false; // Game status
// Function prototypes
void waitForStart();
void playSequence();
void getPlayerInput();
void lightLed(int index);
void turnOffLeds();
void gameOver();
void startGame();
void ledChaseEffect(int durationMs);
void setup() {
// Configure LED and buzzer pins as outputs
for (int i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW);
}
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
// Configure button pins as inputs with pull-up resistors
for (int i = 0; i < 4; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
// Initialize random seed
randomSeed(analogRead(A0));
// Wait for the player to start the game
waitForStart();
}
void loop() {
if (gameActive) {
playSequence();
getPlayerInput();
}
}
// Waits for any button to be pressed to start the game
void waitForStart() {
turnOffLeds();
while (true) {
for (int i = 0; i < 4; i++) {
if (digitalRead(buttonPins[i]) == LOW) { // Button pressed
lightLed(i); // Light up the LED corresponding to the button pressed
delay(200);
turnOffLeds();
startGame(); // Start the game
return;
}
}
}
}
// Starts a new game
void startGame() {
sequenceLength = 1; // Start with one LED in the sequence
gameActive = true;
// Generate the initial sequence
for (int i = 0; i < sequenceMaxLength; i++) {
sequence[i] = random(0, 4); // Random LED index (0-3)
}
delay(1000); // Wait before starting the game
}
// Plays the current sequence
void playSequence() {
for (int i = 0; i < sequenceLength; i++) {
lightLed(sequence[i]);
delay(500); // LED on duration
turnOffLeds();
delay(300); // Pause between LEDs
}
}
// Lights a specific LED and plays its tone
void lightLed(int index) {
digitalWrite(ledPins[index], HIGH);
tone(buzzerPin, tones[index], 300); // Play tone for 300ms
}
// Turns off all LEDs
void turnOffLeds() {
for (int i = 0; i < 4; i++) {
digitalWrite(ledPins[i], LOW);
}
}
// Gets input from the player
void getPlayerInput() {
playerIndex = 0; // Reset player's progress
while (playerIndex < sequenceLength) {
for (int i = 0; i < 4; i++) {
if (digitalRead(buttonPins[i]) == LOW) { // Button pressed
lightLed(i); // Light the corresponding LED and play tone
delay(200); // Short feedback duration
turnOffLeds(); // Turn off LEDs quickly after feedback
if (i == sequence[playerIndex]) {
playerIndex++; // Correct input, move to the next
delay(100); // Debounce delay
} else {
gameOver(); // Incorrect input
return;
}
}
}
}
// If the player completes the sequence
sequenceLength++; // Increase the sequence length
delay(1000); // Pause before the next round
}
// Ends the game with a buzzer sound and LED chase effect
void gameOver() {
gameActive = false;
// Play game-over sound
for (int i = 0; i < 3; i++) {
tone(buzzerPin, 200, 500);
delay(500);
}
// Perform LED chase effect for 5 seconds
ledChaseEffect(5000);
// Wait for the player to restart
waitForStart();
}
// Performs an LED chase effect for a given duration (in milliseconds)
void ledChaseEffect(int durationMs) {
unsigned long startTime = millis();
while (millis() - startTime < durationMs) {
for (int i = 0; i < 4; i++) {
lightLed(i);
delay(100); // Delay between LED steps
turnOffLeds();
}
}
}