//made by rikhil
#define GREEN_BTN A0
#define RED_BTN A1
#define BLUE_BTN A2
#define YELLOW_BTN A3
#define LED_PIN LED_BUILTIN
#define BTN_COUNT 4
#define MAX_SEQ 32
const char* colorNames[BTN_COUNT] = { "Green", "Red", "Blue", "Yellow" };
const int btnPins[BTN_COUNT] = { GREEN_BTN, RED_BTN, BLUE_BTN, YELLOW_BTN };
uint8_t sequence[MAX_SEQ];
uint8_t level = 0;
bool gameOver = false;
void blinkLED(int times, int delayMs) {
for (int i = 0; i < times; i++) {
digitalWrite(LED_PIN, HIGH);
delay(delayMs);
digitalWrite(LED_PIN, LOW);
delay(delayMs);
}
}
// Wait for any button press and return its index
int waitButton() {
while (true) {
for (int i = 0; i < BTN_COUNT; i++) {
if (digitalRead(btnPins[i]) == LOW) {
// Wait for release (debounce)
while (digitalRead(btnPins[i]) == LOW) delay(10);
return i;
}
}
delay(5);
}
}
// "Play" the sequence by printing to Serial and blinking LED
void playSequence() {
Serial.print("Sequence: ");
for (int i = 0; i < level; i++) {
Serial.print(colorNames[sequence[i]]);
Serial.print(" ");
blinkLED(1, 200);
delay(200);
}
Serial.println();
}
void setup() {
Serial.begin(115200);
for (int i = 0; i < BTN_COUNT; i++) {
pinMode(btnPins[i], INPUT_PULLUP);
}
pinMode(LED_PIN, OUTPUT);
randomSeed(analogRead(A0));
Serial.println("=== Simon Says Memory Game ===");
Serial.println("Repeat the sequence using the colored buttons!");
Serial.println("Press any button to start.");
while (waitButton() == -1);
}
void loop() {
// Start or restart game
level = 1;
gameOver = false;
for (int i = 0; i < MAX_SEQ; i++) {
sequence[i] = random(0, BTN_COUNT);
}
while (!gameOver) {
Serial.print("\nLevel ");
Serial.println(level);
delay(500);
playSequence();
// Player input
for (int i = 0; i < level; i++) {
int btn = waitButton();
if (btn == sequence[i]) {
blinkLED(1, 50); // Short blink for correct
} else {
Serial.println("Wrong button! Game Over.");
blinkLED(3, 150); // Triple blink for failure
Serial.print("Your score: ");
Serial.println(level - 1);
gameOver = true;
break;
}
}
if (!gameOver) {
Serial.println("Correct! Next round...");
blinkLED(2, 100); // Double blink for success
level++;
if (level > MAX_SEQ) {
Serial.println("Congratulations! You beat the game!");
gameOver = true;
}
}
delay(700);
}
Serial.println("\nPress any button to play again.");
while (waitButton() == -1);
}