Mission 19 · Stage 2

Memory Game

game state with if decisions

Remember the Serial hint and press the correct button to grow your score.

Memory Game circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 2

Button A

pin 1

Button A

pin 2

Arduino

GND

Arduino

pin 3

Button B

pin 1

Button B

pin 2

Arduino

GND

Arduino

pin 13

Resistor

pin 1

Resistor

pin 2

Score LED

anode (+)

Score LED

cathode (-)

Arduino

GND

See it

Let's test your memory!

The screen gives a hint — press the matching button and build up your score.

Games keep little memory boxes, like your score, and use them to decide what happens next.

The story

The problem

The program needs to remember things between presses, not just read once.

Think of it like

Like remembering a card's color and putting it on the matching pile.

Meet the parts

It picks the hint and checks your answer.

Arduino

The brain

Loading part…

Press it when the hint says A.

Button A

Button A

Loading part…

Press it when the hint says B.

Button B

Button B

Loading part…

Keeps the score light safe.

Resistor

The protector

Loading part…

It flashes when you answer right.

Score LED

The score light

Loading part…

How it works

1

Pick the answer

The game secretly picks A or B and prints a hint for you.

lastChoice = random(0, 2);
2

Wait for your press

A press only counts while the game is waiting for your answer.

if (waiting && digitalRead(BTN_A) == LOW)
3

Right or wrong?

A right press adds to your score; a wrong press sets it back to 0.

if (choice == lastChoice) score++; else score = 0;

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 Button A

    Place the Button A (btnA) on the breadboard.

    Loading part…
  3. 3

    Place Button B

    Place the Button B (btnB) on the breadboard.

    Loading part…
  4. 4

    Place Resistor

    Place the Resistor (r1) on the breadboard.

    Loading part…
  5. 5

    Place Score LED

    Place the Score LED (led1) on the breadboard.

    Loading part…
  6. 6

    Connect Arduino pin 2 to Button A (btnA) 1.l

    Connect Arduino pin 2 to Button A (btnA) 1.l.

  7. 7

    Connect Button A (btnA) 2.l to Arduino GND

    Connect Button A (btnA) 2.l to Arduino GND.

  8. 8

    Connect Arduino pin 3 to Button B (btnB) 1.l

    Connect Arduino pin 3 to Button B (btnB) 1.l.

  9. 9

    Connect Button B (btnB) 2.l to Arduino GND

    Connect Button B (btnB) 2.l to Arduino GND.

  10. 10

    Connect Arduino pin 13 to Resistor (r1) 1

    Connect Arduino pin 13 to Resistor (r1) 1.

  11. 11

    Connect Resistor (r1) 2 to Score LED (led1) anode (+)

    Connect Resistor (r1) 2 to Score LED (led1) anode (+).

  12. 12

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

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

Try it

  • Read the hint on the screen before you press.
  • Right answers stack up your score — a wrong one sends it back to 0.

Peek at code

Memory boxes

int lastChoice = 0;
int score = 0;
bool waiting = true;

lastChoice, score, and waiting remember the game between loop() runs.

check()

void check(int choice) {
  waiting = false;
  if (choice == lastChoice) {
    score++;
    digitalWrite(SCORE_LED, HIGH);
    Serial.print("Correct! Score: ");
    Serial.println(score);
  } else {
    score = 0;
    digitalWrite(SCORE_LED, LOW);
    Serial.println("Wrong — score reset");
  }
  delay(800);
  digitalWrite(SCORE_LED, LOW);
  delay(400);
  pickNext();
  while (digitalRead(BTN_A) == LOW || digitalRead(BTN_B) == LOW) {
    delay(10);
  }
}

It compares your press to the secret answer and updates your score and light.

The main loop

void loop() {
  if (waiting && digitalRead(BTN_A) == LOW) {
    check(0);
  } else if (waiting && digitalRead(BTN_B) == LOW) {
    check(1);
  }
  delay(20);
}

It sends button A or B to check(), but only while the game is waiting.

