Mission 18 · Stage 2

Simon Says

sequence compare with input

Watch a growing LED sequence, then repeat it with matching buttons.

Simon Says circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 2

Red button

pin 1

Red button

pin 2

Arduino

GND

Arduino

pin 3

Green button

pin 1

Green button

pin 2

Arduino

GND

Arduino

pin 4

Yellow button

pin 1

Yellow button

pin 2

Arduino

GND

Arduino

pin 5

Blue button

pin 1

Blue button

pin 2

Arduino

GND

Arduino

pin 8

Red resistor

pin 2

Red resistor

pin 1

Red LED

anode (+)

Red LED

cathode (-)

Arduino

GND

Arduino

pin 9

Green resistor

pin 2

Green resistor

pin 1

Green LED

anode (+)

Green LED

cathode (-)

Arduino

GND

Arduino

pin 10

Yellow resistor

pin 2

Yellow resistor

pin 1

Yellow LED

anode (+)

Yellow LED

cathode (-)

Arduino

GND

Arduino

pin 11

Blue resistor

pin 2

Blue resistor

pin 1

Blue LED

anode (+)

Blue LED

cathode (-)

Arduino

GND

See it

Let's test your memory!

Watch the colors flash, then copy them back — one extra color every round.

This is just like the classic Simon toy that grows harder as you go.

The story

The problem

You must check each press against a saved pattern, one color at a time.

Think of it like

Like the game "Simon says" — copy the pattern exactly or you are out.

Meet the parts

It remembers the pattern and checks your presses.

Arduino

The brain

Loading part…

Press it for the red color in the pattern.

Red button

The red button

Loading part…

Press it for the green color in the pattern.

Green button

The green button

Loading part…

Press it for the yellow color in the pattern.

Yellow button

The yellow button

Loading part…

Press it for the blue color in the pattern.

Blue button

The blue button

Loading part…

It flashes when red is in the pattern.

Red LED

The red light

Loading part…

It flashes when green is in the pattern.

Green LED

The green light

Loading part…

It flashes when yellow is in the pattern.

Yellow LED

The yellow light

Loading part…

It flashes when blue is in the pattern.

Blue LED

The blue light

Loading part…

Keeps the red light safe.

Red resistor

The protector

Loading part…

Keeps the green light safe.

Green resistor

The protector

Loading part…

Keeps the yellow light safe.

Yellow resistor

The protector

Loading part…

Keeps the blue light safe.

Blue resistor

The protector

Loading part…

How it works

1

Play the pattern

It flashes the lights in order from the saved pattern.

showSequence();
2

Read your button

Each button stands for one color, so we know which one you pressed.

digitalRead(BTN_PINS[i]) == LOW
3

Did it match?

A wrong press ends the round; a right one adds a new color to the pattern.

if (i != sequence[step]) failPattern();

Then loop back to step 1

Build the circuit

