Go back

Build a Simon Says Memory Game with Arduino on Velxio

Introduction

Remember the classic Simon Says game? It’s the perfect project to learn about arrays, state machines, and user input handling on Arduino. In this tutorial, we’ll build a fully functional Simon Says game using four LEDs and four pushbuttons, all simulated right in your browser with Velxio. No hardware needed!

Circuit diagram of simon-says on the Velxio simulator canvas

Circuit Walkthrough

Let’s look at the components and how they’re connected.

Components

ComponentIDPurpose
Arduino Unoarduino-unoThe brain of the game
Red LEDled-redOutput for red sequence
Green LEDled-greenOutput for green sequence
Blue LEDled-blueOutput for blue sequence
Yellow LEDled-yellowOutput for yellow sequence
Red Buttonbutton-redUser input for red
Green Buttonbutton-greenUser input for green
Blue Buttonbutton-blueUser input for blue
Yellow Buttonbutton-yellowUser input for yellow

Wiring

Each LED’s anode (A) connects to a digital pin on the Arduino, and its cathode (C) goes to GND through a current-limiting resistor (simulated internally). The buttons are wired with one leg (1.l) to a digital pin and the other leg (2.l) to GND, using the Arduino’s internal pull-up resistors.

All ground connections share the Arduino’s GND pin.

Code Walkthrough

Open the code editor in Velxio to see the full sketch.

Source code for simon-says in the Velxio Monaco editor

Constants and Variables

const int LED_PINS[] = {8, 9, 10, 11};
const int BUTTON_PINS[] = {2, 3, 4, 5};
const int NUM_LEDS = 4;

int sequence[100];
int sequenceLength = 0;
int currentStep = 0;

We define arrays for LED and button pins, matching the wiring. sequence stores the random pattern (up to 100 steps). sequenceLength tracks how many steps the player must repeat, and currentStep tracks the player’s progress.

Setup

void setup() {
  Serial.begin(9600);

  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(LED_PINS[i], OUTPUT);
    pinMode(BUTTON_PINS[i], INPUT_PULLUP);
  }

  randomSeed(millis());
  newGame();
}

We set LED pins as outputs and button pins as inputs with internal pull-ups (so buttons read LOW when pressed). randomSeed(millis()) ensures a different sequence each game. newGame() starts the first round.

Game Logic

newGame() resets the sequence length to 1, adds a random step, and plays the sequence.

void newGame() {
  sequenceLength = 1;
  currentStep = 0;
  addToSequence();
  playSequence();
}

void addToSequence() {
  sequence[sequenceLength - 1] = random(0, NUM_LEDS);
}

void playSequence() {
  for (int i = 0; i < sequenceLength; i++) {
    flashLED(sequence[i]);
    delay(500);
  }
}

void flashLED(int led) {
  digitalWrite(LED_PINS[led], HIGH);
  delay(300);
  digitalWrite(LED_PINS[led], LOW);
}

playSequence() iterates through the stored sequence, flashing each LED for 300ms with a 500ms gap.

Main Loop

void loop() {
  for (int i = 0; i < NUM_LEDS; i++) {
    if (digitalRead(BUTTON_PINS[i]) == LOW) {
      flashLED(i);

      if (i == sequence[currentStep]) {
        currentStep++;
        if (currentStep == sequenceLength) {
          delay(1000);
          sequenceLength++;
          currentStep = 0;
          addToSequence();
          playSequence();
        }
      } else {
        // Wrong button - game over
        for (int j = 0; j < 3; j++) {
          for (int k = 0; k < NUM_LEDS; k++) {
            digitalWrite(LED_PINS[k], HIGH);
          }
          delay(200);
          for (int k = 0; k < NUM_LEDS; k++) {
            digitalWrite(LED_PINS[k], LOW);
          }
          delay(200);
        }
        newGame();
      }

      delay(300);
      while (digitalRead(BUTTON_PINS[i]) == LOW);
    }
  }
}

The loop continuously checks all buttons. When a button is pressed (LOW), it flashes the corresponding LED. If the pressed button matches the expected step (sequence[currentStep]), we advance. If the player completes the sequence, we add a new step and play the longer sequence. If wrong, all LEDs flash three times and a new game starts.

Key Concepts

Common Pitfalls & Debugging Tips

Suggested Extensions

Try It Yourself!

Ready to play? Open the live example on Velxio and start the simulation. Press the buttons to repeat the sequence. How far can you go?

Launch Simon Says on Velxio

Happy building!


Share this post on:
David Montero

Written by

David Montero

Creator of Velxio, the open-source circuit and Arduino simulator.

GitHub velxio.dev

Related posts


Previous Post
TFT ILI9341 Display Demo: Graphics & Bouncing Ball on Arduino
Next Post
RGB LED Color Cycling with Arduino on Velxio