Show full sketch (memory-game.ino)
const int BTN_A = 2;
const int BTN_B = 3;
const int SCORE_LED = 13;
int lastChoice = 0;
int score = 0;
bool waiting = true;
void pickNext() {
  lastChoice = random(0, 2);
  waiting = true;
  Serial.println(lastChoice == 0 ? "Hint: press A" : "Hint: press B");
}
void check(int choice) {
  waiting = false;
  if (choice == lastChoice) {
    score++;
    digitalWrite(SCORE_LED, HIGH);
    Serial.print("Correct! Score: ");
    Serial.println(score);
  } else {
    score = 0;
    digitalWrite(SCORE_LED, LOW);
    Serial.println("Wrong — score reset");
  }
  delay(800);
  digitalWrite(SCORE_LED, LOW);
  delay(400);
  pickNext();
  while (digitalRead(BTN_A) == LOW || digitalRead(BTN_B) == LOW) {
    delay(10);
  }
}
void setup() {
  pinMode(BTN_A, INPUT_PULLUP);
  pinMode(BTN_B, INPUT_PULLUP);
  pinMode(SCORE_LED, OUTPUT);
  Serial.begin(9600);
  randomSeed(analogRead(0));
  Serial.println("Memory Game — match the hint!");
  pickNext();
}
void loop() {
  if (waiting && digitalRead(BTN_A) == LOW) {
    check(0);
  } else if (waiting && digitalRead(BTN_B) == LOW) {
    check(1);
  }
  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 does the waiting box do?

  • A. Decides if your press counts right now
  • B. Sets the random seed only
  • C. Changes how bright the light is
Why: Yes! waiting makes sure only one answer counts per hint.

Code lab — try on your own

  1. Change the hint words on line 10 to include the word "Remember".

    Hint: Edit the Serial.println inside pickNext().

  2. Give more time after each answer — change delay(800) to delay(1200).

    Hint: That's line 24 inside check().

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 screen says "press A" or "press B". Pick the right one and your score goes up.

Tip

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

const int BTN_A = 2;
const int BTN_B = 3;
const int SCORE_LED = 13;
int lastChoice = 0;
int score = 0;
bool waiting = true;
void pickNext() {
  lastChoice = random(0, 2);
  waiting = true;
  Serial.println(lastChoice == 0 ? "Hint: press A" : "Hint: press B");
}
void check(int choice) {
  waiting = false;
  if (choice == lastChoice) {
    score++;
    digitalWrite(SCORE_LED, HIGH);
    Serial.print("Correct! Score: ");
    Serial.println(score);
  } else {
    score = 0;
    digitalWrite(SCORE_LED, LOW);
    Serial.println("Wrong — score reset");
  }
  delay(800);
  digitalWrite(SCORE_LED, LOW);
  delay(400);
  pickNext();
  while (digitalRead(BTN_A) == LOW || digitalRead(BTN_B) == LOW) {
    delay(10);
  }
}

setup()

Big idea

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

In this project

It gets both buttons and the score light ready, mixes up the randomness, and shows the first hint.

Why here

Things we do only once go inside setup().

void setup() {
  pinMode(BTN_A, INPUT_PULLUP);
  pinMode(BTN_B, INPUT_PULLUP);
  pinMode(SCORE_LED, OUTPUT);
  Serial.begin(9600);
  randomSeed(analogRead(0));
  Serial.println("Memory Game — match the hint!");
  pickNext();
}

loop()

Big idea

loop() runs again and again, forever.

In this project

It waits for you to press A or B, then checks your answer.

Why here

Things that repeat go inside loop().

void loop() {
  if (waiting && digitalRead(BTN_A) == LOW) {
    check(0);
  } else if (waiting && digitalRead(BTN_B) == LOW) {
    check(1);
  }
  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 score light pin to push power out.

Why here

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

  pinMode(BTN_A, INPUT_PULLUP);

digitalWrite

Big idea

digitalWrite turns a pin ON or OFF.

In this project

It lights the score LED when you answer right.

Why here

It is used inside check() to show how you did.

    digitalWrite(SCORE_LED, HIGH);

digitalRead

Big idea

digitalRead checks if a pin is ON or OFF.

In this project

It checks which button you pressed.

Why here

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

  while (digitalRead(BTN_A) == LOW || digitalRead(BTN_B) == 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 hints are different each game.

  randomSeed(analogRead(0));

delay

Big idea

delay means wait. Nothing else happens while it waits.

In this project

It pauses after each answer so you can see how you did.

Why here

Right after we show the score light.

  delay(800);

random

Big idea

random picks a surprise number for you.

In this project

It secretly picks A or B for the next hint.

Why here

When we set up the next round.

  lastChoice = random(0, 2);

begin

Big idea

Serial.begin opens a way for the board to send words to your computer screen.

In this project

It lets the program print the hints and your score.

Why here

It goes in setup() once, before we print anything.

  Serial.begin(9600);

print

Big idea

print sends words to the screen but stays on the same line.

In this project

It prints "Correct! Score: " right before your number.

Why here

In check() so the label and number sit together.

    Serial.print("Correct! Score: ");

println

Big idea

println sends a line of words to the screen.

In this project

It prints the hint of which button to press.

Why here

In pickNext() so each hint is on its own line.

  Serial.println(lastChoice == 0 ? "Hint: press A" : "Hint: press B");