Follow these steps in order. Match the wires to the colors shown.

  1. 1

    Place Arduino

    Place the Arduino (uno) on the breadboard.

    Arduino placed — ready to build!

    Loading part…
  2. 2

    Place Red button

    Place the Red button (btn0) on the breadboard.

    Loading part…
  3. 3

    Place Green button

    Place the Green button (btn1) on the breadboard.

    Loading part…
  4. 4

    Place Yellow button

    Place the Yellow button (btn2) on the breadboard.

    Loading part…
  5. 5

    Place Blue button

    Place the Blue button (btn3) on the breadboard.

    Loading part…
  6. 6

    Place Red LED

    Place the Red LED (led0) on the breadboard.

    Loading part…
  7. 7

    Place Green LED

    Place the Green LED (led1) on the breadboard.

    Loading part…
  8. 8

    Place Yellow LED

    Place the Yellow LED (led2) on the breadboard.

    Loading part…
  9. 9

    Place Blue LED

    Place the Blue LED (led3) on the breadboard.

    Loading part…
  10. 10

    Place Red resistor

    Place the Red resistor (r0) on the breadboard.

    Loading part…
  11. 11

    Place Green resistor

    Place the Green resistor (r1) on the breadboard.

    Loading part…
  12. 12

    Place Yellow resistor

    Place the Yellow resistor (r2) on the breadboard.

    Loading part…
  13. 13

    Place Blue resistor

    Place the Blue resistor (r3) on the breadboard.

    Loading part…
  14. 14

    Connect Arduino pin 2 to Red button (btn0) 1.l

    Connect Arduino pin 2 to Red button (btn0) 1.l.

  15. 15

    Connect Red button (btn0) 2.l to Arduino GND

    Connect Red button (btn0) 2.l to Arduino GND.

  16. 16

    Connect Arduino pin 3 to Green button (btn1) 1.l

    Connect Arduino pin 3 to Green button (btn1) 1.l.

  17. 17

    Connect Green button (btn1) 2.l to Arduino GND

    Connect Green button (btn1) 2.l to Arduino GND.

  18. 18

    Connect Arduino pin 4 to Yellow button (btn2) 1.l

    Connect Arduino pin 4 to Yellow button (btn2) 1.l.

  19. 19

    Connect Yellow button (btn2) 2.l to Arduino GND

    Connect Yellow button (btn2) 2.l to Arduino GND.

  20. 20

    Connect Arduino pin 5 to Blue button (btn3) 1.l

    Connect Arduino pin 5 to Blue button (btn3) 1.l.

  21. 21

    Connect Blue button (btn3) 2.l to Arduino GND

    Connect Blue button (btn3) 2.l to Arduino GND.

  22. 22

    Connect Arduino pin 8 to Red resistor (r0) 2

    Connect Arduino pin 8 to Red resistor (r0) 2.

  23. 23

    Connect Red resistor (r0) 1 to Red LED (led0) anode (+)

    Connect Red resistor (r0) 1 to Red LED (led0) anode (+).

  24. 24

    Connect Red LED (led0) cathode (-) to Arduino GND

    Connect Red LED (led0) cathode (-) to Arduino GND.

  25. 25

    Connect Arduino pin 9 to Green resistor (r1) 2

    Connect Arduino pin 9 to Green resistor (r1) 2.

  26. 26

    Connect Green resistor (r1) 1 to Green LED (led1) anode (+)

    Connect Green resistor (r1) 1 to Green LED (led1) anode (+).

  27. 27

    Connect Green LED (led1) cathode (-) to Arduino GND

    Connect Green LED (led1) cathode (-) to Arduino GND.

  28. 28

    Connect Arduino pin 10 to Yellow resistor (r2) 2

    Connect Arduino pin 10 to Yellow resistor (r2) 2.

  29. 29

    Connect Yellow resistor (r2) 1 to Yellow LED (led2) anode (+)

    Connect Yellow resistor (r2) 1 to Yellow LED (led2) anode (+).

  30. 30

    Connect Yellow LED (led2) cathode (-) to Arduino GND

    Connect Yellow LED (led2) cathode (-) to Arduino GND.

  31. 31

    Connect Arduino pin 11 to Blue resistor (r3) 2

    Connect Arduino pin 11 to Blue resistor (r3) 2.

  32. 32

    Connect Blue resistor (r3) 1 to Blue LED (led3) anode (+)

    Connect Blue resistor (r3) 1 to Blue LED (led3) anode (+).

  33. 33

    Connect Blue LED (led3) cathode (-) to Arduino GND

    Connect Blue LED (led3) cathode (-) to Arduino GND.

Try it

  • Watch the color order first — then press the matching buttons.
  • A wrong press blinks all the lights — and the pattern starts over.

Peek at code

Where the pattern lives

const int BTN_PINS[] = {2, 3, 4, 5};
const int LED_PINS[] = {8, 9, 10, 11};
const int NUM = 4;
const int MAX_LEN = 6;
int sequence[MAX_LEN];
int seqLen = 1;
int step = 0;
enum Phase { SHOW, INPUT };
Phase phase = SHOW;

sequence holds the pattern; seqLen is how long it is right now and it grows as you win.

showSequence()

