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

Remember and choose!

Serial hints “press A” or “press B” — pick the right button to grow your score.

Games keep state variables (score, waiting) and branch with if.

The story

The problem

Programs need memory between loop passes — not just instant reads.

Think of it like

Like remembering a card color and picking the matching pile.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

Choice A in the memory game

Button A

Input

Loading part…

Choice B in the memory game

Button B

Input

Loading part…

Protects the score LED

Resistor

Safety

Loading part…

Flashes on correct answers

Score LED

Output

Loading part…

How it works

1

Pick target

pickNext() secretly chooses A or B and prints a hint.

lastChoice = random(0, 2);
2

Wait for input

Only while waiting is true do button presses count.

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

Score or reset

Correct if grows score; wrong if clears it — game state with if.

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!

    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 Serial hint before pressing.
  • Correct answers stack score — wrong resets to 0.

Peek at code

Game variables

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

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

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);
  }
}

Compares choice to lastChoice and updates score and LED feedback.

Main loop

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

Routes button A or B to check() only when waiting for an answer.

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 repeating work belong?

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

Q2. What does the waiting variable control?

  • A. Whether button presses are accepted
  • B. The random seed only
  • C. LED brightness
Why: Correct — waiting gates input so only one answer counts per hint.

Code lab — try on your own

  1. Change the hint text on line 10 to include the word “Remember”.

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

  2. Give more time after a wrong answer — change delay(800) to delay(1200) in check().

    Hint: Line 24.

Code walkthrough

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

Program overview

Technical

Sketches have globals, then setup() once, then loop() forever.

In this project

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

Why here

Read from top to bottom. Hover words or lines for 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()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Memory Game.

Why here

One-time setup belongs here—not in loop().

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()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Memory Game.

Why here

Repeating work (blink, read sensors) goes here.

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

Try this: Change numbers in loop(), then compile and run the simulator.

pinMode

Technical

Tells a pin if it listens or drives something.

In this project

Gets the Memory Game circuit ready in the simulator.

Why here

Goes in setup() because we only set pins once at the start.

  pinMode(BTN_A, INPUT_PULLUP);

digitalWrite

Technical

Turns a pin ON or OFF.

In this project

Controls lights, motors, or buzzers in Memory Game.

Why here

Goes in loop() so it can keep changing while the program runs.

    digitalWrite(SCORE_LED, HIGH);

digitalRead

Technical

Checks if a pin is ON or OFF.

In this project

Reads buttons or sensors in Memory Game.

Why here

Goes in loop() so we can react when something changes.

  while (digitalRead(BTN_A) == LOW || digitalRead(BTN_B) == LOW) {

analogRead

Technical

Reads a sensor number from 0 to 1023.

In this project

Turns a sensor signal into a number for Memory Game.

Why here

Goes in loop() to keep checking the sensor.

  randomSeed(analogRead(0));

delay

Technical

Waits for some time. Nothing else runs during the wait.

In this project

Controls speed so you can see Memory Game in the simulator.

Why here

Right after an action that should stay the same for a moment.

  delay(800);

random

Technical

Picks a random number in a range.

In this project

Adds randomness in Memory Game.

Why here

In loop() when you want different values each time.

  lastChoice = random(0, 2);

begin

Technical

Starts talking to the computer screen (serial monitor).

In this project

Lets Memory Game print debug messages.

Why here

Goes in setup() once before any Serial.print.

  Serial.begin(9600);

print

Technical

Sends text to the serial monitor without a new line.

In this project

Shows values from Memory Game on the screen.

Why here

In loop() or setup() when you want to see what the board is doing.

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

println

Technical

Sends text to the serial monitor and starts a new line.

In this project

Prints one line of output for Memory Game.

Why here

In loop() when each reading should appear on its own line.

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