void showSequence() {
  for (int i = 0; i < seqLen; i++) {
    flash(sequence[i], 350);
    delay(180);
  }
}

It plays the pattern by flashing the matching light for each color.

Checking your turn

void loop() {
  if (phase == SHOW) {
    showSequence();
    step = 0;
    phase = INPUT;
    return;
  }
  for (int i = 0; i < NUM; i++) {
    if (digitalRead(BTN_PINS[i]) == LOW) {
      delay(180);
      flash(i, 250);
      if (i != sequence[step]) {
        failPattern();
        return;
      }
      step++;
      if (step >= seqLen) {
        seqLen++;
        if (seqLen > MAX_LEN) {
          seqLen = MAX_LEN;
        }
        sequence[seqLen - 1] = random(0, NUM);
        phase = SHOW;
      }
      while (digitalRead(BTN_PINS[i]) == LOW) {
        delay(10);
      }
    }
  }
  delay(20);
}

On your turn, each press is checked against the saved pattern — that is the heart of Simon.

Show full sketch (simon-says.ino)
const int BTN_PINS[] = {2, 3, 4, 5};
const int LED_PINS[] = {8, 9, 10, 11};
const int NUM = 4;
const int MAX_LEN = 6;
int sequence[MAX_LEN];
int seqLen = 1;
int step = 0;
enum Phase { SHOW, INPUT };
Phase phase = SHOW;
void flash(int idx, int ms) {
  digitalWrite(LED_PINS[idx], HIGH);
  delay(ms);
  digitalWrite(LED_PINS[idx], LOW);
  delay(120);
}
void showSequence() {
  for (int i = 0; i < seqLen; i++) {
    flash(sequence[i], 350);
    delay(180);
  }
}
void failPattern() {
  for (int blink = 0; blink < 3; blink++) {
    for (int i = 0; i < NUM; i++) {
      digitalWrite(LED_PINS[i], HIGH);
    }
    delay(200);
    for (int i = 0; i < NUM; i++) {
      digitalWrite(LED_PINS[i], LOW);
    }
    delay(200);
  }
  seqLen = 1;
  sequence[0] = random(0, NUM);
  phase = SHOW;
}
void setup() {
  for (int i = 0; i < NUM; i++) {
    pinMode(BTN_PINS[i], INPUT_PULLUP);
    pinMode(LED_PINS[i], OUTPUT);
  }
  randomSeed(analogRead(0));
  sequence[0] = random(0, NUM);
}
void loop() {
  if (phase == SHOW) {
    showSequence();
    step = 0;
    phase = INPUT;
    return;
  }
  for (int i = 0; i < NUM; i++) {
    if (digitalRead(BTN_PINS[i]) == LOW) {
      delay(180);
      flash(i, 250);
      if (i != sequence[step]) {
        failPattern();
        return;
      }
      step++;
      if (step >= seqLen) {
        seqLen++;
        if (seqLen > MAX_LEN) {
          seqLen = MAX_LEN;
        }
        sequence[seqLen - 1] = random(0, NUM);
        phase = SHOW;
      }
      while (digitalRead(BTN_PINS[i]) == LOW) {
        delay(10);
      }
    }
  }
  delay(20);
}

Quick quiz

Q1. Where does work that repeats go?

  • A. loop()
  • B. setup()
  • C. pinMode only
Why: Yes! loop() runs again and again.

Q2. What happens when you press the wrong button?

  • A. All lights blink and the pattern starts over
  • B. The pattern gets longer
  • C. Nothing — it ignores wrong presses
Why: Yes! A wrong press blinks the lights and the pattern goes back to 1 color.

Code lab — try on your own

  1. Slow the pattern down — change the flash time 350 to 500 on line 18.

    Hint: That's the flash(...) call on line 18.

  2. Add a note (comment) on line 56 that says it checks for a wrong button.

    Hint: That's the if (i != sequence[step]) line.

Code walkthrough

A line-by-line tour of the sketch — the same steps as in Robot Gurukul Studio.

Program overview

Big idea

Every Arduino program has a top part, a setup() part that runs once, and a loop() part that runs again and again.

In this project

The lights flash a color pattern. You press the matching buttons to copy it.

Tip

Read from the top to the bottom. Tap any word or line if you need help!

const int BTN_PINS[] = {2, 3, 4, 5};
const int LED_PINS[] = {8, 9, 10, 11};
const int NUM = 4;
const int MAX_LEN = 6;
int sequence[MAX_LEN];
int seqLen = 1;
int step = 0;
enum Phase { SHOW, INPUT };
Phase phase = SHOW;
void flash(int idx, int ms) {
  digitalWrite(LED_PINS[idx], HIGH);
  delay(ms);
  digitalWrite(LED_PINS[idx], LOW);
  delay(120);
}
void showSequence() {
  for (int i = 0; i < seqLen; i++) {
    flash(sequence[i], 350);
    delay(180);
  }
}
void failPattern() {
  for (int blink = 0; blink < 3; blink++) {
    for (int i = 0; i < NUM; i++) {
      digitalWrite(LED_PINS[i], HIGH);
    }
    delay(200);
    for (int i = 0; i < NUM; i++) {
      digitalWrite(LED_PINS[i], LOW);
    }
    delay(200);
  }
  seqLen = 1;
  sequence[0] = random(0, NUM);
  phase = SHOW;
}

setup()

Big idea

setup() runs one time when the board turns on.

In this project

It gets all 4 buttons and 4 lights ready, mixes up the randomness, and picks the first color.

Why here

Things we do only once go inside setup().

void setup() {
  for (int i = 0; i < NUM; i++) {
    pinMode(BTN_PINS[i], INPUT_PULLUP);
    pinMode(LED_PINS[i], OUTPUT);
  }
  randomSeed(analogRead(0));
  sequence[0] = random(0, NUM);
}

loop()

Big idea

loop() runs again and again, forever.

In this project

It shows the pattern, then checks if your presses match it.

Why here

Things that repeat go inside loop().

void loop() {
  if (phase == SHOW) {
    showSequence();
    step = 0;
    phase = INPUT;
    return;
  }
  for (int i = 0; i < NUM; i++) {
    if (digitalRead(BTN_PINS[i]) == LOW) {
      delay(180);
      flash(i, 250);
      if (i != sequence[step]) {
        failPattern();
        return;
      }
      step++;
      if (step >= seqLen) {
        seqLen++;
        if (seqLen > MAX_LEN) {
          seqLen = MAX_LEN;
        }
        sequence[seqLen - 1] = random(0, NUM);
        phase = SHOW;
      }
      while (digitalRead(BTN_PINS[i]) == LOW) {
        delay(10);
      }
    }
  }
  delay(20);
}

Try this: Change a number inside loop(), then press Run to see what happens.

pinMode

Big idea

pinMode tells a pin if it will listen or push power out.

In this project

It sets the button pins to listen and the light pins to push power out.

Why here

It goes in setup() because we only set it once.

    pinMode(BTN_PINS[i], INPUT_PULLUP);

digitalWrite

Big idea

digitalWrite turns a pin ON or OFF.

In this project

It flashes the color lights on and off.

Why here

It is used to show the pattern and your presses.

  digitalWrite(LED_PINS[idx], HIGH);

digitalRead

Big idea

digitalRead checks if a pin is ON or OFF.

In this project

It checks which color button you pressed.

Why here

It goes in loop() so we can react to your presses.

    if (digitalRead(BTN_PINS[i]) == LOW) {

analogRead

Big idea

analogRead reads a number from a pin, from 0 up to 1023.

In this project

It reads a wiggly number from an empty pin to mix up the randomness.

Why here

In setup() so the pattern is different each game.

  randomSeed(analogRead(0));

delay

Big idea

delay means wait. Nothing else happens while it waits.

In this project

Short waits let you see each color flash on and off.

Why here

Between flashing the lights.

  delay(ms);

random

Big idea

random picks a surprise number for you.

In this project

It picks the next color to add to the pattern.

Why here

When the game makes the pattern longer.

  sequence[0] = random(0, NUM);